|
1 | 1 | //! Crate that provides helpers and/or middlewares for Tide |
2 | 2 | //! related to structured logging with slog. |
3 | 3 |
|
| 4 | +#![cfg_attr(docrs, feature(doc_cfg))] |
4 | 5 | #![feature(async_await)] |
5 | 6 | #![warn( |
6 | 7 | nonstandard_style, |
|
9 | 10 | missing_debug_implementations |
10 | 11 | )] |
11 | 12 |
|
12 | | -use slog::{info, o, trace, Drain}; |
13 | | -use slog_async; |
14 | | -use slog_term; |
15 | | - |
16 | | -use futures::future::BoxFuture; |
17 | | - |
18 | | -use tide_core::{ |
19 | | - middleware::{Middleware, Next}, |
20 | | - Context, Response, |
21 | | -}; |
22 | | - |
23 | | -/// RequestLogger based on slog.SimpleLogger |
24 | | -#[derive(Debug)] |
25 | | -pub struct RequestLogger { |
26 | | - // drain: dyn slog::Drain, |
27 | | - inner: slog::Logger, |
28 | | -} |
29 | | - |
30 | | -impl RequestLogger { |
31 | | - pub fn new() -> Self { |
32 | | - Default::default() |
33 | | - } |
34 | | - |
35 | | - pub fn with_logger(logger: slog::Logger) -> Self { |
36 | | - Self { inner: logger } |
37 | | - } |
38 | | -} |
39 | | - |
40 | | -impl Default for RequestLogger { |
41 | | - fn default() -> Self { |
42 | | - let decorator = slog_term::TermDecorator::new().build(); |
43 | | - let drain = slog_term::CompactFormat::new(decorator).build().fuse(); |
44 | | - let drain = slog_async::Async::new(drain).build().fuse(); |
45 | | - |
46 | | - let log = slog::Logger::root(drain, o!()); |
47 | | - Self { inner: log } |
48 | | - } |
| 13 | +mod per_request_logger; |
| 14 | +mod request_logger; |
| 15 | +#[cfg(feature = "scope")] |
| 16 | +mod set_slog_scope_logger; |
| 17 | + |
| 18 | +pub use per_request_logger::PerRequestLogger; |
| 19 | +pub use request_logger::RequestLogger; |
| 20 | +#[cfg(feature = "scope")] |
| 21 | +pub use set_slog_scope_logger::SetSlogScopeLogger; |
| 22 | + |
| 23 | +use tide_core::Context; |
| 24 | + |
| 25 | +/// An extension to [`Context`] that provides access to a per-request [`slog::Logger`] |
| 26 | +pub trait ContextExt { |
| 27 | + /// Returns a [`slog::Logger`] scoped to this request. |
| 28 | + /// |
| 29 | + /// # Panics |
| 30 | + /// |
| 31 | + /// Will panic if no [`PerRequestLogger`] middleware has been used to setup the request scoped |
| 32 | + /// logger. |
| 33 | + fn logger(&self) -> &slog::Logger; |
49 | 34 | } |
50 | 35 |
|
51 | | -/// Stores information during request phase and logs information once the response |
52 | | -/// is generated. |
53 | | -impl<State: Send + Sync + 'static> Middleware<State> for RequestLogger { |
54 | | - fn handle<'a>(&'a self, cx: Context<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> { |
55 | | - Box::pin(async move { |
56 | | - let path = cx.uri().path().to_owned(); |
57 | | - let method = cx.method().as_str().to_owned(); |
58 | | - trace!(self.inner, "IN => {} {}", method, path); |
59 | | - let start = std::time::Instant::now(); |
60 | | - let res = next.run(cx).await; |
61 | | - let status = res.status(); |
62 | | - info!( |
63 | | - self.inner, |
64 | | - "{} {} {} {}ms", |
65 | | - method, |
66 | | - path, |
67 | | - status.as_str(), |
68 | | - start.elapsed().as_millis() |
69 | | - ); |
70 | | - res |
71 | | - }) |
| 36 | +impl<State> ContextExt for Context<State> { |
| 37 | + fn logger(&self) -> &slog::Logger { |
| 38 | + self.extensions() |
| 39 | + .get::<slog::Logger>() |
| 40 | + .expect("PerRequestLogger must be used to populate request logger") |
72 | 41 | } |
73 | 42 | } |
0 commit comments