|
| 1 | +//go:build go1.21 |
| 2 | + |
| 3 | +package logging |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "log/slog" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +// validation that [slog.Logger] implements [LoggerWithLevelI] |
| 14 | +var _ LoggerWithLevelI = &slog.Logger{} |
| 15 | + |
| 16 | +var _ *LoggerWrapper = NewLoggerWrapper(&slog.Logger{}) |
| 17 | + |
| 18 | +func TestLoggerWrapper_slog(t *testing.T) { |
| 19 | + |
| 20 | + ctx := context.Background() |
| 21 | + |
| 22 | + t.Run("Debug", func(t *testing.T) { |
| 23 | + wrapped, buf := NewTestLogger(t) |
| 24 | + wrapped.Debug(ctx, "debug message", "foo", "bar") |
| 25 | + |
| 26 | + checkLog(t, buf, map[string]any{ |
| 27 | + "level": "DEBUG", |
| 28 | + "msg": "debug message", |
| 29 | + "foo": "bar", |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + t.Run("Info", func(t *testing.T) { |
| 34 | + wrapped, buf := NewTestLogger(t) |
| 35 | + wrapped.Info(ctx, "info message", "foo", "bar") |
| 36 | + |
| 37 | + checkLog(t, buf, map[string]any{ |
| 38 | + "level": "INFO", |
| 39 | + "msg": "info message", |
| 40 | + "foo": "bar", |
| 41 | + }) |
| 42 | + }) |
| 43 | + t.Run("Warn", func(t *testing.T) { |
| 44 | + wrapped, buf := NewTestLogger(t) |
| 45 | + wrapped.Warn(ctx, "warn message", "foo", "bar") |
| 46 | + |
| 47 | + checkLog(t, buf, map[string]any{ |
| 48 | + "level": "WARN", |
| 49 | + "msg": "warn message", |
| 50 | + "foo": "bar", |
| 51 | + }) |
| 52 | + }) |
| 53 | + t.Run("Error", func(t *testing.T) { |
| 54 | + wrapped, buf := NewTestLogger(t) |
| 55 | + wrapped.Error(ctx, "error message", "foo", "bar") |
| 56 | + |
| 57 | + checkLog(t, buf, map[string]any{ |
| 58 | + "level": "ERROR", |
| 59 | + "msg": "error message", |
| 60 | + "foo": "bar", |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("Errorf", func(t *testing.T) { |
| 65 | + wrapped, buf := NewTestLogger(t) |
| 66 | + wrapped.Errorf(ctx, "%d is the answer to %s", 42, "everything") |
| 67 | + |
| 68 | + checkLog(t, buf, map[string]any{ |
| 69 | + "level": "ERROR", |
| 70 | + "msg": "42 is the answer to everything", |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("Infof", func(t *testing.T) { |
| 75 | + wrapped, buf := NewTestLogger(t) |
| 76 | + wrapped.Infof(ctx, "%d is the answer to %s", 42, "everything") |
| 77 | + |
| 78 | + checkLog(t, buf, map[string]any{ |
| 79 | + "level": "INFO", |
| 80 | + "msg": "42 is the answer to everything", |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("Warnf", func(t *testing.T) { |
| 85 | + wrapped, buf := NewTestLogger(t) |
| 86 | + wrapped.Warnf(ctx, "%d is the answer to %s", 42, "everything") |
| 87 | + |
| 88 | + checkLog(t, buf, map[string]any{ |
| 89 | + "level": "WARN", |
| 90 | + "msg": "42 is the answer to everything", |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("Debugf", func(t *testing.T) { |
| 95 | + wrapped, buf := NewTestLogger(t) |
| 96 | + wrapped.Debugf(ctx, "%d is the answer to %s", 42, "everything") |
| 97 | + |
| 98 | + checkLog(t, buf, map[string]any{ |
| 99 | + "level": "DEBUG", |
| 100 | + "msg": "42 is the answer to everything", |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("Insufficient loglevel: default", func(t *testing.T) { |
| 105 | + wrapped, buf := NewTestLoggerWithLevel(t, nil) |
| 106 | + |
| 107 | + wrapped.Debug(ctx, "debug message", "foo", "bar") |
| 108 | + if buf.Len() != 0 { |
| 109 | + t.Errorf("expected no log message, got %s", buf.String()) |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("Insufficient loglevel: error", func(t *testing.T) { |
| 114 | + wrapped, buf := NewTestLoggerWithLevel(t, slog.LevelError) |
| 115 | + wrapped.Debug(ctx, "debug message", "foo", "bar") |
| 116 | + wrapped.Warn(ctx, "warn message", "foo", "bar") |
| 117 | + wrapped.Info(ctx, "info message", "foo", "bar") |
| 118 | + if buf.Len() != 0 { |
| 119 | + t.Errorf("expected no log message, got %s", buf.String()) |
| 120 | + } |
| 121 | + wrapped.Error(ctx, "error message", "foo", "bar") |
| 122 | + |
| 123 | + checkLog(t, buf, map[string]any{ |
| 124 | + "level": "ERROR", |
| 125 | + "msg": "error message", |
| 126 | + "foo": "bar", |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("Enabled loglevel: debug", func(t *testing.T) { |
| 131 | + |
| 132 | + wrapped, buf := NewTestLoggerWithLevel(t, slog.LevelInfo) |
| 133 | + if wrapped.Enabled(ctx, LogLevelDebug) { |
| 134 | + t.Errorf("expected debug level to be disabled") |
| 135 | + } |
| 136 | + if !wrapped.Enabled(ctx, LogLevelInfo) { |
| 137 | + t.Errorf("expected info level to be enabled") |
| 138 | + } |
| 139 | + if !wrapped.Enabled(ctx, LogLevelWarn) { |
| 140 | + t.Errorf("expected warn level to be enabled") |
| 141 | + } |
| 142 | + |
| 143 | + if !wrapped.Enabled(ctx, LogLevelError) { |
| 144 | + t.Errorf("expected error level to be enabled") |
| 145 | + } |
| 146 | + |
| 147 | + wrapped.Debug(ctx, "debug message", "foo", "bar") |
| 148 | + if buf.Len() != 0 { |
| 149 | + t.Errorf("expected no log message, got %s", buf.String()) |
| 150 | + } |
| 151 | + |
| 152 | + wrapped.Info(ctx, "info message", "foo", "bar") |
| 153 | + checkLog(t, buf, map[string]any{ |
| 154 | + "level": "INFO", |
| 155 | + "msg": "info message", |
| 156 | + "foo": "bar", |
| 157 | + }) |
| 158 | + }) |
| 159 | +} |
| 160 | + |
| 161 | +func NewTestLogger(t *testing.T) (*LoggerWrapper, *bytes.Buffer) { |
| 162 | + return NewTestLoggerWithLevel(t, slog.LevelDebug) |
| 163 | +} |
| 164 | + |
| 165 | +func NewTestLoggerWithLevel(t *testing.T, level slog.Leveler) (*LoggerWrapper, *bytes.Buffer) { |
| 166 | + var buf bytes.Buffer |
| 167 | + wrapped := NewLoggerWrapper(slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: level}))) |
| 168 | + return wrapped, &buf |
| 169 | +} |
| 170 | + |
| 171 | +func checkLog(t *testing.T, buf *bytes.Buffer, attrs map[string]any) { |
| 172 | + t.Helper() |
| 173 | + |
| 174 | + res := buf.Bytes() |
| 175 | + |
| 176 | + var m map[string]any |
| 177 | + err := json.Unmarshal(res, &m) |
| 178 | + if err != nil { |
| 179 | + t.Fatalf("failed to unmarshal log message: %v", err) |
| 180 | + } |
| 181 | + |
| 182 | + delete(m, "time") // remove time for testing purposes |
| 183 | + |
| 184 | + if len(m) != len(attrs) { |
| 185 | + t.Errorf("expected %d attributes, got %d", len(attrs), len(m)) |
| 186 | + } |
| 187 | + |
| 188 | + for k, expected := range attrs { |
| 189 | + v, ok := m[k] |
| 190 | + if !ok { |
| 191 | + t.Errorf("expected log to have key %s", k) |
| 192 | + continue |
| 193 | + } |
| 194 | + if v != expected { |
| 195 | + t.Errorf("expected %s to be %v, got %v", k, expected, v) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + if t.Failed() { |
| 200 | + t.Logf("log message: %s", res) |
| 201 | + t.Log("time is ignored in comparison") |
| 202 | + } |
| 203 | +} |
0 commit comments