Struct Formatter

struct Formatter { ... }

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: &Self, level: Level) -> style::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 crate::fmt::Formatter

fn timestamp(self: &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: &Self) -> Timestamp

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

fn timestamp_millis(self: &Self) -> Timestamp

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

fn timestamp_micros(self: &Self) -> Timestamp

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

fn timestamp_nanos(self: &Self) -> Timestamp

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

impl Debug for Formatter

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

impl Freeze for Formatter

impl RefUnwindSafe for Formatter

impl Send for Formatter

impl Sync for Formatter

impl Unpin for Formatter

impl UnwindSafe for Formatter

impl Write for Formatter

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

impl<T> Any for Formatter

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Formatter

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Formatter

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

impl<T> From for Formatter

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Formatter

fn into(self: 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 for Formatter

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Formatter

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