-
Notifications
You must be signed in to change notification settings - Fork 12
[wip]feat(log): implement structured logging module #312
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
Draft
GrapeBaBa
wants to merge
2
commits into
main
Choose a base branch
from
gr/feature/logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,219 @@ | ||
| //! Log integration smoke example. | ||
| //! | ||
| //! Demonstrates the structured Logger API, std.log bridge, | ||
| //! caller-owned async pipelines, and all three layout formats. | ||
| //! | ||
| //! Run: | ||
| //! zig build run:log_smoke | ||
| //! | ||
| //! Expected output: structured log lines on stderr from each dispatcher/layout. | ||
|
|
||
| const std = @import("std"); | ||
| const log_mod = @import("log"); | ||
|
|
||
| /// Re-export std_options so std.log calls route through the pipeline. | ||
| pub const std_options = log_mod.std_options; | ||
|
|
||
| const Logger = log_mod.Logger; | ||
| const Attr = log_mod.Attr; | ||
| const Dispatch = log_mod.Dispatch; | ||
| const Dispatcher = log_mod.Dispatcher; | ||
| const LevelFilter = log_mod.LevelFilter; | ||
| const FlushableWriter = log_mod.FlushableWriter; | ||
| const WriterAppend = log_mod.WriterAppend; | ||
| const AsyncAppend = log_mod.AsyncAppend; | ||
| const TextLayout = log_mod.TextLayout; | ||
| const JsonLayout = log_mod.JsonLayout; | ||
| const LogfmtLayout = log_mod.LogfmtLayout; | ||
| const RollingFileWriter = log_mod.RollingFileWriter; | ||
|
|
||
| /// Helper: emit a standard set of log lines through `log`. | ||
| fn emitSample(log: anytype) void { | ||
| log.err("error msg", .{ .code = @as(u64, 500) }); | ||
| log.warn("warn msg", .{}); | ||
| log.info("info msg", .{ .slot = @as(u64, 42) }); | ||
| log.debug("debug msg", .{ .flag = true }); | ||
| } | ||
|
|
||
| pub fn main() !void { | ||
| var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| defer _ = gpa.deinit(); | ||
| const allocator = gpa.allocator(); | ||
|
|
||
| // ── 1. Sync stderr pipeline (explicit TextLayout) ───────────── | ||
|
|
||
| std.debug.print("\n=== 1. Sync stderr pipeline (explicit TextLayout) ===\n", .{}); | ||
| { | ||
| var filter = LevelFilter.init(.debug); | ||
| var sync_appender = WriterAppend(TextLayout).init(allocator, TextLayout{ .color = true }, FlushableWriter.stderrWriter()); | ||
|
|
||
| var d = Dispatch.init(); | ||
| d.addFilter(filter.any()); | ||
| d.addAppend(sync_appender.any()); | ||
|
|
||
| var dispatcher = Dispatcher.init(); | ||
| dispatcher.addDispatch(d); | ||
| log_mod.setGlobalDispatcher(dispatcher.any()); | ||
|
|
||
| const std_log = std.log.scoped(.smoke); | ||
| std_log.err("std.log bridge err", .{}); | ||
| std_log.info("std.log bridge info", .{}); | ||
|
|
||
| const log = Logger(4).init(.smoke, log_mod.getGlobalDispatcher()); | ||
| emitSample(log); | ||
|
|
||
| const child = log.with(&[_]Attr{Attr.str("module", "fc")}); | ||
| child.info("child log", .{ .epoch = @as(u64, 7) }); | ||
|
|
||
| log_mod.resetGlobalDispatcher(); | ||
| } | ||
|
|
||
| // ── 2. Async console pipeline (caller-owned) ──────────── | ||
|
|
||
| std.debug.print("\n=== 2. Async console pipeline (caller-owned) ===\n", .{}); | ||
| { | ||
| var filter = LevelFilter.init(.debug); | ||
| var appender = try AsyncAppend(TextLayout).init( | ||
| allocator, | ||
| 1024, | ||
| FlushableWriter.stderrWriter(), | ||
| TextLayout{ .color = true }, | ||
| .block, | ||
| ); | ||
| try appender.start(); | ||
|
|
||
| var d = Dispatch.init(); | ||
| d.addFilter(filter.any()); | ||
| d.addAppend(appender.any()); | ||
|
|
||
| var dispatcher = Dispatcher.init(); | ||
| dispatcher.addDispatch(d); | ||
| log_mod.setGlobalDispatcher(dispatcher.any()); | ||
|
|
||
| const log = Logger(4).init(.smoke, log_mod.getGlobalDispatcher()); | ||
| emitSample(log); | ||
| log_mod.flushGlobalDispatcher(); | ||
|
|
||
| dispatcher.deinit(); | ||
| log_mod.resetGlobalDispatcher(); | ||
| } | ||
|
|
||
| // ── 3. Async file pipeline (caller-owned) ───── | ||
|
|
||
| std.debug.print("\n=== 3. Async file pipeline (caller-owned) ===\n", .{}); | ||
| { | ||
| var rolling = try RollingFileWriter.init(std.fs.cwd(), "smoke.log", .{ .rotation = .never, .max_bytes = 1024 * 1024 }); | ||
|
|
||
| var filter = LevelFilter.init(.debug); | ||
| var appender = try AsyncAppend(TextLayout).init( | ||
| allocator, | ||
| 1024, | ||
| FlushableWriter.fromFlushable(&rolling), | ||
| TextLayout{}, | ||
| .block, | ||
| ); | ||
| try appender.start(); | ||
|
|
||
| var d = Dispatch.init(); | ||
| d.addFilter(filter.any()); | ||
| d.addAppend(appender.any()); | ||
|
|
||
| var dispatcher = Dispatcher.init(); | ||
| dispatcher.addDispatch(d); | ||
| log_mod.setGlobalDispatcher(dispatcher.any()); | ||
|
|
||
| const log = Logger(4).init(.smoke, log_mod.getGlobalDispatcher()); | ||
| emitSample(log); | ||
| log_mod.flushGlobalDispatcher(); | ||
|
|
||
| dispatcher.deinit(); | ||
| rolling.deinit(); | ||
| log_mod.resetGlobalDispatcher(); | ||
| std.debug.print(" → wrote smoke.log\n", .{}); | ||
| } | ||
|
|
||
| // ── 4. Combined pipeline: console + file (caller-owned) ─ | ||
|
|
||
| std.debug.print("\n=== 4. Combined pipeline: console + file (caller-owned) ===\n", .{}); | ||
| { | ||
| var rolling = try RollingFileWriter.init(std.fs.cwd(), "smoke_combined.log", .{ .rotation = .never, .max_bytes = 1024 * 1024 }); | ||
|
|
||
| var filter = LevelFilter.init(.debug); | ||
| var console_appender = try AsyncAppend(TextLayout).init( | ||
| allocator, | ||
| 1024, | ||
| FlushableWriter.stderrWriter(), | ||
| TextLayout{ .color = true }, | ||
| .block, | ||
| ); | ||
| var file_appender = try AsyncAppend(TextLayout).init( | ||
| allocator, | ||
| 1024, | ||
| FlushableWriter.fromFlushable(&rolling), | ||
| TextLayout{}, | ||
| .block, | ||
| ); | ||
| try console_appender.start(); | ||
| try file_appender.start(); | ||
|
|
||
| var d = Dispatch.init(); | ||
| d.addFilter(filter.any()); | ||
| d.addAppend(console_appender.any()); | ||
| d.addAppend(file_appender.any()); | ||
|
|
||
| var dispatcher = Dispatcher.init(); | ||
| dispatcher.addDispatch(d); | ||
| log_mod.setGlobalDispatcher(dispatcher.any()); | ||
|
|
||
| const log = Logger(4).init(.smoke, log_mod.getGlobalDispatcher()); | ||
| emitSample(log); | ||
| log_mod.flushGlobalDispatcher(); | ||
|
|
||
| dispatcher.deinit(); | ||
| rolling.deinit(); | ||
| log_mod.resetGlobalDispatcher(); | ||
| std.debug.print(" → wrote smoke_combined.log\n", .{}); | ||
| } | ||
|
|
||
| // ── 5. Custom pipeline — JsonLayout (sync stderr) ──────── | ||
|
|
||
| std.debug.print("\n=== 5. JsonLayout (sync stderr) ===\n", .{}); | ||
| { | ||
| var debug_filter = LevelFilter.init(.debug); | ||
| var json_appender = WriterAppend(JsonLayout).init(allocator, JsonLayout{}, FlushableWriter.stderrWriter()); | ||
|
|
||
| var d = Dispatch.init(); | ||
| d.addFilter(debug_filter.any()); | ||
| d.addAppend(json_appender.any()); | ||
|
|
||
| var dispatcher = Dispatcher.init(); | ||
| dispatcher.addDispatch(d); | ||
|
|
||
| const log = Logger(4).init(.smoke, dispatcher.any()); | ||
| emitSample(log); | ||
| } | ||
|
|
||
| // ── 6. Custom pipeline — LogfmtLayout (sync stderr) ────── | ||
|
|
||
| std.debug.print("\n=== 6. LogfmtLayout (sync stderr) ===\n", .{}); | ||
| { | ||
| var debug_filter = LevelFilter.init(.debug); | ||
| var logfmt_appender = WriterAppend(LogfmtLayout).init(allocator, LogfmtLayout{}, FlushableWriter.stderrWriter()); | ||
|
|
||
| var d = Dispatch.init(); | ||
| d.addFilter(debug_filter.any()); | ||
| d.addAppend(logfmt_appender.any()); | ||
|
|
||
| var dispatcher = Dispatcher.init(); | ||
| dispatcher.addDispatch(d); | ||
|
|
||
| const log = Logger(4).init(.smoke, dispatcher.any()); | ||
| emitSample(log); | ||
| } | ||
|
|
||
| // cleanup generated files | ||
| std.fs.cwd().deleteFile("smoke.log") catch {}; | ||
| std.fs.cwd().deleteFile("smoke_combined.log") catch {}; | ||
|
|
||
| std.debug.print("\n=== All 6 configurations OK ===\n", .{}); | ||
| } | ||
Oops, something went wrong.
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.
The errors from
deleteFileare being ignored withcatch {}. While this is an example file, the style guide requires all errors to be handled (Rule 160). Silently ignoring file system errors can hide underlying problems. It would be better to at least print the error to stderr.References
catch {}which silently ignores potential file system errors during cleanup. (link)