Logging: Fix so that filters can contain commented lines (#45159)

Fixes log filters that contains comments should not be enabled
This commit is contained in:
Marcus Efraimsson 2022-02-10 09:52:37 +01:00 committed by GitHub
parent 88cf2da12e
commit 2082eeb72f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 1 deletions

View File

@ -251,7 +251,16 @@ func getLogLevelFromString(levelName string) level.Option {
func getFilters(filterStrArray []string) map[string]level.Option {
filterMap := make(map[string]level.Option)
for _, filterStr := range filterStrArray {
for i := 0; i < len(filterStrArray); i++ {
filterStr := strings.TrimSpace(filterStrArray[i])
if strings.HasPrefix(filterStr, ";") || strings.HasPrefix(filterStr, "#") {
if len(filterStr) == 1 {
i++
}
continue
}
parts := strings.Split(filterStr, ":")
if len(parts) > 1 {
filterMap[parts[0]] = getLogLevelFromString(parts[1])

View File

@ -7,6 +7,7 @@ import (
gokitlog "github.com/go-kit/log"
"github.com/grafana/grafana/pkg/infra/log/level"
"github.com/grafana/grafana/pkg/util"
"github.com/stretchr/testify/require"
)
@ -107,3 +108,73 @@ func TestLogger(t *testing.T) {
})
})
}
func TestGetFilters(t *testing.T) {
t.Run("Parsing filters on single line with only space should return expected result", func(t *testing.T) {
filter := ` `
filters := getFilters(util.SplitString(filter))
require.Len(t, filters, 0)
})
t.Run("Parsing filters on single line with should return expected result", func(t *testing.T) {
filter := `rendering:debug oauth.generic_oauth:debug testwithoutlevel provisioning.dashboard:debug`
filters := getFilters(util.SplitString(filter))
keys := []string{}
for k := range filters {
keys = append(keys, k)
}
require.ElementsMatch(t, []string{
"rendering",
"oauth.generic_oauth",
"provisioning.dashboard",
}, keys)
})
t.Run("Parsing filters spread over multiple lines with comments should return expected result", func(t *testing.T) {
filter := `rendering:debug \
; alerting.notifier:debug \
oauth.generic_oauth:debug \
; oauth.okta:debug \
; tsdb.postgres:debug \
;tsdb.mssql:debug \
#provisioning.plugins:debug \
provisioning.dashboard:debug \
data-proxy-log:debug \
;oauthtoken:debug \
plugins.backend:debug \
tsdb.elasticsearch.client:debug \
server:debug \
tsdb.graphite:debug \
auth:debug \
plugin.manager:debug \
plugin.initializer:debug \
plugin.loader:debug \
plugin.finder:debug \
plugin.installer:debug \
plugin.signature.validator:debug`
filters := getFilters(util.SplitString(filter))
keys := []string{}
for k := range filters {
keys = append(keys, k)
}
require.ElementsMatch(t, []string{
"rendering",
"oauth.generic_oauth",
"provisioning.dashboard",
"data-proxy-log",
"plugins.backend",
"tsdb.elasticsearch.client",
"server",
"tsdb.graphite",
"auth",
"plugin.manager",
"plugin.initializer",
"plugin.loader",
"plugin.finder",
"plugin.installer",
"plugin.signature.validator",
}, keys)
})
}