pub struct Logger { /* private fields */ }Expand description
Builder for initializing the global tracing subscriber.
Uses a fluent builder pattern to configure log level, output format,
and optional environment-based filtering before calling init.
§Examples
Minimal setup (text output at INFO level):
use tiny_tracing::Logger;
Logger::new().init().unwrap();
tiny_tracing::info!("Ready");JSON output with environment filter:
use tiny_tracing::{Logger, LogFormat, Level};
Logger::new()
.with_level(Level::DEBUG)
.with_format(LogFormat::Json)
.with_env_filter("info,my_crate=trace")
.init()
.unwrap();Implementations§
Source§impl Logger
impl Logger
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Logger with default values.
Defaults: Level::INFO, LogFormat::Text, no env filter,
file location off, target on, output to stdout.
Examples found in repository?
More examples
Sourcepub fn with_level(self, level: Level) -> Self
pub fn with_level(self, level: Level) -> Self
Sets the global (default) log level.
This is the base level applied to every target. Per-target directives
passed to with_env_filter refine it on top —
the level is never silently ignored. A global directive inside the env
filter string (e.g. the info in "info,my_crate=debug") does take
precedence over this value.
Sourcepub fn with_format(self, format: LogFormat) -> Self
pub fn with_format(self, format: LogFormat) -> Self
Sets the output format.
Sourcepub fn with_env_filter(self, filter: impl Into<String>) -> Self
pub fn with_env_filter(self, filter: impl Into<String>) -> Self
Adds environment-based, per-target filtering via EnvFilter.
The directives here are layered on top of the global
with_level value. Supported syntax: "info",
"info,my_crate=debug", etc.
If not called, only the static with_level value applies.
Sourcepub fn with_file(self, enabled: bool) -> Self
pub fn with_file(self, enabled: bool) -> Self
Controls whether the source file name appears in log lines.
Disabled by default.
Sourcepub fn with_target(self, enabled: bool) -> Self
pub fn with_target(self, enabled: bool) -> Self
Controls whether the module path (target) appears in log lines.
Enabled by default.
Sourcepub fn with_output(self, output: Output) -> Self
pub fn with_output(self, output: Output) -> Self
Sets where log lines are written: stdout, a file, or both.
Defaults to Output::Stdout. When a file is involved it is opened in
append mode (created if missing) and writes are synchronised, so the
call stays panic-free — an unopenable path yields
LoggerError::OpenLogFile from init.
Sourcepub fn init(self) -> Result<(), LoggerError>
pub fn init(self) -> Result<(), LoggerError>
Initializes the global tracing subscriber.
Consumes the builder. Must be called only once per process;
subsequent calls return LoggerError::TryInitError.
§Errors
LoggerError::InvalidEnvFilterif an env filter was set andEnvFilterrejects it.LoggerError::OpenLogFileif a file output was requested but the path could not be opened for writing.LoggerError::TryInitErrorif a global subscriber is already set.