Struct ContextLogger

Source
pub struct ContextLogger { /* private fields */ }
Expand description

A logger wrapper that enhances log records with contextual properties.

ContextLogger wraps an existing logging implementation and adds additional context properties to log records. These context properties are taken from the current context stack, which is managed by the LogContext type.

§Example

use log::{info, LevelFilter};
use context_logger::{ContextLogger, LogContext};

// Create a logger.
let env_logger = env_logger::builder().build();
let max_level = env_logger.filter();
// Wrap it with ContextLogger to enable context propagation.
let context_logger = ContextLogger::new(env_logger);
// Initialize the resulting logger.
context_logger.init(max_level);

// Create a context with properties
let ctx = LogContext::new()
    .record("request_id", "req-123")
    .record("user_id", 42);

// Use the context while logging
let _guard = ctx.enter();
info!("Processing request"); // Will include request_id and user_id properties

See LogContext for more information on how to create and manage context properties.

Implementations§

Source§

impl ContextLogger

Source

pub fn new<L>(inner: L) -> Self
where L: Log + 'static,

Creates a new ContextLogger that wraps the given logging implementation.

The inner logger will receive log records enhanced with context properties from the current context stack.

Source

pub fn init(self, max_level: LevelFilter)

Initializes the global logger with the context logger.

This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.

§Panics

Panics if a logger has already been set.

Source

pub fn try_init(self, max_level: LevelFilter) -> Result<(), SetLoggerError>

Initializes the global logger with the context logger.

This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.

§Errors

Returns an error if a logger has already been set.

Trait Implementations§

Source§

impl Debug for ContextLogger

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Log for ContextLogger

Source§

fn enabled(&self, metadata: &Metadata<'_>) -> bool

Determines if a log message with the specified metadata would be logged. Read more
Source§

fn log(&self, record: &Record<'_>)

Logs the Record. Read more
Source§

fn flush(&self)

Flushes any buffered records. 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, 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.