Expand description
§ngy-utils-tracing
A logging initialization utility library built on tracing, which automatically selects the
log output based on the “application mode” (AppMode):
Development: Console logging to stderr (pretty format, with file name, line number and thread ids)Test: Console to stderr (pretty) + File (JSON)Production: File logging (JSON; one file per day, named after the date in the configured offset, seeInitOptions::time_offset)
init performs one-time initialization of dotenv and tracing; repeated calls within the
process return a clear error. After initialization, use get_current_mode to query the
current mode.
Every environment variable (log directory, application mode, timestamp offset, …) is read with
the required prefix passed to InitOptions::new, so an unrelated program that happens to use
LOG_DIR or APP_MODE cannot change this crate’s behaviour. RUST_LOG is the one exception:
it keeps its unprefixed name because the whole Rust logging ecosystem shares it.
See the README for mode descriptions and the full API.
Structs§
- Init
Options - Configuration for
init. - Init
Result - Initialization result, containing the guard that must stay alive
Enums§
- AppMode
- Application run mode
Constants§
- DEFAULT_
LOG_ DIR - Default log directory, used when
LOG_DIRis unset. - DEFAULT_
LOG_ LEVEL - Default log level, applied when nothing more specific is configured.
- DEFAULT_
LOG_ PREFIX - Default log file prefix, used when
LOG_PREFIXis unset. - DEFAULT_
MAX_ LOG_ FILES - Default number of retained log files, used when
LOG_MAX_FILESis unset; older files beyond this are deleted when the writer rolls over to a new day. - DEFAULT_
TIME_ OFFSET - Last-resort UTC offset used for log timestamps, applied by both the console and the file layer.
Functions§
- build_
debug_ filter - Build the project-level log filter
- build_
file_ filter - Build the
EnvFilter, readingRUST_LOGfirst and otherwise falling back todefault_directive(callers passcrate::DEFAULT_LOG_LEVEL). - cleanup_
old_ logs - Delete old log files in
log_dir, keeping only the most recentmax_files. - console_
tracing - Initialize console logging: pretty format output to stderr.
- file_
tracing - Initialize file logging: JSON format output to the log directory.
- get_
current_ mode - Get the current application mode
- init
- Initialize dotenv and tracing
- resolve_
log_ dir - Build the log directory path
- resolve_
log_ prefix - Build the log file prefix
- resolve_
max_ log_ files - Resolve the maximum number of retained log files.
- test_
tracing - Initialize test mode logging: outputs to both console and file.