Skip to main content

Format

Trait Format 

Source
pub trait Format: Send + Sync {
    // Required method
    fn write_record(
        &self,
        record: &Record<'_>,
        writer: &mut dyn Write,
    ) -> Result;

    // Provided method
    fn write_record_generic<W: Write>(
        &self,
        record: &Record<'_>,
        writer: &mut W,
    ) -> Result
       where Self: Sized { ... }
}
Expand description

A record-to-text serializer.

Implementors write exactly one logical record per call, including any trailing newline: the built-in sinks add no separators between records. Formatters write to core::fmt::Write (not std::io::Write) so they work under no_std and can format into a String or a fixed buffer.

Only Format::write_record is required. Override Format::write_record_generic as well if your format is on a hot path and you want the built-in sinks to call it without dynamic dispatch.

§Examples

use core::fmt;
use log_io::format::Format;
use log_io::{Level, Metadata, Record};

struct Minimal;

impl Format for Minimal {
    fn write_record(&self, record: &Record<'_>, w: &mut dyn fmt::Write) -> fmt::Result {
        writeln!(w, "{} {}", record.metadata.level, record.message)
    }
}

let record = Record::new(Metadata::new(Level::Info, "app"), "ready", &[]);
let mut out = String::new();
Minimal.write_record(&record, &mut out)?;
assert_eq!(out, "info ready\n");

// The trait is object-safe, so formats can be picked at run time.
let chosen: Box<dyn Format> = Box::new(Minimal);
out.clear();
chosen.write_record(&record, &mut out)?;
assert_eq!(out, "info ready\n");

Required Methods§

Source

fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result

Write record to writer.

§Errors

Returns core::fmt::Error if the writer rejects a write (or if a lazily formatted value’s Display / Debug impl fails).

Provided Methods§

Source

fn write_record_generic<W: Write>( &self, record: &Record<'_>, writer: &mut W, ) -> Result
where Self: Sized,

Statically dispatched variant of Format::write_record.

The built-in sinks call this method. The default implementation forwards to write_record; formats that care about the cost of dynamic dispatch override it with a generic implementation.

§Errors

Same as Format::write_record.

Trait Implementations§

Source§

impl Format for &dyn Format

Source§

fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result

Write record to writer. Read more
Source§

fn write_record_generic<W: Write>( &self, record: &Record<'_>, writer: &mut W, ) -> Result
where Self: Sized,

Statically dispatched variant of Format::write_record. Read more
Source§

impl Format for Box<dyn Format>

Available on crate feature std only.
Source§

fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result

Write record to writer. Read more
Source§

fn write_record_generic<W: Write>( &self, record: &Record<'_>, writer: &mut W, ) -> Result
where Self: Sized,

Statically dispatched variant of Format::write_record. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Format for Arc<dyn Format>

Available on crate feature std only.
Source§

fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result

Source§

impl Format for Box<dyn Format>

Available on crate feature std only.
Source§

fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result

Source§

impl<T: Format> Format for &T

Source§

fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result

Source§

fn write_record_generic<W: Write>( &self, record: &Record<'_>, writer: &mut W, ) -> Result

Source§

impl<T: Format> Format for Arc<T>

Available on crate feature std only.
Source§

fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result

Source§

fn write_record_generic<W: Write>( &self, record: &Record<'_>, writer: &mut W, ) -> Result

Source§

impl<T: Format> Format for Box<T>

Available on crate feature std only.
Source§

fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result

Source§

fn write_record_generic<W: Write>( &self, record: &Record<'_>, writer: &mut W, ) -> Result

Implementors§

Source§

impl Format for &dyn Format

Source§

impl Format for HumanFormat

Available on crate feature human only.
Source§

impl Format for JsonFormat

Available on crate feature json only.
Source§

impl Format for LogfmtFormat

Available on crate feature logfmt only.
Source§

impl<F> Format for FnFormat<F>
where F: Fn(&Record<'_>, &mut dyn Write) -> Result + Send + Sync,