-
Notifications
You must be signed in to change notification settings - Fork 321
/
logger.rs
49 lines (41 loc) · 1.34 KB
/
logger.rs
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
use slog::{info, o, Drain};
use slog_async;
use slog_term;
use futures::future::FutureObj;
use crate::{
middleware::{Middleware, Next},
Context, Response,
};
/// Root logger for Tide. Wraps over logger provided by slog.SimpleLogger
pub struct RootLogger {
// drain: dyn slog::Drain,
inner_logger: slog::Logger,
}
impl RootLogger {
pub fn new() -> RootLogger {
let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
let drain = slog_async::Async::new(drain).build().fuse();
let log = slog::Logger::root(drain, o!());
RootLogger { inner_logger: log }
}
}
impl Default for RootLogger {
fn default() -> Self {
Self::new()
}
}
/// Stores information during request phase and logs information once the response
/// is generated.
impl<Data: Send + Sync + 'static> Middleware<Data> for RootLogger {
fn handle<'a>(&'a self, cx: Context<Data>, next: Next<'a, Data>) -> FutureObj<'a, Response> {
box_async! {
let path = cx.uri().path().to_owned();
let method = cx.method().as_str().to_owned();
let res = await!(next.run(cx));
let status = res.status();
info!(self.inner_logger, "{} {} {}", method, path, status.as_str());
res
}
}
}