Skip to content

Commit

Permalink
docs(readme): improve documentation and logging functionality
Browse files Browse the repository at this point in the history
- Add example of logging data on `gin.Context` in README
- Add `WithContext` option documentation in `options.go`

Signed-off-by: appleboy <[email protected]>
  • Loading branch information
appleboy committed Nov 29, 2024
1 parent 637ac38 commit 62fff47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ func main() {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Example of logging data on gin.Context
r.GET("/context", logger.SetLogger(
logger.WithContext(func(c *gin.Context, e *zerolog.Event) *zerolog.Event {
return e.Any("data1", c.MustGet("data1")).Any("data2", c.MustGet("data2"))
}),
), func(c *gin.Context) {
c.Set("data1", rand.Intn(100))
c.Set("data2", rand.Intn(100))
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Example of skipper usage
r.GET("/health", logger.SetLogger(
logger.WithSkipper(func(c *gin.Context) bool {
Expand Down
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ func WithSkipper(s Skipper) Option {
})
}

// WithContext is an option for configuring the logger with a custom context function.
// The provided function takes a *gin.Context and a *zerolog.Event, and returns a modified *zerolog.Event.
// This allows for custom logging behavior based on the request context.
//
// Parameters:
//
// fn - A function that takes a *gin.Context and a *zerolog.Event, and returns a modified *zerolog.Event.
//
// Returns:
//
// An Option that applies the custom context function to the logger configuration.
func WithContext(fn func(*gin.Context, *zerolog.Event) *zerolog.Event) Option {
return optionFunc(func(c *config) {
c.context = fn
Expand Down

0 comments on commit 62fff47

Please sign in to comment.