(t *testing.T)
| 253 | } |
| 254 | |
| 255 | func TestBatchHandlerAttrsFormat(t *testing.T) { |
| 256 | ctx := context.Background() |
| 257 | |
| 258 | beforeLogs := []*Log{} |
| 259 | |
| 260 | h0 := NewBatchHandler(BatchOptions{ |
| 261 | BeforeAddFunc: func(_ context.Context, log *Log) bool { |
| 262 | beforeLogs = append(beforeLogs, log) |
| 263 | return true |
| 264 | }, |
| 265 | WriteFunc: func(_ context.Context, logs []*Log) error { |
| 266 | return nil |
| 267 | }, |
| 268 | }) |
| 269 | |
| 270 | h1 := h0.WithAttrs([]slog.Attr{slog.Int("a", 1), slog.String("b", "123")}) |
| 271 | |
| 272 | h2 := h1.WithGroup("sub").WithAttrs([]slog.Attr{ |
| 273 | slog.Int("c", 3), |
| 274 | slog.Any("d", map[string]any{"d.1": 1}), |
| 275 | slog.Any("e", errors.New("example error")), |
| 276 | }) |
| 277 | |
| 278 | record := slog.NewRecord(time.Now(), slog.LevelInfo, "hello", 0) |
| 279 | record.AddAttrs(slog.String("name", "test")) |
| 280 | |
| 281 | h0.Handle(ctx, record) |
| 282 | h1.Handle(ctx, record) |
| 283 | h2.Handle(ctx, record) |
| 284 | |
| 285 | // errors serialization checks |
| 286 | errorsRecord := slog.NewRecord(time.Now(), slog.LevelError, "details", 0) |
| 287 | errorsRecord.Add("validation.Errors", validation.Errors{ |
| 288 | "a": validation.NewError("validation_code", "validation_message"), |
| 289 | "b": errors.New("plain"), |
| 290 | }) |
| 291 | errorsRecord.Add("wrapped_validation.Errors", fmt.Errorf("wrapped: %w", validation.Errors{ |
| 292 | "a": validation.NewError("validation_code", "validation_message"), |
| 293 | "b": errors.New("plain"), |
| 294 | })) |
| 295 | errorsRecord.Add("map[string]any", map[string]any{ |
| 296 | "a": validation.NewError("validation_code", "validation_message"), |
| 297 | "b": errors.New("plain"), |
| 298 | "c": "test_any", |
| 299 | "d": map[string]any{ |
| 300 | "nestedA": validation.NewError("nested_code", "nested_message"), |
| 301 | "nestedB": errors.New("nested_plain"), |
| 302 | }, |
| 303 | }) |
| 304 | errorsRecord.Add("map[string]error", map[string]error{ |
| 305 | "a": validation.NewError("validation_code", "validation_message"), |
| 306 | "b": errors.New("plain"), |
| 307 | }) |
| 308 | errorsRecord.Add("map[string]validation.Error", map[string]validation.Error{ |
| 309 | "a": validation.NewError("validation_code", "validation_message"), |
| 310 | "b": nil, |
| 311 | }) |
| 312 | errorsRecord.Add("plain_error", errors.New("plain")) |
nothing calls this directly
no test coverage detected
searching dependent graphs…