pub trait SyslogFormatter {
    type Error: Error;
    type Output: Deref<Target = [u8]>;

    // Required method
    fn format(
        &self,
        level: Level,
        msg: &str,
        timestamp: Option<DateTime<Utc>>
    ) -> Result<Self::Output, Self::Error>;
}
Expand description

Operations all formatters must support

Introduction

The translation from tracing events to syslog messages occurs in three parts:

  1. formatting the event to a textual message

  2. incorporating that message into a syslog packet compliant with your daemon’s implementation

  3. transporting that packet to your daemon

SyslogFormatter implements step 2 in this process: given the Level, a textual message field, and an optional timestamp, produce a compliant syslog packet.

Design

The associated type Output is designed to make illegal states unrepresentable. If the Transport trait simply took, say, a slice of u8 then callers could mistakenly pass anything to it (a little endian binary representation of a u32, [0; 1204] or any silly thing). I would like to enforce the rule that “The thing passed to the Transport trait must have been returned from a SyslogFormatter implementation.” Hence the associated type, and the constraint that it be dereferenceable to a slice of u8 (to enable the Transport implementation to deal with it). This does mean making the SyslogFormatter implementation type a generic parameter to the Transport type.

Required Associated Types§

source

type Error: Error

source

type Output: Deref<Target = [u8]>

Required Methods§

source

fn format( &self, level: Level, msg: &str, timestamp: Option<DateTime<Utc>> ) -> Result<Self::Output, Self::Error>

Implementors§