jonases_tracing_util/
lib.rs

1pub use tracing;
2
3// re-export for scoped_logger! macro
4pub use actix_web;
5pub use futures;
6pub use uuid;
7
8use tracing::{event, Level};
9
10use std::env::{self, VarError};
11use std::sync::Once;
12
13static INIT: Once = Once::new();
14
15pub fn init_logger() {
16  INIT.call_once(|| {
17    tracing_subscriber::fmt()
18      .with_ansi(false)
19      .without_time()
20      .with_env_filter(
21        tracing_subscriber::EnvFilter::from_default_env(),
22      )
23      .init();
24  });
25}
26
27pub fn log_simple_err_callback<E: std::fmt::Debug, M: AsRef<str>>(
28  msg: M,
29) -> impl FnOnce(E) -> E {
30  move |e| {
31    log_simple_err(msg, &e);
32    e
33  }
34}
35
36pub fn log_simple_err<E: std::fmt::Debug, M: AsRef<str>>(
37  msg: M,
38  err: &E,
39) {
40  event!(Level::ERROR, msg = msg.as_ref(), error = ?err);
41}
42
43pub fn logged_var(variable_name: &str) -> Result<String, VarError> {
44  env::var(variable_name).map_err(|e| {
45    event!(
46      Level::ERROR,
47      msg = "unset environment",
48      variable = variable_name
49    );
50    VarError::from(e)
51  })
52}
53
54#[macro_export]
55macro_rules! scoped_logger {
56  () => {
57    |mut req, srv| {
58      use jonases_tracing_util::actix_web::dev::Service;
59      use jonases_tracing_util::actix_web::http::{HeaderName, HeaderValue};
60
61      let mut headers = req.headers_mut();
62
63      let request_id = if let Some(id) = headers.get("x-request-id") {
64        (&*String::from_utf8_lossy(id.as_bytes())).to_owned()
65      } else {
66        let request_id =
67          jonases_tracing_util::uuid::Uuid::new_v4().to_string();
68
69        // savely unwrapable, because I know what data I pass
70        let name =
71          HeaderName::from_lowercase(b"x-request-id").unwrap();
72        let value = HeaderValue::from_str(&request_id).unwrap();
73
74        headers.insert(name, value);
75
76        request_id
77      };
78
79      let uri = req.uri().clone();
80      let res = srv.call(req);
81
82      async move {
83        let span = jonases_tracing_util::tracing::span!(
84          jonases_tracing_util::tracing::Level::INFO,
85          "span",
86          %uri,
87          %request_id
88        );
89        let _enter = span.enter();
90
91        match res.await {
92          Ok(mut res) => {
93            let status = res.status();
94
95            if !res.status().is_success() {
96              jonases_tracing_util::tracing::event!(
97                jonases_tracing_util::tracing::Level::ERROR,
98                msg = "unsuccessful response",
99                status = %status,
100              );
101            } else {
102              jonases_tracing_util::tracing::event!(
103                jonases_tracing_util::tracing::Level::INFO,
104                msg = "successful response",
105                status = %status,
106              );
107            }
108            Ok(res)
109          },
110          Err(e) => {
111            let status = e.as_response_error().status_code();
112
113            jonases_tracing_util::tracing::event!(
114              jonases_tracing_util::tracing::Level::ERROR,
115              msg = "unsuccessful response",
116              status = %status,
117              error_body = %e,
118            );
119
120            Err(e)
121          },
122        }
123      }
124    }
125  }
126}