[][src]Struct env_logger::fmt::Formatter

pub struct Formatter { /* fields omitted */ }

A formatter to write logs into.

Formatter implements the standard Write trait for writing log records. It also supports terminal colors, through the style method.

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()));

Methods

impl Formatter[src]

pub fn style(&self) -> Style[src]

Begin a new Style.

Examples

Create a bold, red colored style and use it to print the log level:

use std::io::Write;
use env_logger::fmt::Color;

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

builder.format(|buf, record| {
    let mut level_style = buf.style();

    level_style.set_color(Color::Red).set_bold(true);

    writeln!(buf, "{}: {}",
        level_style.value(record.level()),
        record.args())
});

pub fn default_level_style(&self, level: Level) -> Style[src]

Get the default Style for the given level.

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

pub fn default_styled_level(&self, level: Level) -> StyledValue<'static, Level>[src]

Get a printable Style for the given level.

The style can only be used to print the level.

impl Formatter[src]

pub fn timestamp(&self) -> Timestamp[src]

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())
});

pub fn precise_timestamp(&self) -> PreciseTimestamp[src]

Get a PreciseTimestamp for the current date and time in UTC with nanos.

Trait Implementations

impl Debug for Formatter[src]

impl Write for Formatter[src]

fn write_vectored(&mut self, bufs: &[IoVec]) -> Result<usize, Error>[src]

🔬 This is a nightly-only experimental API. (iovec)

Like write, except that it writes from a slice of buffers. Read more

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0
[src]

Attempts to write an entire buffer into this writer. Read more

fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0
[src]

Writes a formatted string into this writer, returning any error encountered. Read more

fn by_ref(&mut self) -> &mut Self
1.0.0
[src]

Creates a "by reference" adaptor for this instance of Write. Read more

Auto Trait Implementations

impl !Send for Formatter

impl !Sync for Formatter

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]