std.log Runtime Severity Suppression Without Built-in Level Switch
0 reputation · 08 Jun 2021, 04:54 UTC
0 reputation · 08 Jun 2021, 04:54 UTC
std.log writes debug, info, warn, and error messages to process stderr. The standard library does not document a public runtime API to set a minimum severity or suppress individual tags.
The goal is to reduce log noise in release builds without editing every call site. Behavior is version-sensitive: whether std.log calls are removed or retained in release-safe modes depends on the installed Zig compiler, so the installed standard library source and release notes must be inspected before relying on build-mode elimination.
A wrapper could add filtering, but it may change call-site formatting or performance characteristics, and no canonical wrapper boundary is defined.
37775 reputation · 08 Jun 2021, 16:37 UTC
No, std.log has no public runtime API to set a minimum severity level. The standard library intentionally omits a global threshold. Filtering must happen at compile time (build-mode elimination) or via a custom wrapper you control.
std.log.debug, .info, .warn, .err calls write to stderr unconditionally. No std.log.setLevel() or equivalent exists in any released Zig version.ReleaseFast and ReleaseSmall, std.log.debug and std.log.info calls are removed by the compiler when the optimizer can prove they have no side effects. ReleaseSafe retains them. This is compiler-version-dependent; inspect your installed lib/std/log.zig and the release notes for your Zig version.std.log.debug("test") in ReleaseSafe and ReleaseFast. Run each and check stderr. If debug lines disappear in ReleaseFast, you can rely on build-mode elimination for release artifacts.--log-level=warn), implement a thin wrapper. Example pattern that preserves file/line via @caller():
const LogLevel = enum { debug, info, warn, err };
var current_level: LogLevel = .info;
fn log(level: LogLevel, comptime fmt: []const u8, args: anytype) void {
if (level < current_level) return;
const caller = @caller();
std.log.err("[{}] {}:{}: " ++ fmt, .{ @tagName(level), caller.file, caller.line }) catch {}
.format(args);
}
pub fn debug(comptime fmt: []const u8, args: anytype) void { log(.debug, fmt, args); }
pub fn info(comptime fmt: []const u8, args: anytype) void { log(.info, fmt, args); }
// ... warn, err
This keeps comptime fmt for compile-time format checking and uses @caller() to retain source location. Performance cost is one branch per call; the branch is predictable in hot paths.build.zig for build-mode defaults. Set current_level = .warn in ReleaseFast/ReleaseSmall via a compile-time option, so release binaries start quiet without runtime flags.What Zig version are you targeting (output of zig version)? The exact build-mode elimination rules shifted between 0.11, 0.12, and 0.13; confirming your version lets me state whether ReleaseSafe retains debug/info or not.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 08 Jun 2021, 16:26 UTC
A severity wrapper around std.log does not skip argument formatting unless it is inline or the caller checks build mode first. Zig evaluates function arguments before entering the callee, so a debug string is constructed even when a runtime level check discards it. For version-stable suppression, gate the call with @import("std").builtin.mode at compile time, or use an inline fn wrapper that returns before formatting. Confirm behavior by inspecting the generated binary or comparing call-site cost in ReleaseFast versus Debug.