Skip to content

remove the "scope" parameter from std.log functions; introduce "std.log.scoped" functions #5943

Description

@andrewrk

Accepted Proposal


In effort to make logging more mainstream and ergonomic, this is a proposal to simplify the logging API, yet still provide the advanced functionality.

It's difficult to justify the "scope" parameter, for example, in Hello World. Here I propose to do this:

diff --git a/lib/std/log.zig b/lib/std/log.zig
index d8bcba38c..3bd62e3c0 100644
--- a/lib/std/log.zig+++ b/lib/std/log.zig@@ -90,6 +90,8 @@ pub const default_level: Level = switch (builtin.mode) {
.ReleaseSmall => .emerg,
};
+const default_scope = .main;+
/// The current log level. This is set to root.log_level if present, otherwise
/// log.default_level.
pub const level: Level = if (@hasDecl(root, "log_level"))
@@ -117,7 +119,7 @@ fn log(
/// Log an emergency message to stderr. This log level is intended to be used
/// for conditions that cannot be handled and is usually followed by a panic.
-pub fn emerg(+pub fn emergScope(
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
@@ -126,9 +128,16 @@ pub fn emerg(
log(.emerg, scope, format, args);
}
+/// Log an emergency message to stderr. This log level is intended to be used+/// for conditions that cannot be handled and is usually followed by a panic.+pub fn emerg(comptime format: []const u8, args: anytype) void {+ @setCold(true);+ log(.emerg, default_scope, format, args);+}+
/// Log an alert message to stderr. This log level is intended to be used for
/// conditions that should be corrected immediately (e.g. database corruption).
-pub fn alert(+pub fn alertScope(
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
@@ -137,10 +146,17 @@ pub fn alert(
log(.alert, scope, format, args);
}
+/// Log an alert message to stderr. This log level is intended to be used for+/// conditions that should be corrected immediately (e.g. database corruption).+pub fn alert(comptime format: []const u8, args: anytype) void {+ @setCold(true);+ log(.alert, default_scope, format, args);+}+
/// Log a critical message to stderr. This log level is intended to be used
/// when a bug has been detected or something has gone wrong and it will have
/// an effect on the operation of the program.
-pub fn crit(+pub fn critScope(
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
@@ -149,9 +165,17 @@ pub fn crit(
log(.crit, scope, format, args);
}
+/// Log a critical message to stderr. This log level is intended to be used+/// when a bug has been detected or something has gone wrong and it will have+/// an effect on the operation of the program.+pub fn crit(comptime format: []const u8, args: anytype) void {+ @setCold(true);+ log(.crit, default_scope, format, args);+}+
/// Log an error message to stderr. This log level is intended to be used when
/// a bug has been detected or something has gone wrong but it is recoverable.
-pub fn err(+pub fn errScope(
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
@@ -160,10 +184,17 @@ pub fn err(
log(.err, scope, format, args);
}
+/// Log an error message to stderr. This log level is intended to be used when+/// a bug has been detected or something has gone wrong but it is recoverable.+pub fn err(comptime format: []const u8, args: anytype) void {+ @setCold(true);+ log(.err, default_scope, format, args);+}+
/// Log a warning message to stderr. This log level is intended to be used if
/// it is uncertain whether something has gone wrong or not, but the
/// circumstances would be worth investigating.
-pub fn warn(+pub fn warnScope(
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
@@ -171,9 +202,16 @@ pub fn warn(
log(.warn, scope, format, args);
}
+/// Log a warning message to stderr. This log level is intended to be used if+/// it is uncertain whether something has gone wrong or not, but the+/// circumstances would be worth investigating.+pub fn warn(comptime format: []const u8, args: anytype) void {+ log(.warn, default_scope, format, args);+}+
/// Log a notice message to stderr. This log level is intended to be used for
/// non-error but significant conditions.
-pub fn notice(+pub fn noticeScope(
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
@@ -181,9 +219,15 @@ pub fn notice(
log(.notice, scope, format, args);
}
+/// Log a notice message to stderr. This log level is intended to be used for+/// non-error but significant conditions.+pub fn notice(comptime format: []const u8, args: anytype) void {+ log(.notice, default_scope, format, args);+}+
/// Log an info message to stderr. This log level is intended to be used for
/// general messages about the state of the program.
-pub fn info(+pub fn infoScope(
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
@@ -191,12 +235,24 @@ pub fn info(
log(.info, scope, format, args);
}
+/// Log an info message to stderr. This log level is intended to be used for+/// general messages about the state of the program.+pub fn info(comptime format: []const u8, args: anytype) void {+ log(.info, default_scope, format, args);+}+
/// Log a debug message to stderr. This log level is intended to be used for
/// messages which are only useful for debugging.
-pub fn debug(+pub fn debugScope(
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
log(.debug, scope, format, args);
}
++/// Log a debug message to stderr. This log level is intended to be used for+/// messages which are only useful for debugging.+pub fn debug(comptime format: []const u8, args: anytype) void {+ log(.debug, default_scope, format, args);+}

Hello World becomes:

conststd=@import("std");
pubfnmain() void {
std.log.info("application start: {}", .{"Hello, World!"});
}

A potential future improvement would be if we had package-local configurations and therefore could swap out the default log scope per-package. So packages that didn't need multiple scopes would be able to use the simpler log functions, and then get assigned an optional scope override by the application that used the package.

cc @ifreund

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.breakingImplementing this issue could cause existing code to no longer compile or have different behavior.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions