Skip to main content

Crate ngy_utils_tracing

Crate ngy_utils_tracing 

Source
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, see InitOptions::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§

InitOptions
Configuration for init.
InitResult
Initialization result, containing the guard that must stay alive

Enums§

AppMode
Application run mode

Constants§

DEFAULT_LOG_DIR
Default log directory, used when LOG_DIR is unset.
DEFAULT_LOG_LEVEL
Default log level, applied when nothing more specific is configured.
DEFAULT_LOG_PREFIX
Default log file prefix, used when LOG_PREFIX is unset.
DEFAULT_MAX_LOG_FILES
Default number of retained log files, used when LOG_MAX_FILES is 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, reading RUST_LOG first and otherwise falling back to default_directive (callers pass crate::DEFAULT_LOG_LEVEL).
cleanup_old_logs
Delete old log files in log_dir, keeping only the most recent max_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.