fix(logger): misc fixes and ergonomics improvements #1662
Codecov Report
:x: Patch coverage is
69.07216% with 30 lines in your changes missing coverage. Please review.| Files with missing lines | Patch % | Lines |
|---|---|---|
| v2/lib/telemetry.zig | 10.00% | 9 Missing :warning: |
| v2/services/gossip.zig | 0.00% | 6 Missing :warning: |
| v2/lib/snapshot/download.zig | 0.00% | 4 Missing :warning: |
| v2/lib/telemetry/log.zig | 94.28% | 4 Missing :warning: |
| v2/services/accounts\_db.zig | 0.00% | 4 Missing :warning: |
| v2/lib/accounts\_db/rooted.zig | 0.00% | 1 Missing :warning: |
| v2/lib/gossip/node.zig | 0.00% | 1 Missing :warning: |
| v2/services/snapshot.zig | 0.00% | 1 Missing :warning: |
:x: Your patch check has failed because the patch coverage (69.07%) is below the target coverage (85.00%). You can increase the patch coverage or adjust the target coverage.
| Files with missing lines | Coverage Δ | |
|---|---|---|
| v2/lib/accounts\_db/rooted.zig | 0.00% <0.00%> (ø) | |
| v2/lib/gossip/node.zig | 0.00% <0.00%> (ø) | |
| v2/services/snapshot.zig | 0.00% <0.00%> (ø) | |
| v2/lib/snapshot/download.zig | 10.44% <0.00%> (-0.02%) | :arrow_down: |
| v2/lib/telemetry/log.zig | 57.50% <94.28%> (+5.57%) | :arrow_up: |
| v2/services/accounts\_db.zig | 0.00% <0.00%> (ø) | |
| v2/services/gossip.zig | 0.00% <0.00%> (ø) | |
| v2/lib/telemetry.zig | 59.73% <10.00%> (-2.29%) | :arrow_down: |
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
This docstring says to customize formatting via fromValue/fromFmt, but field() now takes a pointer and the customization path is fieldFmt(name, EntryValueFmt.fromValue/fromFmt(...)). The current wording is misleading for callers.
EntryValueFmt now formats twice (once via WhitespaceDetector.detect and again to the real writer) to decide quoting. This increases cost for every field/message, and the cost is multiplied again when std.fmt.count is used in Message.computeHeader (count + write). Consider an optimization/caching strategy for whitespace detection (or a quote policy) to avoid redundant formatting work on hot paths.
Pull request overview
This PR updates the v2 telemetry logger API to improve ergonomics and consistency, including simplifying quoting behavior (quote only when formatted output contains whitespace) and adding a .log() helper for no-arg messages. It also updates several services/call sites to use the new logging APIs.
Changes:
Logger.Entry.field() default formatting and introduce fieldFmt() for explicit formatting customization.EntryValueFmt and implement whitespace-driven quoting via a WhitespaceDetector..log() (no-args) and migrate a handful of log statements from logf(..., .{}) to log(...), plus convert one multiline log into structured fields.Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
<details>
<summary>Show a summary per file</summary>
| File | Description |
| ---- | ----------- |
| v2/services/snapshot.zig | Use new .log() helper for no-arg logging. |
| v2/services/gossip.zig | Convert startup log to structured fields + .log(). |
| v2/services/accounts_db.zig | Replace logf(..., .{}) with .log(...) in several places. |
| v2/lib/telemetry/log.zig | Remove surround mode and add whitespace-based quoting; update tests; add WhitespaceDetector. |
| v2/lib/telemetry.zig | Update Logger entry API: default .field() formatting, add fieldFmt(), add .log(), and adjust message encoding usage. |
| v2/lib/snapshot/download.zig | Convert completion log to structured fields + .log(). |
| v2/lib/gossip/node.zig | Use new .log() helper for no-arg logging. |
| v2/lib/accounts_db/rooted.zig | Use new .log() helper for no-arg logging. |
</details>
---
💡 <a href="/Syndica/sig/new/main?filename=.github/instructions/*.instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add Copilot custom instructions</a> for smarter, more guided reviews. <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn how to get started</a>.
This is complex to optimize and I don't think it's worth prioritizing at this point.
if (has_whitespace) try w.writeByte('"');
try w.print(fmt_str, args.*);
if (has_whitespace) try w.writeByte('"');
pub fn init(buffer: []u8) WhitespaceDetector {
return .{
.writer = .{
.vtable = &.{ .drain = drain },
.buffer = buffer,
},
.found = false,
};
}
Never any harm in taking a buffer, the caller can always just pass
&.{} if they don't care. Though passing a buffer will help to amortize indirect calls. var wd: WhitespaceDetector = undefined;
wd = .init();
try wd.writer.writeAll("no-whitespace-here");
try std.testing.expect(!wd.found);
wd = .init();
try wd.writer.writeAll("hello world");
try std.testing.expect(wd.found);
wd = .init();
try wd.writer.writeAll("end ");
try std.testing.expect(wd.found);
wd = .init();
try wd.writer.writeAll(" start");
try std.testing.expect(wd.found);
wd = .init();
try wd.writer.print("value={d}", .{42});
try std.testing.expect(!wd.found);
wd = .init();
try wd.writer.print("value = {d}", .{42});
try std.testing.expect(wd.found);
wd = .init();
try wd.writer.print("value={s}", .{"hello world"});
try std.testing.expect(wd.found);
wd = .init();
try wd.writer.writeByte('\t');
try std.testing.expect(wd.found);
wd = .init();
try wd.writer.writeByte('\n');
try std.testing.expect(wd.found);
5a2fffe99a58b5bbd13860a0a1da0b33a291987b
5a2fffe99a58b5bbd13860a0a1da0b33a291987b
I don't see how using a buffer here could help. There's nothing expensive to amortize because it's a discarding writer. Using a buffer would introduce pointless copies for data that's being discarded. This also means that found won't be accurate until after calling flush. Accepting a buffer here just feels like a footgun to me.
Approved these changes.
Approved these changes.
ajw221
dnut
fieldfunction with mutable pointers.field()and add.fieldFmt()for when you want to customize the formatter. previously this function was confusing to use. ergonomics is very important for code as widely used as a logger. the default formatters will be the right answer in almost every case. i doubt there will be many exceptions, if any.logfunction for when there are no argsfieldandlogin a few places