1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-21 21:33:18 +01:00

feat: Supports log levels with common separators (#3656)

This commit is contained in:
garikkh
2025-02-21 11:56:02 -08:00
committed by GitHub
parent bc842389f9
commit 90b90ceea4
2 changed files with 19 additions and 20 deletions

View File

@@ -20,23 +20,18 @@ var logLevels = [][]string{
{"fatal", "sev", "severe", "crit", "critical"}, {"fatal", "sev", "severe", "crit", "critical"},
} }
var plainLevels = map[string][]*regexp.Regexp{} var plainLevels = map[string]*regexp.Regexp{}
var bracketLevels = map[string][]*regexp.Regexp{} var bracketLevels = map[string]*regexp.Regexp{}
var separatorLevels = 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+)`) 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() { func init() {
for _, levelGroup := range logLevels { for _, levelGroup := range logLevels {
first := levelGroup[0] first := levelGroup[0]
for _, level := range levelGroup { levelsGroup := "(?:" + strings.Join(levelGroup, "|") + ")"
plainLevels[first] = append(plainLevels[first], regexp.MustCompile("(?i)^"+level+"[^a-z]")) plainLevels[first] = regexp.MustCompile("(?i)^" + levelsGroup + "[^a-z]")
} bracketLevels[first] = regexp.MustCompile("(?i)\\[ ?" + levelsGroup + " ?\\]")
} separatorLevels[first] = regexp.MustCompile("(?i) " + levelsGroup + "[/-]")
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) SupportedLogLevels = make(map[string]struct{}, len(logLevels)+1)
@@ -54,17 +49,18 @@ func guessLogLevel(logEvent *LogEvent) string {
for _, levelGroup := range logLevels { for _, levelGroup := range logLevels {
first := levelGroup[0] first := levelGroup[0]
// Look for the level at the beginning of the message // Look for the level at the beginning of the message
for _, regex := range plainLevels[first] { if plainLevels[first].MatchString(value) {
if regex.MatchString(value) {
return first return first
} }
}
// Look for the level in brackets // Look for the level in brackets
for _, regex := range bracketLevels[first] { if bracketLevels[first].MatchString(value) {
if regex.MatchString(value) {
return first return first
} }
// Look for the level with a separator after
if separatorLevels[first].MatchString(value) {
return first
} }
// Look for the level in the middle of the message that are uppercase and surrounded by quotes // Look for the level in the middle of the message that are uppercase and surrounded by quotes

View File

@@ -14,6 +14,8 @@ func TestGuessLogLevel(t *testing.T) {
expected string expected string
}{ }{
{"2024/12/30 12:21AM INF this is a test", "info"}, {"2024/12/30 12:21AM INF this is a test", "info"},
{"2025-01-07 22:00:08,059: DEBUG/MainProcess TaskPool: ", "debug"},
{"Some test with error-test", "error"},
{"2024-12-30T17:43:16Z DBG loggging debug from here", "debug"}, {"2024-12-30T17:43:16Z DBG loggging debug from here", "debug"},
{"2025-01-07 15:40:15,784 LL=\"ERROR\" some message", "error"}, {"2025-01-07 15:40:15,784 LL=\"ERROR\" some message", "error"},
{"2025-01-07 15:40:15,784 LL=\"WARN\" some message", "warn"}, {"2025-01-07 15:40:15,784 LL=\"WARN\" some message", "warn"},
@@ -35,6 +37,7 @@ func TestGuessLogLevel(t *testing.T) {
{"123 ERROR Something went wrong", "error"}, {"123 ERROR Something went wrong", "error"},
{"123 Something went wrong", "unknown"}, {"123 Something went wrong", "unknown"},
{"DBG Something went wrong", "debug"}, {"DBG Something went wrong", "debug"},
{"DBG with more error=msg", "debug"},
{"inf Something went wrong", "info"}, {"inf Something went wrong", "info"},
{"crit: Something went wrong", "fatal"}, {"crit: Something went wrong", "fatal"},
{orderedmap.New[string, string]( {orderedmap.New[string, string](