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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
pub use tracing;

// re-export for scoped_logger! macro
pub use actix_web;
pub use futures;
pub use uuid;

use tracing::{event, Level};

use std::env::{self, VarError};
use std::sync::Once;

static INIT: Once = Once::new();

pub fn init_logger() {
  INIT.call_once(|| {
    tracing_subscriber::fmt()
      .with_ansi(false)
      .without_time()
      .with_env_filter(
        tracing_subscriber::EnvFilter::from_default_env(),
      )
      .init();
  });
}

pub fn log_simple_err_callback<E: std::fmt::Debug>(
  msg: &'static str,
) -> impl FnOnce(E) -> E {
  move |e| {
    log_simple_err(msg, &e);
    e
  }
}

pub fn log_simple_err<E: std::fmt::Debug>(msg: &str, err: &E) {
  event!(Level::ERROR, msg, error = ?err);
}

pub fn logged_var(variable_name: &str) -> Result<String, VarError> {
  env::var(variable_name).map_err(|e| {
    event!(
      Level::ERROR,
      msg = "unset environment",
      variable = variable_name
    );
    VarError::from(e)
  })
}

#[macro_export]
macro_rules! scoped_logger {
  () => {
    |mut req, srv| {
      use jonases_tracing_util::actix_web::dev::Service;
      use jonases_tracing_util::actix_web::http::{HeaderName, HeaderValue};

      let mut headers = req.headers_mut();

      let request_id = if let Some(id) = headers.get("x-request-id") {
        (&*String::from_utf8_lossy(id.as_bytes())).to_owned()
      } else {
        let request_id =
          jonases_tracing_util::uuid::Uuid::new_v4().to_string();

        // savely unwrapable, because I know what data I pass
        let name =
          HeaderName::from_lowercase(b"x-request-id").unwrap();
        let value = HeaderValue::from_str(&request_id).unwrap();

        headers.insert(name, value);

        request_id
      };

      let uri = req.uri().clone();
      let res = srv.call(req);

      async move {
        let span = jonases_tracing_util::tracing::span!(
          jonases_tracing_util::tracing::Level::INFO,
          "span",
          %uri,
          %request_id
        );
        let _enter = span.enter();

        match res.await {
          Ok(mut res) => {
            let status = res.status();

            if !res.status().is_success() {
              jonases_tracing_util::tracing::event!(
                jonases_tracing_util::tracing::Level::ERROR,
                msg = "unsuccessful response",
                status = %status,
              );
            } else {
              jonases_tracing_util::tracing::event!(
                jonases_tracing_util::tracing::Level::INFO,
                msg = "successful response",
                status = %status,
              );
            }
            Ok(res)
          },
          Err(e) => {
            let status = e.as_response_error().status_code();

            jonases_tracing_util::tracing::event!(
              jonases_tracing_util::tracing::Level::ERROR,
              msg = "unsuccessful response",
              status = %status,
              error_body = %e,
            );

            Err(e)
          },
        }
      }
    }
  }
}