std::misc::logStructured, leveled logging for Hew programs (slog-style).
import std::misc::log;
Provides two surfaces:
Module-level functions — convenience API using the global level/format
settings, equivalent to Go's log/slog package-level functions.
Logger record — a value that carries its own level and format,
so multiple loggers can coexist with different settings without touching
the global state.
fn main() {
log.setup(); // set global level to INFO
log.info("server starting");
log.info_with("listening", [log.field_int("port", 8080), log.field("env", "prod")]);
log.debug_with("cache miss", [log.field("key", "user:1")]);
}
fn main() {
let lg = log.new_logger(log.DEBUG, log.JSON);
log.logger_info(lg, "startup", [log.field("svc", "auth")]);
log.logger_debug(lg, "trace", [log.field_int("uid", 42)]);
}
Fields are key=value strings built by the helper functions:
log.field(key, val) string valuelog.field_int(key, n) i64 valuelog.field_float(key, f) f64 valuelog.field_bool(key, b) bool valuePass them as a Vec<string> to any *_with or logger_* function.
Empty Vec is valid and equivalent to calling the plain one-arg variant.
ERROR (0) > WARN (1) > INFO (2) > DEBUG (3) > TRACE (4)
TEXT (0) — coloured timestamp + level + message + fields to stderr:
12:34:56.789 INFO server starting port=8080
JSON (1) — single-line NDJSON to stderr:
{"ts":1234567890.123,"level":"INFO","msg":"server starting","port":"8080"}
new_loggerConstruct a Logger with the given level threshold and output format.
level — minimum severity to emit (0=ERROR … 4=TRACE).
format — log.TEXT (0) or log.JSON (1).
logger_levelReturn the level threshold of a Logger.
Because the Logger record's fields are not directly accessible from
outside this module (cross-module record field access is not yet
supported by the MIR), use this accessor when the level needs to be
inspected — for example, in tests.
logger_formatReturn the output format of a Logger.
See logger_level for why an explicit accessor is provided.
fieldBuild a key=value field from two strings.
Values that contain spaces, double-quotes, backslashes, or newlines are
automatically quoted so they roundtrip correctly through the log wire
format. field("user", "John Doe") produces user="John Doe" on
the wire and renders as user=John Doe in TEXT output.
let f = log.field("env", "production"); // "env=production"
let g = log.field("user", "John Doe"); // user="John Doe" (quoted)
field_intBuild a key=N field from a string key and an i64 value.
let f = log.field_int("port", 8080); // "port=8080"
field_floatBuild a key=X field from a string key and an f64 value.
let f = log.field_float("ratio", 0.75); // "ratio=0.75"
field_boolBuild a key=true or key=false field.
let f = log.field_bool("debug", true); // "debug=true"
setupInitialize logging at the default INFO level.
set_levelSet the log level filter.
Levels: 0=error, 1=warn, 2=info, 3=debug, 4=trace.
log.set_level(log.DEBUG);
get_levelGet the current log level filter.
set_formatSet the log output format.
Use log.TEXT (0) for coloured text or log.JSON (1) for NDJSON.
log.set_format(log.JSON);
log.info_with("structured", [log.field("service", "api")]);
get_formatGet the current log output format.
errorLog a message at error level with no structured fields.
error_withLog a message at error level with structured key-value fields.
log.error_with("request failed", [log.field_int("status", 500), log.field("path", "/api")]);
warnLog a message at warn level with no structured fields.
warn_withLog a message at warn level with structured key-value fields.
infoLog a message at info level with no structured fields.
info_withLog a message at info level with structured key-value fields.
log.info_with("server started", [log.field_int("port", 8080)]);
debugLog a message at debug level with no structured fields.
debug_withLog a message at debug level with structured key-value fields.
traceLog a message at trace level with no structured fields.
trace_withLog a message at trace level with structured key-value fields.
logger_errorEmit an error-level message through a Logger with structured fields.
logger_warnEmit a warn-level message through a Logger with structured fields.
logger_infoEmit an info-level message through a Logger with structured fields.
let lg = log.new_logger(log.INFO, log.JSON);
log.logger_info(lg, "ready", [log.field("svc", "auth")]);
logger_debugEmit a debug-level message through a Logger with structured fields.
logger_traceEmit a trace-level message through a Logger with structured fields.
LoggerA logger value with its own level filter and output format.
Construct with log.new_logger(level, format). Pass to logger_*
functions; it is a plain record so it can be stored in actor fields,
passed to functions, or kept in a let binding.
let lg = log.new_logger(log.DEBUG, log.JSON);
log.logger_info(lg, "ready", [log.field_int("port", 8080)]);
ERRORError-level severity (highest priority).
WARNWarning-level severity.
INFOInfo-level severity (default threshold after setup()).
DEBUGDebug-level severity.
TRACETrace-level severity (lowest priority).
TEXTColoured text output format (default).
JSONSingle-line JSON (NDJSON) output format.