From 0af3e29ce1962f4cf16e731e291b82c027e37196 Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Mon, 27 Feb 2023 11:03:21 -0500 Subject: [PATCH] Allow multiple `log-level-overrides` on the same field --- docs/releases.md | 3 ++- log/event.go | 8 +++++--- log/log.go | 9 ++++++--- log/log_test.go | 21 +++++++++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/docs/releases.md b/docs/releases.md index 92dd4dbd..533909ec 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -6,8 +6,9 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release **Bug fixes + maintenance:** -* Upgrade dialog: Disable submit button for free tier (no ticket) * Remove health check from `Dockerfile` and [document it](config.md#health-checks) ([#635](https://github.com/binwiederhier/ntfy/issues/635), thanks to [@Andersbiha](https://github.com/Andersbiha)) +* Upgrade dialog: Disable submit button for free tier (no ticket) +* Allow multiple `log-level-overrides` on the same field (no ticket) ## ntfy server v2.1.0 Released February 25, 2023 diff --git a/log/event.go b/log/event.go index 1e99b7bf..775159b5 100644 --- a/log/event.go +++ b/log/event.go @@ -210,11 +210,13 @@ func (e *Event) globalLevelWithOverride() Level { if e.fields == nil { return l } - for field, override := range ov { + for field, fieldOverrides := range ov { value, exists := e.fields[field] if exists { - if override.value == "" || override.value == value || override.value == fmt.Sprintf("%v", value) { - return override.level + for _, o := range fieldOverrides { + if o.value == "" || o.value == value || o.value == fmt.Sprintf("%v", value) { + return o.level + } } } } diff --git a/log/log.go b/log/log.go index 7c0f9cdc..20ad6151 100644 --- a/log/log.go +++ b/log/log.go @@ -19,7 +19,7 @@ var ( var ( level = DefaultLevel format = DefaultFormat - overrides = make(map[string]*levelOverride) + overrides = make(map[string][]*levelOverride) output io.Writer = DefaultOutput filename = "" mu = &sync.RWMutex{} @@ -111,14 +111,17 @@ func SetLevel(newLevel Level) { func SetLevelOverride(field string, value string, level Level) { mu.Lock() defer mu.Unlock() - overrides[field] = &levelOverride{value: value, level: level} + if _, ok := overrides[field]; !ok { + overrides[field] = make([]*levelOverride, 0) + } + overrides[field] = append(overrides[field], &levelOverride{value: value, level: level}) } // ResetLevelOverrides removes all log level overrides func ResetLevelOverrides() { mu.Lock() defer mu.Unlock() - overrides = make(map[string]*levelOverride) + overrides = make(map[string][]*levelOverride) } // CurrentFormat returns the current log format diff --git a/log/log_test.go b/log/log_test.go index 0d1ec4af..ed35b495 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -177,6 +177,27 @@ func TestLog_LevelOverrideAny(t *testing.T) { require.Equal(t, "", File()) } +func TestLog_LevelOverride_ManyOnSameField(t *testing.T) { + t.Cleanup(resetState) + + var out bytes.Buffer + SetOutput(&out) + SetFormat(JSONFormat) + SetLevelOverride("tag", "manager", DebugLevel) + SetLevelOverride("tag", "publish", DebugLevel) + + Time(time.Unix(11, 0).UTC()).Field("tag", "manager").Debug("this is logged") + Time(time.Unix(12, 0).UTC()).Field("tag", "no-match").Debug("this is not logged") + Time(time.Unix(13, 0).UTC()).Field("tag", "publish").Info("this is also logged") + + expected := `{"time":"1970-01-01T00:00:11Z","level":"DEBUG","message":"this is logged","tag":"manager"} +{"time":"1970-01-01T00:00:13Z","level":"INFO","message":"this is also logged","tag":"publish"} +` + require.Equal(t, expected, out.String()) + require.False(t, IsFile()) + require.Equal(t, "", File()) +} + func TestLog_UsingStdLogger_JSON(t *testing.T) { t.Cleanup(resetState)