Gin middleware for slog logger
Gin middleware to log http requests using slog.
See also:
slog.Handler chaining, fanout, routing, failover, load balancing...slog attribute formattingslog sampling policyslog.Handler for test purposesHTTP middlewares:
slog loggerslog loggerslog loggerslog loggernet/http middleware for slog loggerLoggers:
slog handler for Zapslog handler for Zerologslog handler for LogrusLog sinks:
slog handler for Datadogslog handler for Betterstackslog handler for Rollbarslog handler for Lokislog handler for Sentryslog handler for Syslogslog handler for Logstashslog handler for Fluentdslog handler for Graylogslog handler for Quickwitslog handler for Slackslog handler for Telegramslog handler for Mattermostslog handler for Microsoft Teamsslog handler for Webhookslog handler for Kafkaslog handler for NATSslog handler for Parquet + Object Storageslog handler for Go channelsgo get github.com/samber/slog-ginCompatibility: go >= 1.21
No breaking changes will be made to exported APIs before v2.0.0.
type Config struct {
DefaultLevel slog.Level
ClientErrorLevel slog.Level
ServerErrorLevel slog.Level
WithUserAgent bool
WithRequestID bool
WithRequestBody bool
WithRequestHeader bool
WithResponseBody bool
WithResponseHeader bool
WithSpanID bool
WithTraceID bool
WithClientIP bool
WithCustomMessage func(c *gin.Context) string
HandleGinDebug bool
Filters []Filter
}Attributes will be injected in log payload.
Other global parameters:
sloggin.TraceIDKey = "trace_id"
sloggin.SpanIDKey = "span_id"
sloggin.RequestBodyMaxSize = 64 * 1024 // 64KB
sloggin.ResponseBodyMaxSize = 64 * 1024 // 64KB
sloggin.HiddenRequestHeaders = map[string]struct{}{ ... }
sloggin.HiddenResponseHeaders = map[string]struct{}{ ... }
sloggin.RequestIDHeaderKey = "X-Request-Id"
sloggin.RequestIDContextKey = "slog-gin.request-id"…logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
config := sloggin.Config{
WithSpanID: true,
WithTraceID: true,
}
router := gin.New()
router.Use(sloggin.NewWithConfig(logger, config))logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
config := sloggin.Config{
DefaultLevel: slog.LevelInfo,
ClientErrorLevel: slog.LevelWarn,
ServerErrorLevel: slog.LevelError,
}
router := gin.New()
router.Use(sloggin.NewWithConfig(logger, config))logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
config := sloggin.Config{
WithRequestBody: true,
WithResponseBody: true,
WithRequestHeader: true,
WithResponseHeader: true,
}
router := gin.New()
router.Use(sloggin.NewWithConfig(logger, config))logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
router := gin.New()
router.Use(
sloggin.NewWithFilters(
logger,
sloggin.Accept(func (c *gin.Context) bool {
return xxx
}),
sloggin.IgnoreStatus(401, 404),
),
)Available filters:
……logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
router := gin.New()
router.Use(gin.Recovery())
// Example pong request.
// Add the sloggin middleware to a single routes.
router.GET("/pong", sloggin.New(logger), func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
router.Run(":1234")……Don't hesitate ;)
# Install some dev dependencies
make tools
# Run tests
make test
# or
make watch-testGive a ⭐️ if this project helped you!
Copyright © 2023 Samuel Berthe.
This project is MIT licensed.
No open issues yet, or sync has not completed.