1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use iron::{Request, Response};
use std::fmt;
use chrono::{DateTime, Duration, Local};
#[derive(Debug)]
pub struct LogContext<'req, 'res, 'a: 'req, 'b: 'a, 's, 'e> {
pub req: &'req Request<'a, 'b>,
pub res: &'res Response,
pub start_time: &'s DateTime<Local>,
pub end_time: &'e DateTime<Local>,
}
impl<'req, 'res, 'a: 'req, 'b: 'a, 's, 'e> LogContext<'req, 'res, 'a, 'b, 's, 'e> {
pub fn response_time(&self) -> Duration {
self.end_time.signed_duration_since(self.start_time.clone())
}
}
pub trait LogFormatter: 'static + Send + Sync {
fn format(&self, f: &mut fmt::Formatter, context: &LogContext) -> fmt::Result;
}
impl<F> LogFormatter for F
where F: 'static + Send + Sync + Fn(&mut fmt::Formatter, &LogContext) -> fmt::Result
{
fn format(&self, f: &mut fmt::Formatter, context: &LogContext) -> fmt::Result {
(*self)(f, context)
}
}
pub struct DefaultLogFormatter;
impl LogFormatter for DefaultLogFormatter {
fn format(&self, f: &mut fmt::Formatter, ctx: &LogContext) -> fmt::Result {
write!(f, "{} ", ctx.req.method)?;
write!(f, "{} ", ctx.req.url)?;
match ctx.res.status {
Some(status) => write!(f, "{} ", status)?,
None => write!(f, "<missing status code>")?,
}
write!(f, "({} ms)", ctx.response_time().num_milliseconds())?;
Ok(())
}
}