-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: single-pass structured log parsing for level detection #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package logparser | ||
|
|
||
| import "strings" | ||
|
|
||
| // logfmtLevelKeys are the key names checked for level in logfmt-style logs. | ||
| var logfmtLevelKeys = []string{"level=", "lvl=", "severity="} | ||
|
|
||
| // parseLogfmtLevel extracts the log level from a logfmt-style line. | ||
| // It scans for level=/lvl=/severity= and parses the value. | ||
| // Returns (level, true) if a recognized level field is found. | ||
| func parseLogfmtLevel(line string) (Level, bool) { | ||
| lower := strings.ToLower(line) | ||
| for _, key := range logfmtLevelKeys { | ||
| idx := strings.Index(lower, key) | ||
| if idx < 0 { | ||
| continue | ||
| } | ||
| // Ensure it's at a word boundary (start of line or preceded by space/tab). | ||
| if idx > 0 { | ||
| prev := line[idx-1] | ||
| if prev != ' ' && prev != '\t' { | ||
| continue | ||
| } | ||
| } | ||
| valStart := idx + len(key) | ||
| if valStart >= len(line) { | ||
| continue | ||
| } | ||
| val := extractLogfmtValue(line[valStart:]) | ||
| if lvl := parseLevelValue(val); lvl != LevelUnknown { | ||
| return lvl, true | ||
| } | ||
| } | ||
| return LevelUnknown, false | ||
| } | ||
|
|
||
| // extractLogfmtValue extracts a logfmt value starting at the given position. | ||
| // Handles both quoted ("error") and unquoted (error) values. | ||
| func extractLogfmtValue(s string) string { | ||
| if len(s) == 0 { | ||
| return "" | ||
| } | ||
| if s[0] == '"' { | ||
| // Quoted value: find closing quote. | ||
| end := strings.IndexByte(s[1:], '"') | ||
| if end < 0 { | ||
| return "" | ||
| } | ||
| return s[1 : 1+end] | ||
| } | ||
| // Unquoted value: ends at space or end of string. | ||
| end := strings.IndexAny(s, " \t") | ||
| if end < 0 { | ||
| return s | ||
| } | ||
| return s[:end] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package logparser | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestParseLogfmtLevel(t *testing.T) { | ||
| // Basic unquoted values | ||
| lvl, ok := parseLogfmtLevel(`ts=2024-01-15 level=error msg="failed"`) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, LevelError, lvl) | ||
|
|
||
| lvl, ok = parseLogfmtLevel(`level=info msg="started"`) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, LevelInfo, lvl) | ||
|
|
||
| lvl, ok = parseLogfmtLevel(`ts=2024-01-15 level=warn msg="slow"`) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, LevelWarning, lvl) | ||
|
|
||
| // Quoted values | ||
| lvl, ok = parseLogfmtLevel(`ts=2024 level="error" msg="timeout"`) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, LevelError, lvl) | ||
|
|
||
| lvl, ok = parseLogfmtLevel(`level="WARNING" msg="deprecated"`) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, LevelWarning, lvl) | ||
|
|
||
| // Alternative key names | ||
| lvl, ok = parseLogfmtLevel(`ts=2024 lvl=error msg="fail"`) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, LevelError, lvl) | ||
|
|
||
| lvl, ok = parseLogfmtLevel(`severity=fatal msg="crash"`) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, LevelCritical, lvl) | ||
|
|
||
| // No level field | ||
| _, ok = parseLogfmtLevel(`ts=2024 msg="no level here"`) | ||
| assert.False(t, ok) | ||
|
|
||
| // Not logfmt at all | ||
| _, ok = parseLogfmtLevel(`just a plain text log line`) | ||
| assert.False(t, ok) | ||
|
|
||
| // level= not at word boundary (should not match) | ||
| _, ok = parseLogfmtLevel(`mylevel=error msg="fail"`) | ||
| assert.False(t, ok) | ||
|
|
||
| // level at start of line (valid) | ||
| lvl, ok = parseLogfmtLevel(`level=debug msg="trace"`) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, LevelDebug, lvl) | ||
| } | ||
|
|
||
| func TestExtractLogfmtValue(t *testing.T) { | ||
| assert.Equal(t, "error", extractLogfmtValue(`error msg="test"`)) | ||
| assert.Equal(t, "error", extractLogfmtValue(`"error" msg="test"`)) | ||
| assert.Equal(t, "warn", extractLogfmtValue("warn")) | ||
| assert.Equal(t, "", extractLogfmtValue("")) | ||
| assert.Equal(t, "info", extractLogfmtValue(`"info"`)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.