1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-22 03:13:33 +01:00

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:** **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)) * 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 ## ntfy server v2.1.0
Released February 25, 2023 Released February 25, 2023

View file

@ -210,11 +210,13 @@ func (e *Event) globalLevelWithOverride() Level {
if e.fields == nil { if e.fields == nil {
return l return l
} }
for field, override := range ov { for field, fieldOverrides := range ov {
value, exists := e.fields[field] value, exists := e.fields[field]
if exists { if exists {
if override.value == "" || override.value == value || override.value == fmt.Sprintf("%v", value) { for _, o := range fieldOverrides {
return override.level if o.value == "" || o.value == value || o.value == fmt.Sprintf("%v", value) {
return o.level
}
} }
} }
} }

View file

@ -19,7 +19,7 @@ var (
var ( var (
level = DefaultLevel level = DefaultLevel
format = DefaultFormat format = DefaultFormat
overrides = make(map[string]*levelOverride) overrides = make(map[string][]*levelOverride)
output io.Writer = DefaultOutput output io.Writer = DefaultOutput
filename = "" filename = ""
mu = &sync.RWMutex{} mu = &sync.RWMutex{}
@ -111,14 +111,17 @@ func SetLevel(newLevel Level) {
func SetLevelOverride(field string, value string, level Level) { func SetLevelOverride(field string, value string, level Level) {
mu.Lock() mu.Lock()
defer mu.Unlock() 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 // ResetLevelOverrides removes all log level overrides
func ResetLevelOverrides() { func ResetLevelOverrides() {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
overrides = make(map[string]*levelOverride) overrides = make(map[string][]*levelOverride)
} }
// CurrentFormat returns the current log format // CurrentFormat returns the current log format

View file

@ -177,6 +177,27 @@ func TestLog_LevelOverrideAny(t *testing.T) {
require.Equal(t, "", File()) 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) { func TestLog_UsingStdLogger_JSON(t *testing.T) {
t.Cleanup(resetState) t.Cleanup(resetState)