-
Notifications
You must be signed in to change notification settings - Fork 2
fix(log): route Writer through the structured encoder instead of raw passthrough #797
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
+201
−15
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dbf8a66
fix(log): route Writer through the structured encoder instead of raw …
skevetter fe12612
fix(log): satisfy funcorder and goconst lint rules
skevetter e2a84ec
style: trim comment
skevetter 3b03917
fix(log): address CodeRabbit review on Writer
skevetter 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package log | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| const testFormatJSON = "json" | ||
|
|
||
| func TestWriter_EmitsStructuredJSONLine(t *testing.T) { | ||
| Init(Config{Verbosity: 2, Format: testFormatJSON}) | ||
|
|
||
| var sink bytes.Buffer | ||
| remove := AddSink(&sink) | ||
| defer remove() | ||
|
|
||
| w := Writer(LevelInfo) | ||
| _, err := w.Write([]byte("Cloning into 'repo'...\n")) | ||
| if err != nil { | ||
| t.Fatalf("Write: %v", err) | ||
| } | ||
| _ = Sync() | ||
|
|
||
| got := strings.TrimSpace(sink.String()) | ||
| if !strings.HasPrefix(got, "{") || !strings.HasSuffix(got, "}") { | ||
| t.Fatalf("expected a single JSON object, got %q", got) | ||
| } | ||
| if !strings.Contains(got, `"msg":"Cloning into 'repo'..."`) { | ||
| t.Errorf("missing expected msg field: %q", got) | ||
| } | ||
| if !strings.Contains(got, `"level":"info"`) { | ||
| t.Errorf("missing expected level field: %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestWriter_SplitsMultipleLinesInOneWrite(t *testing.T) { | ||
| Init(Config{Verbosity: 2, Format: testFormatJSON}) | ||
|
|
||
| var sink bytes.Buffer | ||
| remove := AddSink(&sink) | ||
| defer remove() | ||
|
|
||
| w := Writer(LevelInfo) | ||
| _, err := w.Write([]byte("line one\nline two\n")) | ||
| if err != nil { | ||
| t.Fatalf("Write: %v", err) | ||
| } | ||
|
|
||
| // A line split across two Write calls (as os/exec delivers subprocess | ||
| // output in arbitrary chunks) must still be logged as one complete | ||
| // record, not two fragments. | ||
| if _, err := w.Write([]byte("line three\nline ")); err != nil { | ||
| t.Fatalf("Write: %v", err) | ||
| } | ||
| if _, err := w.Write([]byte("four\n")); err != nil { | ||
| t.Fatalf("Write: %v", err) | ||
| } | ||
| _ = Sync() | ||
|
|
||
| lines := strings.Split(strings.TrimSpace(sink.String()), "\n") | ||
| wantMsgs := []string{"line one", "line two", "line three", "line four"} | ||
| if len(lines) != len(wantMsgs) { | ||
| t.Fatalf("got %d lines, want %d: %q", len(lines), len(wantMsgs), lines) | ||
| } | ||
| for i, l := range lines { | ||
| assertJSONLineMsg(t, i, l, wantMsgs[i]) | ||
| } | ||
| } | ||
|
|
||
| func assertJSONLineMsg(t *testing.T, i int, line, wantMsg string) { | ||
| t.Helper() | ||
| if !strings.HasPrefix(line, "{") || !strings.HasSuffix(line, "}") { | ||
| t.Errorf("line %d is not valid single-object JSON: %q", i, line) | ||
| return | ||
| } | ||
| var rec struct { | ||
| Msg string `json:"msg"` | ||
| } | ||
| if err := json.Unmarshal([]byte(line), &rec); err != nil { | ||
| t.Errorf("line %d: json.Unmarshal: %v", i, err) | ||
| return | ||
| } | ||
| if rec.Msg != wantMsg { | ||
| t.Errorf("line %d msg = %q, want %q", i, rec.Msg, wantMsg) | ||
| } | ||
| } | ||
|
|
||
| func TestWriter_FlushesTrailingPartialLineOnClose(t *testing.T) { | ||
| Init(Config{Verbosity: 2, Format: testFormatJSON}) | ||
|
|
||
| var sink bytes.Buffer | ||
| remove := AddSink(&sink) | ||
| defer remove() | ||
|
|
||
| w := Writer(LevelInfo) | ||
| _, _ = w.Write([]byte("no trailing newline")) | ||
| _ = Sync() | ||
| if sink.Len() != 0 { | ||
| t.Errorf("expected nothing logged before Close, got %q", sink.String()) | ||
| } | ||
|
|
||
| if err := w.Close(); err != nil { | ||
| t.Fatalf("Close: %v", err) | ||
| } | ||
| _ = Sync() | ||
|
|
||
| if !strings.Contains(sink.String(), "no trailing newline") { | ||
| t.Errorf("expected trailing partial line flushed on Close, got %q", sink.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestWriter_PreservesBlankLines(t *testing.T) { | ||
| Init(Config{Verbosity: 2, Format: testFormatJSON}) | ||
|
|
||
| var sink bytes.Buffer | ||
| remove := AddSink(&sink) | ||
| defer remove() | ||
|
|
||
| w := Writer(LevelInfo) | ||
| if _, err := w.Write([]byte("one\n\ntwo\n")); err != nil { | ||
| t.Fatalf("Write: %v", err) | ||
| } | ||
| _ = Sync() | ||
|
|
||
| lines := strings.Split(strings.TrimSpace(sink.String()), "\n") | ||
| if len(lines) != 3 { | ||
| t.Fatalf("got %d lines, want 3 (blank line preserved): %q", len(lines), lines) | ||
| } | ||
| if !strings.Contains(lines[1], `"msg":""`) { | ||
| t.Errorf("line 1 = %q, want an empty msg field", lines[1]) | ||
| } | ||
| } | ||
|
|
||
| func TestWriter_DiscardsBelowConfiguredLevel(t *testing.T) { | ||
| Init(Config{Verbosity: 1, Format: testFormatJSON}) // info+ only, debug disabled | ||
|
|
||
| var sink bytes.Buffer | ||
| remove := AddSink(&sink) | ||
| defer remove() | ||
|
|
||
| w := Writer(LevelDebug) | ||
| _, _ = w.Write([]byte("should not appear\n")) | ||
| _ = w.Close() | ||
| _ = Sync() | ||
|
|
||
| if sink.Len() != 0 { | ||
| t.Errorf("expected debug output discarded below configured level, got %q", sink.String()) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Validate the blank record through the JSON helper.
The substring check can accept malformed output, while
Msg stringcannot distinguish a missing field from{"msg":""}. MakeMsga pointer (or validate the raw field), then callassertJSONLineMsg(t, 1, lines[1], "").Proposed test hardening
var rec struct { - Msg string `json:"msg"` + Msg *string `json:"msg"` } if err := json.Unmarshal([]byte(line), &rec); err != nil { t.Errorf("line %d: json.Unmarshal: %v", i, err) return } - if rec.Msg != wantMsg { - t.Errorf("line %d msg = %q, want %q", i, rec.Msg, wantMsg) + if rec.Msg == nil { + t.Errorf("line %d is missing msg", i) + return + } + if *rec.Msg != wantMsg { + t.Errorf("line %d msg = %q, want %q", i, *rec.Msg, wantMsg) } - if !strings.Contains(lines[1], `"msg":""`) { - t.Errorf("line 1 = %q, want an empty msg field", lines[1]) - } + assertJSONLineMsg(t, 1, lines[1], "")Also applies to: 131-133
🤖 Prompt for AI Agents