百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

slog-gin

> 数据库
开源

用于 slog 日志记录器的 Gin 中间件

188 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 slog 日志记录器的 Gin 中间件

slog: Gin middleware

Gin middleware to log http requests using slog.

See also:

  • slog-multi: slog.Handler chaining, fanout, routing, failover, load balancing...
  • slog-formatter: slog attribute formatting
  • slog-sampling: slog sampling policy
  • slog-mock: slog.Handler for test purposes

HTTP middlewares:

  • slog-gin: Gin middleware for slog logger
  • slog-echo: Echo middleware for slog logger
  • slog-fiber: Fiber middleware for slog logger
  • slog-chi: Chi middleware for slog logger
  • slog-http: net/http middleware for slog logger

Loggers:

  • slog-zap: A slog handler for Zap
  • slog-zerolog: A slog handler for Zerolog
  • slog-logrus: A slog handler for Logrus

Log sinks:

  • slog-datadog: A slog handler for Datadog
  • slog-betterstack: A slog handler for Betterstack
  • slog-rollbar: A slog handler for Rollbar
  • slog-loki: A slog handler for Loki
  • slog-sentry: A slog handler for Sentry
  • slog-syslog: A slog handler for Syslog
  • slog-logstash: A slog handler for Logstash
  • slog-fluentd: A slog handler for Fluentd
  • slog-graylog: A slog handler for Graylog
  • slog-quickwit: A slog handler for Quickwit
  • slog-slack: A slog handler for Slack
  • slog-telegram: A slog handler for Telegram
  • slog-mattermost: A slog handler for Mattermost
  • slog-microsoft-teams: A slog handler for Microsoft Teams
  • slog-webhook: A slog handler for Webhook
  • slog-kafka: A slog handler for Kafka
  • slog-nats: A slog handler for NATS
  • slog-parquet: A slog handler for Parquet + Object Storage
  • slog-channel: A slog handler for Go channels

Install

bash
go get github.com/samber/slog-gin

Compatibility: go >= 1.21

No breaking changes will be made to exported APIs before v2.0.0.

Usage

Handler options

go
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:

go
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"

Minimal

…

OTEL

go
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

config := sloggin.Config{
	WithSpanID:  true,
	WithTraceID: true,
}

router := gin.New()
router.Use(sloggin.NewWithConfig(logger, config))

Custom log levels

go
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))

Verbose

go
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))

Filters

go
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:

  • Accept / Ignore
  • AcceptMethod / IgnoreMethod
  • AcceptStatus / IgnoreStatus
  • AcceptStatusGreaterThan / IgnoreStatusGreaterThan
  • AcceptStatusLessThan / IgnoreStatusLessThan
  • AcceptStatusGreaterThanOrEqual / IgnoreStatusGreaterThanOrEqual
  • AcceptStatusLessThanOrEqual / IgnoreStatusLessThanOrEqual
  • AcceptPath / IgnorePath
  • AcceptPathContains / IgnorePathContains
  • AcceptPathPrefix / IgnorePathPrefix
  • AcceptPathSuffix / IgnorePathSuffix
  • AcceptPathMatch / IgnorePathMatch
  • AcceptHost / IgnoreHost
  • AcceptHostContains / IgnoreHostContains
  • AcceptHostPrefix / IgnoreHostPrefix
  • AcceptHostSuffix / IgnoreHostSuffix
  • AcceptHostMatch / IgnoreHostMatch

Using custom time formatters

…

Using custom logger sub-group

…

Add logger to a single route

go
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")

Adding custom attributes

…

JSON output

…

Contributing

  • Ping me on twitter @samuelberthe (DMs, mentions, whatever :))
  • Fork the project
  • Fix open issues or request new features

Don't hesitate ;)

bash
# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

Contributors

Show your support

Give a ⭐️ if this project helped you!

License

Copyright © 2023 Samuel Berthe.

This project is MIT licensed.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Goerrorformattergingin-gonic

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库