2023-09-16 14:19:21 +03:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-14 18:59:27 +03:00
|
|
|
"log/slog"
|
2023-10-14 16:58:40 +03:00
|
|
|
"os"
|
2023-10-14 18:59:27 +03:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2023-10-14 16:58:40 +03:00
|
|
|
|
2023-09-16 14:19:21 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
)
|
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
var (
|
|
|
|
traceValue = slog.StringValue("trace")
|
|
|
|
debugValue = slog.StringValue("debug")
|
|
|
|
infoValue = slog.StringValue("info")
|
|
|
|
warnValue = slog.StringValue("warn")
|
|
|
|
errorValue = slog.StringValue("error")
|
|
|
|
fatalValue = slog.StringValue("fatal")
|
2023-09-16 14:19:21 +03:00
|
|
|
)
|
|
|
|
|
2023-10-17 01:05:55 +03:00
|
|
|
func (s *slogLogger) renameAttr(_ []string, a slog.Attr) slog.Attr {
|
2023-10-14 16:58:40 +03:00
|
|
|
switch a.Key {
|
2023-10-14 18:59:27 +03:00
|
|
|
case slog.SourceKey:
|
|
|
|
source := a.Value.Any().(*slog.Source)
|
|
|
|
a.Value = slog.StringValue(source.File + ":" + strconv.Itoa(source.Line))
|
2023-10-17 01:05:55 +03:00
|
|
|
a.Key = s.opts.SourceKey
|
2023-10-14 16:58:40 +03:00
|
|
|
case slog.TimeKey:
|
2023-10-17 01:05:55 +03:00
|
|
|
a.Key = s.opts.TimeKey
|
|
|
|
case slog.MessageKey:
|
|
|
|
a.Key = s.opts.MessageKey
|
2023-10-14 16:58:40 +03:00
|
|
|
case slog.LevelKey:
|
|
|
|
level := a.Value.Any().(slog.Level)
|
|
|
|
lvl := slogToLoggerLevel(level)
|
2023-10-17 01:05:55 +03:00
|
|
|
a.Key = s.opts.LevelKey
|
2023-10-14 16:58:40 +03:00
|
|
|
switch {
|
|
|
|
case lvl < DebugLevel:
|
|
|
|
a.Value = traceValue
|
|
|
|
case lvl < InfoLevel:
|
|
|
|
a.Value = debugValue
|
|
|
|
case lvl < WarnLevel:
|
|
|
|
a.Value = infoValue
|
|
|
|
case lvl < ErrorLevel:
|
|
|
|
a.Value = warnValue
|
|
|
|
case lvl < FatalLevel:
|
|
|
|
a.Value = errorValue
|
|
|
|
case lvl >= FatalLevel:
|
|
|
|
a.Value = fatalValue
|
|
|
|
default:
|
|
|
|
a.Value = infoValue
|
|
|
|
}
|
|
|
|
}
|
2023-09-16 14:19:21 +03:00
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
return a
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
type slogLogger struct {
|
|
|
|
slog *slog.Logger
|
|
|
|
leveler *slog.LevelVar
|
|
|
|
opts Options
|
|
|
|
}
|
2023-09-16 14:19:21 +03:00
|
|
|
|
|
|
|
func (s *slogLogger) Clone(opts ...options.Option) Logger {
|
2023-10-14 16:58:40 +03:00
|
|
|
options := s.opts
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &slogLogger{
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
|
|
|
|
if slog, ok := s.opts.Context.Value(loggerKey{}).(*slog.Logger); ok {
|
|
|
|
l.slog = slog
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
l.leveler = new(slog.LevelVar)
|
|
|
|
handleOpt := &slog.HandlerOptions{
|
2023-10-17 01:05:55 +03:00
|
|
|
ReplaceAttr: s.renameAttr,
|
2023-10-14 16:58:40 +03:00
|
|
|
Level: l.leveler,
|
2023-10-14 18:59:27 +03:00
|
|
|
AddSource: true,
|
2023-10-14 16:58:40 +03:00
|
|
|
}
|
|
|
|
l.leveler.Set(loggerToSlogLevel(l.opts.Level))
|
|
|
|
handler := slog.NewJSONHandler(options.Out, handleOpt)
|
2023-10-16 23:43:39 +03:00
|
|
|
l.slog = slog.New(handler).With(options.Attrs...)
|
2023-10-14 16:58:40 +03:00
|
|
|
|
|
|
|
return l
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slogLogger) V(level Level) bool {
|
2023-10-14 16:58:40 +03:00
|
|
|
return s.opts.Level.Enabled(level)
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slogLogger) Level(level Level) {
|
2023-10-14 16:58:40 +03:00
|
|
|
s.leveler.Set(loggerToSlogLevel(level))
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slogLogger) Options() Options {
|
2023-10-14 16:58:40 +03:00
|
|
|
return s.opts
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *slogLogger) Attrs(attrs ...interface{}) Logger {
|
2023-10-14 16:58:40 +03:00
|
|
|
nl := &slogLogger{opts: s.opts}
|
|
|
|
nl.leveler = new(slog.LevelVar)
|
|
|
|
nl.leveler.Set(s.leveler.Level())
|
2023-09-16 14:19:21 +03:00
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
handleOpt := &slog.HandlerOptions{
|
2023-10-17 01:05:55 +03:00
|
|
|
ReplaceAttr: nl.renameAttr,
|
2023-10-14 16:58:40 +03:00
|
|
|
Level: s.leveler,
|
2023-10-14 18:59:27 +03:00
|
|
|
AddSource: true,
|
2023-10-14 16:58:40 +03:00
|
|
|
}
|
2023-09-16 14:19:21 +03:00
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
handler := slog.NewJSONHandler(s.opts.Out, handleOpt)
|
2023-10-16 23:43:39 +03:00
|
|
|
nl.slog = slog.New(handler).With(attrs...)
|
2023-09-16 14:19:21 +03:00
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
return nl
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slogLogger) Init(opts ...options.Option) error {
|
2023-10-17 01:00:00 +03:00
|
|
|
if len(s.opts.ContextAttrFuncs) == 0 {
|
|
|
|
s.opts.ContextAttrFuncs = DefaultContextAttrFuncs
|
|
|
|
}
|
2023-09-16 14:19:21 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
if err := o(&s.opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if slog, ok := s.opts.Context.Value(loggerKey{}).(*slog.Logger); ok {
|
|
|
|
s.slog = slog
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
s.leveler = new(slog.LevelVar)
|
2023-09-16 14:19:21 +03:00
|
|
|
handleOpt := &slog.HandlerOptions{
|
2023-10-17 01:05:55 +03:00
|
|
|
ReplaceAttr: s.renameAttr,
|
2023-10-14 16:58:40 +03:00
|
|
|
Level: s.leveler,
|
2023-10-14 18:59:27 +03:00
|
|
|
AddSource: true,
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
2023-10-14 16:58:40 +03:00
|
|
|
s.leveler.Set(loggerToSlogLevel(s.opts.Level))
|
|
|
|
handler := slog.NewJSONHandler(s.opts.Out, handleOpt)
|
2023-10-16 23:43:39 +03:00
|
|
|
s.slog = slog.New(handler).With(s.opts.Attrs...)
|
2023-09-16 14:19:21 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *slogLogger) Log(ctx context.Context, lvl Level, msg string, attrs ...interface{}) {
|
2023-10-14 16:58:40 +03:00
|
|
|
if !s.V(lvl) {
|
|
|
|
return
|
|
|
|
}
|
2023-10-14 18:59:27 +03:00
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
|
|
|
r := slog.NewRecord(time.Now(), loggerToSlogLevel(lvl), msg, pcs[0])
|
2023-10-16 23:43:39 +03:00
|
|
|
for _, fn := range s.opts.ContextAttrFuncs {
|
2023-10-17 01:00:00 +03:00
|
|
|
attrs = append(attrs, fn(ctx)...)
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
r.Add(attrs...)
|
2023-10-14 18:59:27 +03:00
|
|
|
_ = s.slog.Handler().Handle(ctx, r)
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *slogLogger) Info(ctx context.Context, msg string, attrs ...interface{}) {
|
2023-10-14 18:59:27 +03:00
|
|
|
if !s.V(InfoLevel) {
|
2023-10-14 16:58:40 +03:00
|
|
|
return
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
2023-10-14 18:59:27 +03:00
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
|
|
|
r := slog.NewRecord(time.Now(), slog.LevelInfo, msg, pcs[0])
|
2023-10-16 23:43:39 +03:00
|
|
|
for _, fn := range s.opts.ContextAttrFuncs {
|
2023-10-17 01:00:00 +03:00
|
|
|
attrs = append(attrs, fn(ctx)...)
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
r.Add(attrs...)
|
2023-10-14 18:59:27 +03:00
|
|
|
_ = s.slog.Handler().Handle(ctx, r)
|
2023-10-14 16:58:40 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *slogLogger) Debug(ctx context.Context, msg string, attrs ...interface{}) {
|
2023-10-14 18:59:27 +03:00
|
|
|
if !s.V(InfoLevel) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
|
|
|
r := slog.NewRecord(time.Now(), slog.LevelDebug, msg, pcs[0])
|
2023-10-16 23:43:39 +03:00
|
|
|
for _, fn := range s.opts.ContextAttrFuncs {
|
2023-10-17 01:00:00 +03:00
|
|
|
attrs = append(attrs, fn(ctx)...)
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
r.Add(attrs...)
|
2023-10-14 18:59:27 +03:00
|
|
|
_ = s.slog.Handler().Handle(ctx, r)
|
2023-10-14 16:58:40 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *slogLogger) Trace(ctx context.Context, msg string, attrs ...interface{}) {
|
2023-10-14 18:59:27 +03:00
|
|
|
if !s.V(InfoLevel) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
|
|
|
r := slog.NewRecord(time.Now(), slog.LevelDebug-1, msg, pcs[0])
|
2023-10-16 23:43:39 +03:00
|
|
|
for _, fn := range s.opts.ContextAttrFuncs {
|
2023-10-17 01:00:00 +03:00
|
|
|
attrs = append(attrs, fn(ctx)...)
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
r.Add(attrs...)
|
2023-10-14 18:59:27 +03:00
|
|
|
_ = s.slog.Handler().Handle(ctx, r)
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *slogLogger) Error(ctx context.Context, msg string, attrs ...interface{}) {
|
2023-10-14 18:59:27 +03:00
|
|
|
if !s.V(InfoLevel) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
|
|
|
r := slog.NewRecord(time.Now(), slog.LevelError, msg, pcs[0])
|
2023-10-16 23:43:39 +03:00
|
|
|
for _, fn := range s.opts.ContextAttrFuncs {
|
2023-10-17 01:00:00 +03:00
|
|
|
attrs = append(attrs, fn(ctx)...)
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
r.Add(attrs...)
|
2023-10-14 18:59:27 +03:00
|
|
|
_ = s.slog.Handler().Handle(ctx, r)
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *slogLogger) Fatal(ctx context.Context, msg string, attrs ...interface{}) {
|
2023-10-14 18:59:27 +03:00
|
|
|
if !s.V(InfoLevel) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
|
|
|
r := slog.NewRecord(time.Now(), slog.LevelError+1, msg, pcs[0])
|
2023-10-16 23:43:39 +03:00
|
|
|
for _, fn := range s.opts.ContextAttrFuncs {
|
2023-10-17 01:00:00 +03:00
|
|
|
attrs = append(attrs, fn(ctx)...)
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
r.Add(attrs...)
|
2023-10-14 18:59:27 +03:00
|
|
|
_ = s.slog.Handler().Handle(ctx, r)
|
2023-10-14 16:58:40 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *slogLogger) Warn(ctx context.Context, msg string, attrs ...interface{}) {
|
2023-10-14 18:59:27 +03:00
|
|
|
if !s.V(InfoLevel) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
|
|
|
r := slog.NewRecord(time.Now(), slog.LevelWarn, msg, pcs[0])
|
2023-10-16 23:43:39 +03:00
|
|
|
for _, fn := range s.opts.ContextAttrFuncs {
|
2023-10-17 01:00:00 +03:00
|
|
|
attrs = append(attrs, fn(ctx)...)
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
r.Add(attrs...)
|
2023-10-14 18:59:27 +03:00
|
|
|
_ = s.slog.Handler().Handle(ctx, r)
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slogLogger) String() string {
|
2023-10-14 16:58:40 +03:00
|
|
|
return "slog"
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
func NewLogger(opts ...options.Option) Logger {
|
2023-09-16 14:19:21 +03:00
|
|
|
l := &slogLogger{
|
|
|
|
opts: NewOptions(opts...),
|
|
|
|
}
|
2023-10-14 16:58:40 +03:00
|
|
|
return l
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func loggerToSlogLevel(level Level) slog.Level {
|
|
|
|
switch level {
|
2023-10-14 16:58:40 +03:00
|
|
|
case DebugLevel:
|
2023-09-16 14:19:21 +03:00
|
|
|
return slog.LevelDebug
|
|
|
|
case WarnLevel:
|
|
|
|
return slog.LevelWarn
|
2023-10-14 16:58:40 +03:00
|
|
|
case ErrorLevel:
|
2023-09-16 14:19:21 +03:00
|
|
|
return slog.LevelError
|
2023-10-14 16:58:40 +03:00
|
|
|
case TraceLevel:
|
|
|
|
return slog.LevelDebug - 1
|
|
|
|
case FatalLevel:
|
|
|
|
return slog.LevelError + 1
|
2023-09-16 14:19:21 +03:00
|
|
|
default:
|
|
|
|
return slog.LevelInfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 16:58:40 +03:00
|
|
|
func slogToLoggerLevel(level slog.Level) Level {
|
|
|
|
switch level {
|
|
|
|
case slog.LevelDebug:
|
|
|
|
return DebugLevel
|
|
|
|
case slog.LevelWarn:
|
|
|
|
return WarnLevel
|
|
|
|
case slog.LevelError:
|
|
|
|
return ErrorLevel
|
|
|
|
case slog.LevelDebug - 1:
|
|
|
|
return TraceLevel
|
|
|
|
case slog.LevelError + 1:
|
|
|
|
return FatalLevel
|
|
|
|
default:
|
|
|
|
return InfoLevel
|
2023-09-16 14:19:21 +03:00
|
|
|
}
|
|
|
|
}
|