MCPcopy
hub / github.com/tuna/tunasync / TestLineHandlerFormatsAttrsAndSource

Function TestLineHandlerFormatsAttrsAndSource

internal/logger_test.go:167–226  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

165}
166
167func TestLineHandlerFormatsAttrsAndSource(t *testing.T) {
168 var buf bytes.Buffer
169
170 handler := newLineHandler(&buf, slog.LevelDebug, true, false).
171 WithGroup("grp").
172 WithAttrs([]slog.Attr{
173 slog.String("prefix", "yes"),
174 {},
175 })
176
177 pc, _, _, ok := runtime.Caller(0)
178 if !ok {
179 t.Fatal("runtime.Caller failed")
180 }
181
182 record := slog.NewRecord(
183 time.Date(2024, time.January, 2, 3, 4, 5, 0, time.UTC),
184 LevelNotice,
185 "hello",
186 pc,
187 )
188 record.AddAttrs(
189 slog.String("name", "value"),
190 slog.Bool("ok", true),
191 slog.Int64("n", -3),
192 slog.Uint64("u", 4),
193 slog.Float64("f", 1.5),
194 slog.Duration("d", 2*time.Second),
195 slog.Time("when", time.Date(2024, time.January, 2, 3, 4, 5, 0, time.UTC)),
196 slog.Any("obj", struct{ X int }{X: 7}),
197 slog.Any("lazy", lazyString("resolved")),
198 )
199
200 if !handler.Enabled(context.Background(), LevelNotice) {
201 t.Fatal("handler should be enabled for notice")
202 }
203 if err := handler.Handle(context.Background(), record); err != nil {
204 t.Fatalf("Handle returned error: %v", err)
205 }
206
207 out := buf.String()
208 for _, want := range []string{
209 "[24-01-02 03:04:05][NOTICE][logger_test.go:",
210 " hello",
211 "grp.prefix=\"yes\"",
212 "grp.name=\"value\"",
213 "grp.ok=true",
214 "grp.n=-3",
215 "grp.u=4",
216 "grp.f=1.5",
217 "grp.d=2s",
218 "grp.when=2024-01-02T03:04:05Z",
219 "grp.obj={7}",
220 "grp.lazy=\"resolved\"",
221 } {
222 if !strings.Contains(out, want) {
223 t.Fatalf("output = %q, want substring %q", out, want)
224 }

Callers

nothing calls this directly

Calls 7

newLineHandlerFunction · 0.85
lazyStringTypeAlias · 0.85
WithAttrsMethod · 0.80
WithGroupMethod · 0.80
EnabledMethod · 0.80
HandleMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected