Struct Formatter

pub struct Formatter { /* private fields */ }

A formatter to write logs into.

Formatter implements the standard Write trait for writing log records. It also supports terminal styling using ANSI escape codes.

Examples

Use the writeln macro to format a log record. An instance of a Formatter is passed to an env_logger format as buf:

use std::io::Write;

let mut builder = env_logger::Builder::new();

builder.format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args()));

Implementations

impl Formatter

fn default_level_style(&self, level: Level) -> Style

Get the default style::Style for the given level.

The style can be used to print other values besides the level.

See style for how to adapt it to the styling crate of your choice

impl Formatter

fn timestamp(&self) -> Timestamp

Get a Timestamp for the current date and time in UTC.

Examples

Include the current timestamp with the log record:

use std::io::Write;

let mut builder = env_logger::Builder::new();

builder.format(|buf, record| {
    let ts = buf.timestamp();

    writeln!(buf, "{}: {}: {}", ts, record.level(), record.args())
});
fn timestamp_seconds(&self) -> Timestamp

Get a Timestamp for the current date and time in UTC with full second precision.

fn timestamp_millis(&self) -> Timestamp

Get a Timestamp for the current date and time in UTC with millisecond precision.

fn timestamp_micros(&self) -> Timestamp

Get a Timestamp for the current date and time in UTC with microsecond precision.

fn timestamp_nanos(&self) -> Timestamp

Get a Timestamp for the current date and time in UTC with nanosecond precision.

Trait Implementations

impl Debug for Formatter

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

impl Write for Formatter

fn write(&mut self, buf: &[u8]) -> Result<usize>
fn flush(&mut self) -> Result<()>

Auto Trait Implementations

impl !RefUnwindSafe for Formatter

impl !Send for Formatter

impl !Sync for Formatter

impl !UnwindSafe for Formatter

impl Freeze for Formatter

impl Unpin for Formatter

impl UnsafeUnpin for Formatter

Blanket Implementations

impl<T> Any for Formatter where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Formatter where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Formatter where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for Formatter

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for Formatter where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Formatter where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>