slogstrict
defines a stricter interface for slog.
Slog is a great logging library, but the *slog.Logger
contains a lot of methods. This makes it easy to use, but also easy to misuse. The slogstrict.Logger
in this package does this:
- Always require a context for log messages. This is very useful if you want to include things like
requestID
,traceID
orspanID
in our log messages. - Always log with
slog.Attr
. This prevents any mistakes when using...any
in theslog.Logger
methods.
type Logger interface {
With(attrs ...slog.Attr) Logger
WithGroup(name string) Logger
Debug(ctx context.Context, msg string, attrs ...slog.Attr)
Info(ctx context.Context, msg string, attrs ...slog.Attr)
Warn(ctx context.Context, msg string, attrs ...slog.Attr)
Error(ctx context.Context, msg string, err error, attrs ...slog.Attr)
// For custom levels
Log(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
}
The package includes a slogstrict.New()
function that creates a new logger from a slog.Handler
, similar to slog.New()
.
// Create a new [Logger] from a [slog.Handler]
func New(h slog.Handler) Logger {
return impl{slog.New(h)}
}
The package also includes a slogstrict.FromSlog()
function that creates a new logger from a *slog.Logger
.
// Create a new [Logger] from a [*slog.Logger]
func FromSlog(l *slog.Logger) Logger {
return logger{l}
}