1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-30 17:47:28 +01:00

fix: fixes log levels not being parsed correctly and supports short levels like dbg (#3491)

This commit is contained in:
Amir Raminfar
2024-12-30 09:59:01 -08:00
committed by GitHub
parent d93efedc11
commit 9cff809a8e
3 changed files with 43 additions and 26 deletions

View File

@@ -11,22 +11,37 @@ import (
var SupportedLogLevels map[string]struct{}
// Changing this also needs to change the logContext.ts file
var logLevels = []string{"error", "warn", "warning", "info", "debug", "trace", "severe", "critical", "fatal"}
var plainLevels = map[string]*regexp.Regexp{}
var bracketLevels = map[string]*regexp.Regexp{}
var logLevels = [][]string{
{"error", "err"},
{"warn", "warning"},
{"info", "inf"},
{"debug", "dbg"},
{"trace"},
{"fatal", "sev", "severe", "crit", "critical"},
}
var plainLevels = map[string][]*regexp.Regexp{}
var bracketLevels = map[string][]*regexp.Regexp{}
var timestampRegex = regexp.MustCompile(`^(?:\d{4}[-/]\d{2}[-/]\d{2}(?:[T ](?:\d{2}:\d{2}:\d{2}(?:\.\d+)?Z?|\d{2}:\d{2}(?:AM|PM)))?\s+)`)
func init() {
for _, level := range logLevels {
plainLevels[level] = regexp.MustCompile("(?i)^" + level + "[^a-z]")
for _, levelGroup := range logLevels {
first := levelGroup[0]
for _, level := range levelGroup {
plainLevels[first] = append(plainLevels[first], regexp.MustCompile("(?i)^"+level+"[^a-z]"))
}
}
for _, level := range logLevels {
bracketLevels[level] = regexp.MustCompile("(?i)\\[ ?" + level + " ?\\]")
for _, levelGroup := range logLevels {
first := levelGroup[0]
for _, level := range levelGroup {
bracketLevels[first] = append(bracketLevels[first], regexp.MustCompile("(?i)\\[ ?"+level+" ?\\]"))
}
}
SupportedLogLevels = make(map[string]struct{}, len(logLevels)+1)
for _, level := range logLevels {
SupportedLogLevels[level] = struct{}{}
for _, levelGroup := range logLevels {
SupportedLogLevels[levelGroup[0]] = struct{}{}
}
SupportedLogLevels["unknown"] = struct{}{}
}
@@ -35,25 +50,26 @@ func guessLogLevel(logEvent *LogEvent) string {
switch value := logEvent.Message.(type) {
case string:
value = stripANSI(value)
for _, level := range logLevels {
value = timestampRegex.ReplaceAllString(value, "")
for _, levelGroup := range logLevels {
first := levelGroup[0]
// Look for the level at the beginning of the message
if plainLevels[level].MatchString(value) {
return level
for _, regex := range plainLevels[first] {
if regex.MatchString(value) {
return first
}
}
// Look for the level in brackets
if bracketLevels[level].MatchString(value) {
return level
for _, regex := range bracketLevels[first] {
if regex.MatchString(value) {
return first
}
}
// Look for the level in the middle of the message that are uppercase
if strings.Contains(value, " "+strings.ToUpper(level)+" ") {
return level
}
// Look for levels with equal sign and quotes around them
if strings.Contains(value, level+"=") {
return level
if strings.Contains(value, " "+strings.ToUpper(first)+" ") {
return first
}
}

View File

@@ -13,6 +13,8 @@ func TestGuessLogLevel(t *testing.T) {
input any
expected string
}{
{"2024/12/30 12:21AM INF this is a test", "info"},
{"2024-12-30T17:43:16Z DBG loggging debug from here", "debug"},
{"ERROR: Something went wrong", "error"},
{"WARN: Something might be wrong", "warn"},
{"INFO: Something happened", "info"},
@@ -25,10 +27,12 @@ func TestGuessLogLevel(t *testing.T) {
{"[ ERROR ] Something went wrong", "error"},
{"[error] Something went wrong", "error"},
{"[test] [error] Something went wrong", "error"},
{"Some test with error=test", "error"},
{"[foo] [ ERROR] Something went wrong", "error"},
{"123 ERROR Something went wrong", "error"},
{"123 Something went wrong", "unknown"},
{"DBG Something went wrong", "debug"},
{"inf Something went wrong", "info"},
{"crit: Something went wrong", "fatal"},
{orderedmap.New[string, string](
orderedmap.WithInitialData(
orderedmap.Pair[string, string]{Key: "key", Value: "value"},