Skip to content

Commit

Permalink
improve detection of .NET log severities
Browse files Browse the repository at this point in the history
  • Loading branch information
def committed Dec 29, 2023
1 parent 1e9e775 commit 0599992
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
41 changes: 27 additions & 14 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,33 @@ func GuessLevel(line string) Level {
for _, sf := range subfields {
sf = strings.TrimLeft(strings.ToLower(sf), "\"[(<'")
sf = strings.TrimPrefix(sf, "level=")
if len(sf) < 4 {
continue
}
switch sf[:4] {
case "debu":
return LevelDebug
case "info", "noti":
return LevelInfo
case "warn":
return LevelWarning
case "erro":
return LevelError
case "crit", "fata":
return LevelCritical

if l := len(sf); l == 3 {
switch sf {
case "dbg", "trc":
return LevelDebug
case "inf":
return LevelInfo
case "wrn":
return LevelWarning
case "err":
return LevelError
case "ftl":
return LevelCritical
}
} else if l >= 4 {
switch sf[:4] {
case "debu":
return LevelDebug
case "info", "noti":
return LevelInfo
case "warn":
return LevelWarning
case "erro":
return LevelError
case "crit", "fata":
return LevelCritical
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ func TestGuessLevel(t *testing.T) {
assert.Equal(t, LevelDebug, GuessLevel(`[2020-06-25 17:35:37,609][DEBUG][action.search ] [srv] [tweets-100][6]`))
assert.Equal(t, LevelError, GuessLevel(`[2023-10-12T09:56:53.393595+00:00] otel-php.ERROR: Export failure {"exception":"[object] (RuntimeException(code: 0): Export retry limit exceeded at /var/www/vendor/open-telemetry/sdk/Common/Export/Http/PsrTransport.php:114)","source":"OpenTelemetry\\Contrib\\Otlp\\SpanExporter"} []`))
assert.Equal(t, LevelWarning, GuessLevel(`2023.10.12 13:58:41.168802 [ 847 ] {} <Warning> TCPHandler: Using deprecated interserver protocol because the client is too old. Consider upgrading all nodes in cluster.`))

assert.Equal(t, LevelDebug, GuessLevel("[06:23:18 DBG] message"))
assert.Equal(t, LevelInfo, GuessLevel("[06:23:18 INF] message"))
assert.Equal(t, LevelWarning, GuessLevel("[06:23:18 WRN] message"))
assert.Equal(t, LevelError, GuessLevel("[06:23:18 ERR] message"))
assert.Equal(t, LevelCritical, GuessLevel("[06:23:18 FTL] message"))

}

0 comments on commit 0599992

Please sign in to comment.