Trait trillium_logger::LogFormatter
source · pub trait LogFormatter: Send + Sync + 'static {
type Output: Display + Send + Sync + 'static;
// Required method
fn format(&self, conn: &Conn, color: bool) -> Self::Output;
}
Expand description
The interface to format a &Conn
as a Display
-able output
In general, the included loggers provide a mechanism for composing
these, so top level formats like dev_formatter
, apache_common
and apache_combined
are composed in terms of component formatters
like formatters::method
, formatters::ip
,
formatters::timestamp
, and many others (see formatters
for a
full list)
When implementing this trait, note that Display::fmt
is called on
LogFormatter::Output
after the response has been fully sent, but
that the LogFormatter::format
is called before the response has
been sent. If you need to perform timing-sensitive calculations that
represent the full http cycle, move whatever data is needed to make
the calculation into a new type that implements Display, ensuring that
it is calculated at the right time.
§Implementations
§Tuples
LogFormatter is implemented for all tuples of other LogFormatter types, from 2-26 formatters long. The output of these formatters is concatenated with no space between.
§&'static str
LogFormatter is implemented for &’static str, allowing for interspersing spaces and other static formatting details into tuples.
use trillium_logger::{Logger, formatters};
let handler = Logger::new()
.with_formatter(("-> ", formatters::method, " ", formatters::url));
§Fn(&Conn, bool) -> impl Display
LogFormatter is implemented for all functions that conform to this signature.
fn user(conn: &Conn, color: bool) -> Cow<'static, str> {
match conn.state::<User>() {
Some(user) => String::from(user.name()).into(),
None => "guest".into()
}
}
let handler = Logger::new().with_formatter((dev_formatter, " ", user));