Allow multiple log-level-overrides on the same field

This commit is contained in:
binwiederhier 2023-02-27 11:03:21 -05:00
parent dd6462de13
commit 0af3e29ce1
4 changed files with 34 additions and 7 deletions

View file

@ -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

View file

@ -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
}
}
}
}

View file

@ -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

View file

@ -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)