pub struct Logger { /* private fields */ }std only.Expand description
The configured logging pipeline.
Build one with Logger::builder. A Logger is cheap to clone and
Send + Sync; share it by cloning or install it process-wide with
crate::init.
§Record assembly
For each admitted record the logger:
- stamps the wall-clock time (unless disabled);
- collects ambient fields: default fields, then the thread’s
context (
crate::context), then any extra context supplied by an integration (for exampletracingspan fields); - removes duplicate keys so every key appears once in the output.
Precedence is call-site fields > extra context > thread context >
defaults, and within call-site fields the last one wins (as in
most JSON parsers). Keys count as duplicates when the JSON and
logfmt formats would write them identically, so a
levelfield (written asfields.level) and afields.levelfield collide. Records written straight to acrate::format::Formatwithout a logger are not de-duplicated; - replaces the values of redacted keys with
REDACTED; - hands the record to every sink in installation order. A failing sink does not stop the others; each failure goes to the error handler;
- flushes every sink if the record is at or above the flush level
(default
Level::Error).
Steps 2-4 use stack buffers for up to 16 fields per group and do not allocate in that case.
§Shutdown
When the last clone is dropped, every sink is shut down (flushed,
background threads drained and joined) and failures are reported
to the error handler. A logger stored in a static is never
dropped: keep the crate::FlushGuard from crate::init or
Logger::flush_guard alive, or call Logger::shutdown.
§Examples
use log_io::{Field, Level, Logger};
let logger = Logger::builder()
.level(Level::Info)
.with_default_field("service", "billing")
.stdout_json()
.build();
logger.log(Level::Info, "ready", &[Field::new("port", 8443_u16)]);
log_io::warn!(logger, "slow query took {} ms", 1280, table = "invoices");Implementations§
Source§impl Logger
impl Logger
Sourcepub fn builder() -> LoggerBuilder
pub fn builder() -> LoggerBuilder
Start configuring a logger. See LoggerBuilder for defaults.
Sourcepub fn threshold_for(&self, target: &str) -> Level
pub fn threshold_for(&self, target: &str) -> Level
The filter threshold that applies to target.
Sourcepub fn enabled(&self, target: &str, level: Level) -> bool
pub fn enabled(&self, target: &str, level: Level) -> bool
Returns true if a record with this target and level would pass
the filter. The macros call this before evaluating any field or
formatting the message.
Sourcepub fn min_level(&self) -> Level
pub fn min_level(&self) -> Level
The lowest severity the filter admits for any target. A record below it is rejected without looking at its target.
Sourcepub fn sink_count(&self) -> usize
pub fn sink_count(&self) -> usize
Number of installed sinks.
Sourcepub fn default_target(&self) -> &str
pub fn default_target(&self) -> &str
The target used by Logger::log (empty unless set with
LoggerBuilder::default_target).
Sourcepub fn log(&self, level: Level, message: &str, fields: &[Field<'_>])
pub fn log(&self, level: Level, message: &str, fields: &[Field<'_>])
Emit a record with the logger’s default target.
Errors are passed to the error handler, not returned. Note that per-target filter rules only apply to this record if they match the default target; the macros use the calling module’s path instead.
Sourcepub fn try_log(
&self,
level: Level,
target: &str,
message: &str,
fields: &[Field<'_>],
) -> Result<()>
pub fn try_log( &self, level: Level, target: &str, message: &str, fields: &[Field<'_>], ) -> Result<()>
Emit a record with an explicit target.
§Errors
Returns the first sink or flush error. Every sink still receives the record, and every error is also passed to the error handler.
Sourcepub fn try_emit(
&self,
metadata: Metadata<'_>,
message: &str,
fields: &[Field<'_>],
) -> Result<()>
pub fn try_emit( &self, metadata: Metadata<'_>, message: &str, fields: &[Field<'_>], ) -> Result<()>
Lowest-level entry point: emit a record with caller-supplied
metadata (for example a source location). The filter uses
metadata.target and metadata.level; a timestamp is added if
timestamps are enabled and metadata has none.
§Errors
Same as Logger::try_log.
Sourcepub fn flush(&self) -> Result<()>
pub fn flush(&self) -> Result<()>
Flush every sink. Records at or above the flush level are already flushed as they are written; call this before a planned exit.
§Errors
Returns the first error. Every sink is still flushed, and every error is also passed to the error handler.
Sourcepub fn shutdown(&self) -> Result<()>
pub fn shutdown(&self) -> Result<()>
Flush every sink and stop background workers, draining their queues first. The logger stays usable afterwards: sinks with a background thread write later records synchronously.
§Errors
Same as Logger::flush.
Sourcepub fn flush_guard(&self) -> FlushGuard
pub fn flush_guard(&self) -> FlushGuard
A guard that calls Logger::shutdown when dropped. Keep it
alive in main when the logger outlives main (for example in
a static).