Skip to content

Commit

Permalink
Fix last issues with phuslu/slog handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mAdkins committed May 1, 2024
1 parent e70e581 commit 3edf7d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
9 changes: 0 additions & 9 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
TODO
* phuslu/slog
x get it done
x what's up with TestLevelVar: LevelVar unused warning?
Seems to happen to all handlers that don't use it.
- Required [Mismatch] Logged record does not match expected: Attributes: ?Any
Handler seems to dump JSON in a string instead of group object for some types.
IP data and mac (h/w) address are good examples.
Fix it with encoding/json magic or configure a unique warning?
Apply the unique warning where currently using the strFields list.
* Discuss slog_madkins_replattr benchmark somewhere
* Use json.Expect and reflect.DeepEqual()/assert.Equal() more often
* Creator could have:
Expand Down
29 changes: 14 additions & 15 deletions bench/tests/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,23 @@ func fields(testName string, fields ...string) VerifyFn {
func finder(testName string, expected map[string]any) VerifyFn {
return func(captured []byte, logMap map[string]any, manager *warning.Manager) error {
logMap = getLogMap(captured, logMap, manager)
if badFields, strFields, err := finderDeep(expected, logMap, ""); err != nil {
badFields, strFields, err := finderDeep(expected, logMap, "")
if err != nil {
return fmt.Errorf("finderDeep: %w", err)
} else if len(badFields) > 0 || len(strFields) > 0 {
var fields []string
for _, field := range badFields {
test.Debugf(2, ">?> bad %s: %v != %v\n", field, expected[field], logMap[field])
fields = append(fields, "!"+field)
}
for _, field := range strFields {
test.Debugf(2, ">?> str %s: %v != %v\n", field, expected[field], logMap[field])
fields = append(fields, "?"+field)
}
text := testName + ": " + strings.Join(fields, ",")
}
if len(badFields) > 0 {
text := testName + ": " + strings.Join(badFields, ",")
manager.AddWarningFn(warning.Mismatch, text, string(captured))
return warning.Mismatch.ErrorExtra(text)
} else {
return nil
} else if manager.HasWarning(warning.Mismatch) {
manager.AddUnused(warning.Mismatch, string(captured))
}
if len(strFields) > 0 {
text := testName + ": " + strings.Join(strFields, ",")
manager.AddWarningFn(warning.StringAny, text, string(captured))
} else if manager.HasWarning(warning.StringAny) {
manager.AddUnused(warning.StringAny, string(captured))
}
return nil
}
}

Expand Down
6 changes: 5 additions & 1 deletion internal/warning/suggested.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ var (
The log level name is not what was expected (e.g. "WARNING" instead of "WARN").
This is different from the LevelCase warning which is from the right level name but the wrong character case.`)

StringAny = NewWarning(LevelSuggested, "StringAny", "Some structs are converted to strings in log records", `
The ^slog.JSONHandler^ converts ^Any^ objects that are ^struct^s into JSON maps.
Some handlers convert one or more of these into strings instead of maps.`)

TimeMillis = NewWarning(LevelSuggested, "TimeMillis", "slog.Time() logs milliseconds instead of nanoseconds", `
The ^slog.JSONHandler^ uses nanoseconds for ^time.Time^ but some other handlers use milliseconds.
This does _not_ apply to the basic ^time^ field, only attribute fields.
Expand All @@ -61,7 +65,7 @@ var (

func init() {
// Always update this number when adding or removing Warning objects.
addTestCount(LevelSuggested, 9)
addTestCount(LevelSuggested, 10)
}

// Suggested returns an array of all LevelSuggested warnings.
Expand Down

0 comments on commit 3edf7d8

Please sign in to comment.