|
| 1 | +package logger |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "os" |
| 8 | + "runtime" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type slogLogEntry struct { |
| 13 | + base *slogLogger |
| 14 | + fields Fields |
| 15 | +} |
| 16 | + |
| 17 | +type slogLogger struct { |
| 18 | + logger *slog.Logger |
| 19 | +} |
| 20 | + |
| 21 | +// NewSlogLogger erstellt einen neuen Logger mit slog Logger |
| 22 | +func NewSlogLogger(logger *slog.Logger) (Logger, error) { |
| 23 | + return &slogLogger{ |
| 24 | + logger: logger, |
| 25 | + }, nil |
| 26 | +} |
| 27 | + |
| 28 | +func (l *slogLogger) doLog(level slog.Level, msg string, attrs ...slog.Attr) { |
| 29 | + if !l.logger.Enabled(context.Background(), level) { |
| 30 | + return |
| 31 | + } |
| 32 | + var pcs [1]uintptr |
| 33 | + runtime.Callers(2, pcs[:]) // skip [Callers, log] |
| 34 | + r := slog.NewRecord(time.Now(), level, msg, pcs[0]) |
| 35 | + if len(attrs) > 0 { |
| 36 | + r.AddAttrs(attrs...) |
| 37 | + } |
| 38 | + _ = l.logger.Handler().Handle(context.Background(), r) |
| 39 | +} |
| 40 | + |
| 41 | +// Debug uses fmt.Sprint to construct and log a message. |
| 42 | +func (l *slogLogger) Debug(args ...any) { |
| 43 | + l.doLog(slog.LevelDebug, fmt.Sprint(args...)) |
| 44 | +} |
| 45 | + |
| 46 | +// Info uses fmt.Sprint to construct and log a message. |
| 47 | +func (l *slogLogger) Info(args ...any) { |
| 48 | + l.doLog(slog.LevelInfo, fmt.Sprint(args...)) |
| 49 | +} |
| 50 | + |
| 51 | +// Warn uses fmt.Sprint to construct and log a message. |
| 52 | +func (l *slogLogger) Warn(args ...any) { |
| 53 | + l.doLog(slog.LevelWarn, fmt.Sprint(args...)) |
| 54 | +} |
| 55 | + |
| 56 | +// Error uses fmt.Sprint to construct and log a message. |
| 57 | +func (l *slogLogger) Error(args ...any) { |
| 58 | + l.doLog(slog.LevelError, fmt.Sprint(args...)) |
| 59 | +} |
| 60 | + |
| 61 | +// Panic uses fmt.Sprint to construct and log a message, then panics. |
| 62 | +func (l *slogLogger) Panic(args ...any) { |
| 63 | + msg := fmt.Sprint(args...) |
| 64 | + l.doLog(slog.LevelError, msg) |
| 65 | + panic(msg) |
| 66 | +} |
| 67 | + |
| 68 | +// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit. |
| 69 | +func (l *slogLogger) Fatal(args ...any) { |
| 70 | + l.doLog(slog.LevelError, fmt.Sprint(args...)) |
| 71 | + os.Exit(1) |
| 72 | +} |
| 73 | + |
| 74 | +// Debugf uses fmt.Sprintf to log a templated message. |
| 75 | +func (l *slogLogger) Debugf(template string, args ...any) { |
| 76 | + l.doLog(slog.LevelDebug, fmt.Sprintf(template, args...)) |
| 77 | +} |
| 78 | + |
| 79 | +func (l *slogLogger) Infof(template string, args ...any) { |
| 80 | + l.doLog(slog.LevelInfo, fmt.Sprintf(template, args...)) |
| 81 | +} |
| 82 | + |
| 83 | +// Warnf uses fmt.Sprintf to log a templated message. |
| 84 | +func (l *slogLogger) Warnf(template string, args ...any) { |
| 85 | + l.doLog(slog.LevelWarn, fmt.Sprintf(template, args...)) |
| 86 | +} |
| 87 | + |
| 88 | +// Errorf uses fmt.Sprintf to log a templated message. |
| 89 | +func (l *slogLogger) Errorf(template string, args ...any) { |
| 90 | + l.doLog(slog.LevelError, fmt.Sprintf(template, args...)) |
| 91 | +} |
| 92 | + |
| 93 | +// Panicf uses fmt.Sprintf to log a templated message, then panics. |
| 94 | +func (l *slogLogger) Panicf(template string, args ...any) { |
| 95 | + msg := fmt.Sprintf(template, args...) |
| 96 | + l.doLog(slog.LevelError, msg) |
| 97 | + panic(msg) |
| 98 | +} |
| 99 | + |
| 100 | +// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit. |
| 101 | +func (l *slogLogger) Fatalf(template string, args ...any) { |
| 102 | + l.doLog(slog.LevelError, fmt.Sprintf(template, args...)) |
| 103 | + os.Exit(1) |
| 104 | +} |
| 105 | + |
| 106 | +// Adds a struct of fields to the log entry. All it does is call `WithField` for |
| 107 | +// each `Field`. |
| 108 | +func (l *slogLogger) WithFields(fields Fields) Logger { |
| 109 | + return &slogLogEntry{ |
| 110 | + base: l, |
| 111 | + fields: fields, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (l *slogLogEntry) doLogWithFields(level slog.Level, msg string) { |
| 116 | + var attrs []slog.Attr |
| 117 | + for k, v := range l.fields { |
| 118 | + attrs = append(attrs, slog.Attr{Key: k, Value: slog.AnyValue(v)}) |
| 119 | + } |
| 120 | + l.base.doLog(level, msg, attrs...) |
| 121 | +} |
| 122 | + |
| 123 | +// Debug uses fmt.Sprint to construct and log a message. |
| 124 | +func (l *slogLogEntry) Debug(args ...any) { |
| 125 | + l.doLogWithFields(slog.LevelDebug, fmt.Sprint(args...)) |
| 126 | +} |
| 127 | + |
| 128 | +// Info uses fmt.Sprint to construct and log a message. |
| 129 | +func (l *slogLogEntry) Info(args ...any) { |
| 130 | + l.doLogWithFields(slog.LevelInfo, fmt.Sprint(args...)) |
| 131 | +} |
| 132 | + |
| 133 | +// Warn uses fmt.Sprint to construct and log a message. |
| 134 | +func (l *slogLogEntry) Warn(args ...any) { |
| 135 | + l.doLogWithFields(slog.LevelWarn, fmt.Sprint(args...)) |
| 136 | +} |
| 137 | + |
| 138 | +// Error uses fmt.Sprint to construct and log a message. |
| 139 | +func (l *slogLogEntry) Error(args ...any) { |
| 140 | + l.doLogWithFields(slog.LevelError, fmt.Sprint(args...)) |
| 141 | +} |
| 142 | + |
| 143 | +// Panic uses fmt.Sprint to construct and log a message, then panics. |
| 144 | +func (l *slogLogEntry) Panic(args ...any) { |
| 145 | + msg := fmt.Sprint(args...) |
| 146 | + l.doLogWithFields(slog.LevelError, msg) |
| 147 | + panic(msg) |
| 148 | +} |
| 149 | + |
| 150 | +// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit. |
| 151 | +func (l *slogLogEntry) Fatal(args ...any) { |
| 152 | + l.doLogWithFields(slog.LevelError, fmt.Sprint(args...)) |
| 153 | + os.Exit(1) |
| 154 | +} |
| 155 | + |
| 156 | +// Debugf uses fmt.Sprintf to log a templated message. |
| 157 | +func (l *slogLogEntry) Debugf(template string, args ...any) { |
| 158 | + l.doLogWithFields(slog.LevelDebug, fmt.Sprintf(template, args...)) |
| 159 | +} |
| 160 | + |
| 161 | +// Infof uses fmt.Sprintf to log a templated message. |
| 162 | +func (l *slogLogEntry) Infof(template string, args ...any) { |
| 163 | + l.doLogWithFields(slog.LevelInfo, fmt.Sprintf(template, args...)) |
| 164 | +} |
| 165 | + |
| 166 | +// Warnf uses fmt.Sprintf to log a templated message. |
| 167 | +func (l *slogLogEntry) Warnf(template string, args ...any) { |
| 168 | + l.doLogWithFields(slog.LevelWarn, fmt.Sprintf(template, args...)) |
| 169 | +} |
| 170 | + |
| 171 | +// Errorf uses fmt.Sprintf to log a templated message. |
| 172 | +func (l *slogLogEntry) Errorf(template string, args ...any) { |
| 173 | + l.doLogWithFields(slog.LevelError, fmt.Sprintf(template, args...)) |
| 174 | +} |
| 175 | + |
| 176 | +// Panicf uses fmt.Sprintf to log a templated message, then panics. |
| 177 | +func (l *slogLogEntry) Panicf(template string, args ...any) { |
| 178 | + msg := fmt.Sprintf(template, args...) |
| 179 | + l.doLogWithFields(slog.LevelError, msg) |
| 180 | + panic(msg) |
| 181 | +} |
| 182 | + |
| 183 | +// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit. |
| 184 | +func (l *slogLogEntry) Fatalf(template string, args ...any) { |
| 185 | + l.doLogWithFields(slog.LevelError, fmt.Sprintf(template, args...)) |
| 186 | +} |
| 187 | + |
| 188 | +// WithFields adds fields to the logging context |
| 189 | +func (l *slogLogEntry) WithFields(fields Fields) Logger { |
| 190 | + var allFields = make(Fields, len(l.fields)+len(fields)) |
| 191 | + for k, v := range fields { |
| 192 | + allFields[k] = v |
| 193 | + } |
| 194 | + for k, v := range l.fields { |
| 195 | + allFields[k] = v |
| 196 | + } |
| 197 | + return l.base.WithFields(allFields) |
| 198 | +} |
0 commit comments