diff --git a/append_handler_test.go b/append_handler_test.go index ebf868b..f49ff60 100644 --- a/append_handler_test.go +++ b/append_handler_test.go @@ -125,10 +125,10 @@ func TestAppendHandler_CaseInsensitiveKeepIfBuiltinConflict(t *testing.T) { t.Parallel() tester := &testHandler{} - h := NewAppendHandler(tester, &AppendHandlerOptions{ + h := NewAppendMiddleware(&AppendHandlerOptions{ KeyCompare: CaseInsensitiveCmp, ResolveBuiltinKeyConflict: KeepIfBuiltinKeyConflict, - }) + })(tester) log := slog.New(h) log.Info("case insenstive, keep builtin conflict", "arg1", "val1", "ARG1", "val2", slog.MessageKey, "builtin-conflict") diff --git a/ignore_handler_test.go b/ignore_handler_test.go index cfaac4d..7482374 100644 --- a/ignore_handler_test.go +++ b/ignore_handler_test.go @@ -1,6 +1,7 @@ package slogdedup import ( + "log/slog" "strings" "testing" ) @@ -51,3 +52,37 @@ func TestIgnoreHandler(t *testing.T) { checkRecordForDuplicates(t, tester.Record) } + +func TestIgnoreHandler_ResolveBuiltinKeyConflict(t *testing.T) { + t.Parallel() + + tester := &testHandler{} + h := NewIgnoreMiddleware(&IgnoreHandlerOptions{ + ResolveBuiltinKeyConflict: func(k string) (string, bool) { + if k == "time" { + return "", false + } else { + return "arg-" + k, true + } + }, + })(tester) + + slog.New(h).Info("main message", "time", "hello", "foo", "bar") + + jBytes, err := tester.MarshalJSON() + if err != nil { + t.Errorf("Unable to marshal json: %v", err) + } + jStr := strings.TrimSpace(string(jBytes)) + + expected := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","arg-foo":"bar"}` + if jStr != expected { + t.Errorf("Expected:\n%s\nGot:\n%s", expected, jStr) + } + + // Uncomment to see the results + // t.Error(jStr) + // t.Error(tester.String()) + + checkRecordForDuplicates(t, tester.Record) +} diff --git a/increment_handler_test.go b/increment_handler_test.go index df215c1..866836e 100644 --- a/increment_handler_test.go +++ b/increment_handler_test.go @@ -1,6 +1,8 @@ package slogdedup import ( + "log/slog" + "strconv" "strings" "testing" ) @@ -90,3 +92,39 @@ func TestIncrementHandler(t *testing.T) { checkRecordForDuplicates(t, tester.Record) } + +func TestIncrementHandler_DoesKeyConflict_IncrementKeyName(t *testing.T) { + t.Parallel() + + tester := &testHandler{} + h := NewIncrementMiddleware(&IncrementHandlerOptions{ + DoesBuiltinKeyConflict: func(key string) bool { + if key == "foo" { + return true + } + return false + }, + IncrementKeyName: func(key string, index int) string { + return key + "@" + strconv.Itoa(index) + }, + })(tester) + + slog.New(h).Info("main message", "foo", "bar") + + jBytes, err := tester.MarshalJSON() + if err != nil { + t.Errorf("Unable to marshal json: %v", err) + } + jStr := strings.TrimSpace(string(jBytes)) + + expected := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","foo@1":"bar"}` + if jStr != expected { + t.Errorf("Expected:\n%s\nGot:\n%s", expected, jStr) + } + + // Uncomment to see the results + // t.Error(jStr) + // t.Error(tester.String()) + + checkRecordForDuplicates(t, tester.Record) +} diff --git a/overwrite_handler_test.go b/overwrite_handler_test.go index 2974990..5db4e16 100644 --- a/overwrite_handler_test.go +++ b/overwrite_handler_test.go @@ -88,10 +88,10 @@ func TestOverwriteHandler_CaseInsensitiveDropBuiltinConflicts(t *testing.T) { t.Parallel() tester := &testHandler{} - h := NewOverwriteHandler(tester, &OverwriteHandlerOptions{ + h := NewOverwriteMiddleware(&OverwriteHandlerOptions{ KeyCompare: CaseInsensitiveCmp, ResolveBuiltinKeyConflict: DropIfBuiltinKeyConflict, - }) + })(tester) log := slog.New(h) log.Info("case insenstive, drop builtin conflict", "arg1", "val1", "ARG1", "val2", slog.MessageKey, "builtin-conflict")