std.log: simplify to 4 distinct log levels

Over the last year of using std.log in practice, it has become clear to
me that having the current 8 distinct log levels does more harm than
good. It is too subjective which level a given message should have which
makes filtering based on log level weaker as not all messages will have
been assigned the log level one might expect.

Instead, more granular filtering should be achieved by leveraging the
logging scope feature. Filtering based on a combination of scope and log
level should be sufficiently powerful for all use-cases.

Note that the self hosted compiler has already limited itself to 4
distinct log levels for many months and implemented granular filtering
based on both log scope and level. This has worked very well in practice
while working on the self hosted compiler.
This commit is contained in:
Isaac Freund
2021-10-24 13:13:06 +02:00
committed by Andrew Kelley
parent 6cf5305e47
commit f7b090d707
5 changed files with 59 additions and 138 deletions

View File

@@ -435,8 +435,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\pub const log_level: std.log.Level = .debug;
\\
\\pub const scope_levels = [_]std.log.ScopeLevel{
\\ .{ .scope = .a, .level = .alert },
\\ .{ .scope = .c, .level = .emerg },
\\ .{ .scope = .a, .level = .warn },
\\ .{ .scope = .c, .level = .err },
\\};
\\
\\const loga = std.log.scoped(.a);
@@ -452,10 +452,6 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ logb.info("", .{});
\\ logc.info("", .{});
\\
\\ loga.notice("", .{});
\\ logb.notice("", .{});
\\ logc.notice("", .{});
\\
\\ loga.warn("", .{});
\\ logb.warn("", .{});
\\ logc.warn("", .{});
@@ -463,18 +459,6 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ loga.err("", .{});
\\ logb.err("", .{});
\\ logc.err("", .{});
\\
\\ loga.crit("", .{});
\\ logb.crit("", .{});
\\ logc.crit("", .{});
\\
\\ loga.alert("", .{});
\\ logb.alert("", .{});
\\ logc.alert("", .{});
\\
\\ loga.emerg("", .{});
\\ logb.emerg("", .{});
\\ logc.emerg("", .{});
\\}
\\pub fn log(
\\ comptime level: std.log.Level,
@@ -483,22 +467,18 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ args: anytype,
\\) void {
\\ const level_txt = comptime level.asText();
\\ const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
\\ const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "):";
\\ const stdout = std.io.getStdOut().writer();
\\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
\\}
,
\\debug(b):
\\info(b):
\\notice(b):
\\warning(b):
\\error(b):
\\critical(b):
\\alert(a):
\\alert(b):
\\emergency(a):
\\emergency(b):
\\emergency(c):
\\debug(b):
\\info(b):
\\warning(a):
\\warning(b):
\\error(a):
\\error(b):
\\error(c):
\\
);
@@ -534,7 +514,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
,
\\debug: alloc - success - len: 10, ptr_align: 1, len_align: 0
\\debug: shrink - success - 10 to 5, len_align: 0, buf_align: 1
\\critical: expand - failure: OutOfMemory - 5 to 20, len_align: 0, buf_align: 1
\\error: expand - failure: OutOfMemory - 5 to 20, len_align: 0, buf_align: 1
\\debug: free - success - len: 5
\\
);