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;
let _guard = Logger::new().init().unwrap();
tiny_tracing::info!("Ready");JSON output with environment filter:
use tiny_tracing::{Logger, LogFormat, Level};
let _guard = 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, timestamps on, output to stdout, colours
chosen automatically.
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.
Examples found in repository?
More examples
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12 let _guard = Logger::new()
13 .with_level(Level::DEBUG)
14 .with_env_filter_from_env()
15 .init()?;
16
17 warn!("warn line — visible unless RUST_LOG is set to error");
18 info!("info line — visible at INFO or lower");
19 debug!("debug line — visible by default, hidden by RUST_LOG=info");
20 trace!("trace line — needs RUST_LOG=trace");
21 Ok(())
22}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_from_env(self) -> Self
pub fn with_env_filter_from_env(self) -> Self
Reads the filter directives from the RUST_LOG environment variable.
When RUST_LOG is set (and valid UTF-8) it is used exactly as if it had
been passed to with_env_filter, replacing any
filter set earlier. When it is not set, nothing changes and only the
with_level value (plus any earlier env filter)
applies. A RUST_LOG that EnvFilter rejects makes
init return LoggerError::InvalidEnvFilter.
The precedence rules of with_level apply: a global
directive inside RUST_LOG (e.g. RUST_LOG=warn) takes precedence over
the configured level.
Examples found in repository?
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12 let _guard = Logger::new()
13 .with_level(Level::DEBUG)
14 .with_env_filter_from_env()
15 .init()?;
16
17 warn!("warn line — visible unless RUST_LOG is set to error");
18 info!("info line — visible at INFO or lower");
19 debug!("debug line — visible by default, hidden by RUST_LOG=info");
20 trace!("trace line — needs RUST_LOG=trace");
21 Ok(())
22}Sourcepub fn colored(self, colored: bool) -> Self
pub fn colored(self, colored: bool) -> Self
Forces ANSI colours on or off for stdout or stderr.
By default colours are chosen automatically (on only when the stream being written to, stdout or stderr, is a terminal). File output is never coloured, regardless of this setting.
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.
To take the directives from RUST_LOG instead of passing them in code, use
with_env_filter_from_env.
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_timestamp(self, enabled: bool) -> Self
pub fn with_timestamp(self, enabled: bool) -> Self
Controls whether each log line starts with a timestamp.
Enabled by default. When on, timestamps are always UTC and formatted as
RFC 3339 with a sub-second fraction (e.g. 2026-10-01T12:00:00.123456Z),
in both text and JSON output. Trailing zeros of the fraction are trimmed,
so its length varies. Turn it off when something else already adds
one, such as journald or a container runtime.
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, stderr, a file, or stdout and a file.
Defaults to Output::Stdout. When a file is involved it is opened in
append mode (created if missing), so the call stays panic-free — an
unopenable path yields LoggerError::OpenLogFile from
init.
File writes are non-blocking: lines are queued and written by a
background thread owned by the LoggerGuard that
init returns, which must be kept alive. The queue holds
up to 128 000 lines and no line is ever dropped; if it fills up, the
logging call waits for room.
Writes to stdout and stderr are synchronous: each log line is written on the calling thread, which waits until the write finishes.
Examples found in repository?
More examples
Sourcepub fn init(self) -> Result<LoggerGuard, LoggerError>
pub fn init(self) -> Result<LoggerGuard, LoggerError>
Initializes the global tracing subscriber.
Consumes the builder. Must be called only once per process;
subsequent calls return LoggerError::TryInitError.
Returns a LoggerGuard that must be kept alive for as long as logging
is needed: dropping it flushes and stops the background file writer.
§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.