Skip to main content

Logger

Struct Logger 

Source
pub struct Logger { /* private fields */ }
Available on crate feature 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:

  1. stamps the wall-clock time (unless disabled);
  2. collects ambient fields: default fields, then the thread’s context (crate::context), then any extra context supplied by an integration (for example tracing span fields);
  3. 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 level field (written as fields.level) and a fields.level field collide. Records written straight to a crate::format::Format without a logger are not de-duplicated;
  4. replaces the values of redacted keys with REDACTED;
  5. hands the record to every sink in installation order. A failing sink does not stop the others; each failure goes to the error handler;
  6. 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

Source

pub fn builder() -> LoggerBuilder

Start configuring a logger. See LoggerBuilder for defaults.

Source

pub fn threshold_for(&self, target: &str) -> Level

The filter threshold that applies to target.

Source

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.

Source

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.

Source

pub fn filter(&self) -> &Filter

The filter in use.

Source

pub fn sink_count(&self) -> usize

Number of installed sinks.

Source

pub fn default_target(&self) -> &str

The target used by Logger::log (empty unless set with LoggerBuilder::default_target).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Trait Implementations§

Source§

impl Clone for Logger

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Logger

Source§

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

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

impl RefUnwindSafe for Logger

Source§

impl UnwindSafe for Logger

Auto Trait Implementations§

§

impl Forget for Logger

§

impl Freeze for Logger

§

impl Move for Logger

§

impl Send for Logger

§

impl Sync for Logger

§

impl Unpin for Logger

§

impl UnsafeUnpin for Logger

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.