diff --git a/README.md b/README.md index acefa6c..98dbf9f 100644 --- a/README.md +++ b/README.md @@ -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 { diff --git a/options.go b/options.go index eec355f..c88ff4c 100644 --- a/options.go +++ b/options.go @@ -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