Skip to main content

ConsoleLogger

Struct ConsoleLogger 

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

Logger implementation that writes log records to the console.

This implementation of the Logger trait writes log records (Record) to the console using the provided log::Level. Log records with the Error kind ignore the provided log::Level and are always written with log::Level::Error.

Optionally, a prefix can be configured via with_prefix or set_prefix. When set, it is printed verbatim at the beginning of every log line, before the record kind character. This is useful to disambiguate output when several LoggedStreams (for example one per connection) log to the same console. No prefix is configured by default.

Implementations§

Source§

impl ConsoleLogger

Source

pub fn new(level: &str) -> Result<Self, ParseLevelError>

Construct a new instance of ConsoleLogger using the provided log level str. Returns an Err if the provided log level is invalid. The constructed logger has no prefix; use with_prefix or set_prefix to add one.

Source

pub fn new_unchecked(level: &str) -> Self

Construct a new instance of ConsoleLogger using the provided log level str. Panics if the provided log level is invalid.

Source

pub fn with_prefix(self, prefix: impl Into<Cow<'static, str>>) -> Self

Set a prefix that will be printed at the beginning of every log line produced by this logger, and return the modified logger. This is a chainable builder method.

The prefix is rendered verbatim immediately before the record kind character — no separator is inserted between them — so include any trailing separator you want yourself (for example a trailing space or brackets). An empty prefix therefore produces the same output as no prefix at all.

§Examples
use logged_stream::ConsoleLogger;

let logger = ConsoleLogger::new_unchecked("debug").with_prefix("[conn 5] ");
assert_eq!(logger.prefix(), Some("[conn 5] "));
Source

pub fn set_prefix(&mut self, prefix: impl Into<Cow<'static, str>>)

Set or replace the prefix printed at the beginning of every log line produced by this logger, in place. See with_prefix for details on how the prefix is rendered.

Source

pub fn clear_prefix(&mut self)

Remove the configured prefix, so log lines are printed without any leading prefix again.

Source

pub fn prefix(&self) -> Option<&str>

Return the currently configured prefix, or None if no prefix is set.

Trait Implementations§

Source§

impl Clone for ConsoleLogger

Source§

fn clone(&self) -> ConsoleLogger

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 ConsoleLogger

Source§

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

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

impl Logger for ConsoleLogger

Source§

fn log(&mut self, record: Record)

Source§

impl Logger for Box<ConsoleLogger>

Source§

fn log(&mut self, record: Record)

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