2022-11-03 13:54:49 +00:00
|
|
|
// Package log is a context wrapper around slog.Logger
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-10-03 21:14:14 +00:00
|
|
|
"log/slog"
|
2022-11-03 13:54:49 +00:00
|
|
|
"os"
|
|
|
|
|
"runtime/debug"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
var _default = slog.New(NewPrettyHandler(NewLevelHandler(slog.LevelInfo, slog.NewTextHandler(os.Stderr, nil)))).With(slog.String("logger", "default"))
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2023-11-11 03:31:51 +00:00
|
|
|
func Init() {
|
2024-10-03 21:14:14 +00:00
|
|
|
slog.SetDefault(_default)
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type loggerKey struct{}
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
func from(ctx context.Context) *slog.Logger {
|
|
|
|
|
l, ok := ctx.Value(loggerKey{}).(*slog.Logger)
|
2022-11-03 13:54:49 +00:00
|
|
|
if !ok {
|
2024-10-03 21:14:14 +00:00
|
|
|
_default.WarnContext(ctx, "missing slog.Logger in context, see lib/log.With", slog.String("stack", string(debug.Stack())))
|
2022-11-03 13:54:49 +00:00
|
|
|
return _default
|
|
|
|
|
}
|
|
|
|
|
return l
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
func With(ctx context.Context, l *slog.Logger) context.Context {
|
2022-11-03 13:54:49 +00:00
|
|
|
return context.WithValue(ctx, loggerKey{}, l)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
func WithDefault(ctx context.Context) context.Context {
|
|
|
|
|
return context.WithValue(ctx, loggerKey{}, _default)
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Leveled(ctx context.Context, level slog.Level) context.Context {
|
2024-10-03 21:14:14 +00:00
|
|
|
logger := from(ctx)
|
|
|
|
|
handler := logger.Handler()
|
|
|
|
|
leveledHandler := NewLevelHandler(level, handler)
|
|
|
|
|
prettyHandler := NewPrettyHandler(leveledHandler)
|
|
|
|
|
return With(ctx, slog.New(prettyHandler))
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
func WithTB(ctx context.Context, tb testing.TB) context.Context {
|
|
|
|
|
writer := &tbWriter{tb: tb}
|
|
|
|
|
handler := slog.NewTextHandler(writer, nil)
|
|
|
|
|
logger := slog.New(handler)
|
|
|
|
|
return With(ctx, logger)
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
func Debug(ctx context.Context, msg string, attrs ...slog.Attr) {
|
|
|
|
|
from(ctx).LogAttrs(ctx, slog.LevelDebug, msg, attrs...)
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
func Info(ctx context.Context, msg string, attrs ...slog.Attr) {
|
|
|
|
|
from(ctx).LogAttrs(ctx, slog.LevelInfo, msg, attrs...)
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
func Warn(ctx context.Context, msg string, attrs ...slog.Attr) {
|
|
|
|
|
from(ctx).LogAttrs(ctx, slog.LevelWarn, msg, attrs...)
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:14:14 +00:00
|
|
|
func Error(ctx context.Context, msg string, attrs ...slog.Attr) {
|
|
|
|
|
from(ctx).LogAttrs(ctx, slog.LevelError, msg, attrs...)
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|