slogx provides enhanced extensions for slog, including handler, writetr, format, middleware, etc.
go get -u golang.org/x/exp/slog
import "github.com/three-body/slogx"
Compatibility: go >= 1.20.3
This library is v0 and follows SemVer strictly. On slog final release (go 1.21), this library will go v1.
No breaking changes will be made to exported APIs before v1.0.0.
writer, err := slogx.NewFileWriter()
if err != nil {
panic(err)
}
h := slogx.HandlerOptions{
Level: slog.LevelError,
}.NewHandler(writer)
logger := slog.New(h)
slog.SetDefault(logger)
slog.Info("hello world")
errHandler := slogx.HandlerOptions{
Level: slog.LevelError,
}.NewHandler(os.Stderr)
infoHandler := slogx.HandlerOptions{
Level: slog.LevelInfo,
}.NewHandler(os.Stdout)
h := slogx.NewMultiHandler(errHandler, infoHandler)
logger := slog.New(h)
slog.SetDefault(logger)
slog.Info("hello world")
Writer is a io.Writer that can write log to a file or a stream.
writer, err := slogx.FileWriterOptions{
Path: "./logs",
FileName: "app.log",
MaxTime: 0,
MaxCount: 0,
RotateTimeLayout: slogx.RotateTimeLayoutEveryHour,
RotateSize: 100,
Compress: false,
}.NewFileWriter()
if err != nil {
panic(err)
}
logger := slog.New(slog.NewJSONHandler(writer))
slog.SetDefault(logger)
slog.Info("hello world")
Formatter is a function that can format log entry to a string.
Copyright 2023 three-body.
This project is Apache-2.0 licensed.git