Skip to main content

Logger

Struct Logger 

Source
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

Source

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?
examples/basic.rs (line 8)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    Logger::new().init()?;
9
10    info!("hello from tiny-tracing");
11    Ok(())
12}
More examples
Hide additional examples
examples/file.rs (line 11)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    Logger::new()
12        .with_output(Output::Both("app.log".into()))
13        .init()?;
14
15    info!("this line goes to both stdout and app.log");
16    warn!(file = "app.log", "check the file for a colour-free copy");
17    Ok(())
18}
examples/json.rs (line 8)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    Logger::new()
9        .with_level(Level::DEBUG)
10        .with_format(LogFormat::Json)
11        .with_file(true)
12        .init()?;
13
14    info!(user_id = 42, action = "login", "structured event");
15    warn!("something worth a second look");
16    Ok(())
17}
examples/env_filter.rs (line 11)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "info,env_filter=debug".to_string());
10
11    Logger::new().with_env_filter(filter).init()?;
12
13    info!("info line — always visible");
14    debug!("debug line — visible when this crate is at DEBUG or lower");
15    trace!("trace line — needs RUST_LOG=trace or similar");
16    Ok(())
17}
Source

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?
examples/json.rs (line 9)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    Logger::new()
9        .with_level(Level::DEBUG)
10        .with_format(LogFormat::Json)
11        .with_file(true)
12        .init()?;
13
14    info!(user_id = 42, action = "login", "structured event");
15    warn!("something worth a second look");
16    Ok(())
17}
Source

pub fn with_format(self, format: LogFormat) -> Self

Sets the output format.

Examples found in repository?
examples/json.rs (line 10)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    Logger::new()
9        .with_level(Level::DEBUG)
10        .with_format(LogFormat::Json)
11        .with_file(true)
12        .init()?;
13
14    info!(user_id = 42, action = "login", "structured event");
15    warn!("something worth a second look");
16    Ok(())
17}
Source

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.

Examples found in repository?
examples/env_filter.rs (line 11)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "info,env_filter=debug".to_string());
10
11    Logger::new().with_env_filter(filter).init()?;
12
13    info!("info line — always visible");
14    debug!("debug line — visible when this crate is at DEBUG or lower");
15    trace!("trace line — needs RUST_LOG=trace or similar");
16    Ok(())
17}
Source

pub fn with_file(self, enabled: bool) -> Self

Controls whether the source file name appears in log lines.

Disabled by default.

Examples found in repository?
examples/json.rs (line 11)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    Logger::new()
9        .with_level(Level::DEBUG)
10        .with_format(LogFormat::Json)
11        .with_file(true)
12        .init()?;
13
14    info!(user_id = 42, action = "login", "structured event");
15    warn!("something worth a second look");
16    Ok(())
17}
Source

pub fn with_target(self, enabled: bool) -> Self

Controls whether the module path (target) appears in log lines.

Enabled by default.

Source

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.

Examples found in repository?
examples/file.rs (line 12)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    Logger::new()
12        .with_output(Output::Both("app.log".into()))
13        .init()?;
14
15    info!("this line goes to both stdout and app.log");
16    warn!(file = "app.log", "check the file for a colour-free copy");
17    Ok(())
18}
Source

pub fn level(&self) -> Level

Returns the configured log level.

Source

pub fn format(&self) -> LogFormat

Returns the configured output format.

Source

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
Examples found in repository?
examples/basic.rs (line 8)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    Logger::new().init()?;
9
10    info!("hello from tiny-tracing");
11    Ok(())
12}
More examples
Hide additional examples
examples/file.rs (line 13)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    Logger::new()
12        .with_output(Output::Both("app.log".into()))
13        .init()?;
14
15    info!("this line goes to both stdout and app.log");
16    warn!(file = "app.log", "check the file for a colour-free copy");
17    Ok(())
18}
examples/json.rs (line 12)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    Logger::new()
9        .with_level(Level::DEBUG)
10        .with_format(LogFormat::Json)
11        .with_file(true)
12        .init()?;
13
14    info!(user_id = 42, action = "login", "structured event");
15    warn!("something worth a second look");
16    Ok(())
17}
examples/env_filter.rs (line 11)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "info,env_filter=debug".to_string());
10
11    Logger::new().with_env_filter(filter).init()?;
12
13    info!("info line — always visible");
14    debug!("debug line — visible when this crate is at DEBUG or lower");
15    trace!("trace line — needs RUST_LOG=trace or similar");
16    Ok(())
17}

Trait Implementations§

Source§

impl Default for Logger

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more