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
impl ConsoleLogger
Sourcepub fn new(level: &str) -> Result<Self, ParseLevelError>
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.
Sourcepub fn new_unchecked(level: &str) -> Self
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.
Sourcepub fn with_prefix(self, prefix: impl Into<Cow<'static, str>>) -> Self
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] "));Sourcepub fn set_prefix(&mut self, prefix: impl Into<Cow<'static, str>>)
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.
Sourcepub fn clear_prefix(&mut self)
pub fn clear_prefix(&mut self)
Remove the configured prefix, so log lines are printed without any leading prefix again.
Trait Implementations§
Source§impl Clone for ConsoleLogger
impl Clone for ConsoleLogger
Source§fn clone(&self) -> ConsoleLogger
fn clone(&self) -> ConsoleLogger
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more