Skip to content

Commit

Permalink
fix: fixes log levels not being parsed correctly and supports short l…
Browse files Browse the repository at this point in the history
…evels like dbg (#3491)
  • Loading branch information
amir20 authored Dec 30, 2024
1 parent d93efed commit 9cff809
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
5 changes: 1 addition & 4 deletions assets/components/LogViewer/LogLevel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ defineProps<{
}
[data-level="error"],
[data-level="severe"],
[data-level="critical"],
[data-level="fatal"] {
@apply !bg-red;
}
[data-level="warn"],
[data-level="warning"] {
[data-level="warn"] {
@apply !bg-orange;
}
</style>
58 changes: 37 additions & 21 deletions internal/docker/level_guesser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
}
Expand All @@ -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
}
}

Expand Down
6 changes: 5 additions & 1 deletion internal/docker/level_guesser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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"},
Expand Down

0 comments on commit 9cff809

Please sign in to comment.