slog experimental features such individual log files for different levels, and other extensions.
See also:
- slog-multi:
slog.Handler
chaining, fanout, routing, failover, load balancing... - slog-formatter:
slog
attribute formatting - slog-sampling:
slog
sampling policy - 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-datadog: A
slog
handler forDatadog
- slog-rollbar: A
slog
handler forRollbar
- slog-sentry: A
slog
handler forSentry
- slog-syslog: A
slog
handler forSyslog
- slog-logstash: A
slog
handler forLogstash
- slog-fluentd: A
slog
handler forFluentd
- slog-graylog: A
slog
handler forGraylog
- slog-loki: A
slog
handler forLoki
- slog-slack: A
slog
handler forSlack
- slog-telegram: A
slog
handler forTelegram
- slog-mattermost: A
slog
handler forMattermost
- slog-microsoft-teams: A
slog
handler forMicrosoft Teams
- slog-webhook: A
slog
handler forWebhook
- slog-kafka: A
slog
handler forKafka
- slog-nats: A
slog
handler forNATS
- slog-parquet: A
slog
handler forParquet
+Object Storage
- slog-zap: A
slog
handler forZap
- slog-zerolog: A
slog
handler forZerolog
- slog-logrus: A
slog
handler forLogrus
- slog-channel: A
slog
handler for Go channels - slog-clickhouse: A
slog
handler for ClickHouse
go get github.com/smllnest/slog-clickhouse
Compatibility: go >= 1.21
Set different handlers for different log levels.
For example, you can set different handlers to save files for different log levels, such as info
, warn
, and error
.
infoFile, err := os.OpenFile("testdata/info.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
defer infoFile.Close()
warnFile, err := os.OpenFile("testdata/warn.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
defer warnFile.Close()
errorFile, err := os.OpenFile("testdata/error.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
defer errorFile.Close()
infoHandler := slog.NewTextHandler(infoFile, &slog.HandlerOptions{Level: slog.LevelInfo})
warnHandler := slog.NewTextHandler(warnFile, &slog.HandlerOptions{Level: slog.LevelWarn})
errorHandler := slog.NewTextHandler(errorFile, &slog.HandlerOptions{Level: slog.LevelError})
handler := NewLevelHandler(map[slog.Level]slog.Handler{
slog.LevelInfo: infoHandler,
slog.LevelWarn: warnHandler,
slog.LevelError: errorHandler,
})
logger := slog.New(handler)
logger.Info("info text")
logger.Warn("warn text")
logger.Error("error text")
use ReplaceTimeAttr
and ReplaceSourceAttr
.
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: WrapReplaceAttrFunc(ReplaceTimeAttr(time.TimeOnly), ReplaceSourceAttr()),
AddSource: true,
})
logger := slog.New(handler)
logger.Info("info text")
logger.Warn("warn text")
logger.Error("error text")
handler := NewLevelHandler(map[slog.Level]slog.Handler{
slog.LevelInfo: infoHandler,
slog.LevelWarn: warnHandler,
slog.LevelError: errorHandler,
}).WithAttrs([]slog.Attr{
slog.Int64("gid", gid.ID()),
})