pub struct ContextLogger { /* private fields */ }Expand description
A logger wrapper that enhances log records with scope records.
ContextLogger wraps an existing logging implementation and adds additional
scope records to log records. These records are taken from the
current scope stack, which is managed by LogScope.
§Example
use log::{info, LevelFilter};
use context_logger::{ContextLogger, LogContext, LogScope};
// 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()
.with_local_record("request_id", "req-123")
.with_local_record("user_id", 42);
// Use the context while logging
let _guard = LogScope::enter(ctx);
info!("Processing request"); // Will include request_id and user_id recordsSee LogContext for more information on how to create and manage scope records.
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 scope records from the current scope 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<LogValue>,
) -> Self
pub fn default_record( self, key: impl Into<Cow<'static, str>>, value: impl Into<LogValue>, ) -> 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, LogScope};
// 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 = LogScope::enter(LogContext::new()
.with_local_record("request_id", "123"));
info!("Processing request"); // Will include service="api", version="1.0.0", request_id="123"