Code review (round 1)

This commit is contained in:
binwiederhier 2023-02-08 22:57:10 -05:00
parent 7706bd9845
commit b37cf02a6e
12 changed files with 78 additions and 23 deletions

View file

@ -197,8 +197,12 @@ func (e *Event) globalLevelWithOverride() Level {
}
for field, override := range ov {
value, exists := e.fields[field]
if exists && value == override.value {
return override.level
if exists {
if value == override.value {
return override.level
} else if fmt.Sprintf("%v", value) == override.value {
return override.level
}
}
}
return l

View file

@ -93,7 +93,7 @@ func SetLevel(newLevel Level) {
}
// SetLevelOverride adds a log override for the given field
func SetLevelOverride(field string, value any, level Level) {
func SetLevelOverride(field string, value string, level Level) {
mu.Lock()
defer mu.Unlock()
overrides[field] = &levelOverride{value: value, level: level}

View file

@ -29,6 +29,7 @@ func TestLog_TagContextFieldFields(t *testing.T) {
SetOutput(&out)
SetFormat(JSONFormat)
SetLevelOverride("tag", "stripe", DebugLevel)
SetLevelOverride("number", "5", DebugLevel)
Tag("mytag").
Field("field2", 123).
@ -49,8 +50,13 @@ func TestLog_TagContextFieldFields(t *testing.T) {
Time(time.Unix(456, 123000000).UTC()).
Debug("Subscription status %s", "active")
Field("number", 5).
Time(time.Unix(777, 001000000).UTC()).
Debug("The number 5 is an int, but the level override is a string")
expected := `{"time":"1970-01-01T00:02:03.999Z","level":"INFO","message":"hi there phil","field1":"value1","field2":123,"tag":"mytag"}
{"time":"1970-01-01T00:07:36.123Z","level":"DEBUG","message":"Subscription status active","error":"some error","error_code":123,"stripe_customer_id":"acct_123","stripe_subscription_id":"sub_123","tag":"stripe","user_id":"u_abc","visitor_ip":"1.2.3.4"}
{"time":"1970-01-01T00:12:57Z","level":"DEBUG","message":"The number 5 is an int, but the level override is a string","number":5}
`
require.Equal(t, expected, out.String())
}

View file

@ -55,6 +55,8 @@ func ToLevel(s string) Level {
return WarnLevel
case "ERROR":
return ErrorLevel
case "FATAL":
return FatalLevel
default:
return InfoLevel
}
@ -101,6 +103,6 @@ type Contexter interface {
type Context map[string]any
type levelOverride struct {
value any
value string
level Level
}