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
impl ContextLogger
Sourcepub fn new<L>(inner: L) -> Selfwhere
L: Log + 'static,
pub fn new<L>(inner: L) -> Selfwhere
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.
Sourcepub fn init(self, max_level: LevelFilter)
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.
Sourcepub fn try_init(self, max_level: LevelFilter) -> Result<(), SetLoggerError>
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.
Sourcepub fn default_record(
self,
key: impl Into<Cow<'static, str>>,
value: impl Into<ContextValue>,
) -> Self
pub fn default_record( self, key: impl Into<Cow<'static, str>>, value: impl Into<ContextValue>, ) -> Self
Adds a default record that will be included in all log entries.
Default records are automatically added to all log entries, regardless of the current context. They are defined when the logger is created and remain constant throughout the application’s lifetime.
§Behavior with Duplicate Keys
When logging, default records are added first, followed by records from the current context. If multiple records with the same key exist, the behavior depends on the underlying logger implementation. In most implementations, later records with the same key will typically replace earlier ones.
§Example
use log::{info, LevelFilter};
use context_logger::{ContextLogger, LogContext};
// Create a logger with default records
let logger = ContextLogger::new(env_logger::builder().build())
.default_record("service", "api")
.default_record("version", "1.0.0");
// Initialize it
logger.init(LevelFilter::Info);
// Context records are added after default records
let _guard = LogContext::new()
.record("request_id", "123")
.enter();
info!("Processing request"); // Will include service="api", version="1.0.0", request_id="123"