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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! structured request logging middleware
//!
//! ```no_run
//! use actix_web::{HttpServer, App};
//! use actix_slog::StructuredLogger;
//! use slog::o;
//!
//! fn main() {
//!   let logger: slog::Logger = unimplemented!();
//!   let server = HttpServer::new(move || {
//!     App::new()
//!       .wrap(
//!         StructuredLogger::new(logger.new(o!("log_type" => "access"))),
//!       )
//!     })
//!     .bind("[::1]:8080");
//!
//!     unimplemented!()
//! }
//!     
//! ```
use actix_web::dev::{
    BodySize, MessageBody, ResponseBody, Service, ServiceRequest, ServiceResponse, Transform,
};
use actix_web::error::{Error, Result};
use actix_web::http::header::{HOST, REFERER, USER_AGENT};
use actix_web::http::StatusCode;
use actix_web::web::Bytes;
use chrono::prelude::*;
use futures::future::{ok, Ready};
use slog::{debug, info, o, Logger};
use std::borrow::ToOwned;
use std::collections::HashSet;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};

/// global configuration/builder for the log middleware
pub struct StructuredLogger(Rc<Inner>);

struct Inner {
    logger: Logger,
    exclude: HashSet<String>,
}

impl StructuredLogger {
    /// Create `Logger` middleware with the specified `format`.
    #[must_use]
    pub fn new(logger: Logger) -> StructuredLogger {
        StructuredLogger(Rc::new(Inner {
            logger,
            exclude: HashSet::new(),
        }))
    }

    /// Ignore and do not log access for specified path.
    pub fn exclude<T: Into<String>>(mut self, path: T) -> Self {
        Rc::get_mut(&mut self.0)
            .unwrap()
            .exclude
            .insert(path.into());
        self
    }
}

/// "initializer" for the service/the actual middleware (called once per worker)
impl<S, B> Transform<S> for StructuredLogger
where
    S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    B: MessageBody,
{
    type Request = ServiceRequest;
    type Response = ServiceResponse<StreamLog<B>>;
    type Error = Error;
    type Transform = StructuredLoggerMiddleware<S>;
    type InitError = ();
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ok(StructuredLoggerMiddleware {
            service,
            inner: self.0.clone(),
        })
    }
}

/// Logger middleware
pub struct StructuredLoggerMiddleware<S> {
    inner: Rc<Inner>,

    /// the next service in the chain, kind of like express' next()
    service: S,
}

impl<S, B> Service for StructuredLoggerMiddleware<S>
where
    S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    B: MessageBody,
{
    type Request = ServiceRequest;
    type Response = ServiceResponse<StreamLog<B>>;
    type Error = Error;
    type Future = LoggerResponse<S, B>;

    fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
        self.service.poll_ready(cx)
    }

    fn call(&mut self, req: ServiceRequest) -> Self::Future {
        // check the exclude-list if to skip this path…
        let is_exclude = self.inner.exclude.contains(req.path());

        // …but collect other fields nevertheless, to log errors etc.
        let timestamp = Utc::now();

        let user_agent = req
            .headers()
            .get(USER_AGENT)
            .and_then(|v| v.to_str().ok())
            .unwrap_or("-");

        let referer = req
            .headers()
            .get(REFERER)
            .and_then(|v| v.to_str().ok())
            .unwrap_or("-");

        let remote_addr = req
            .connection_info()
            .remote()
            .map_or(String::from("-"), ToOwned::to_owned);

        let host = req
            .headers()
            .get(HOST)
            .and_then(|v| v.to_str().ok())
            .unwrap_or("-");

        let correlation_id = req
            .headers()
            .get("correlation-id")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("-");

        let logger = self.inner.logger.new(o!(
            "version" => format!("{:?}", req.version()),
            "http_host" => host.to_owned(),
            "referer" => referer.to_owned(),
            "remote_address" => remote_addr,
            "user-agent" => user_agent.to_owned(),
            "request_method" => req.method().to_string(),
            "correlation_id" => correlation_id.to_owned(),
            "uri" => req.path().to_owned(),
            "query" => format!("?{}", req.query_string()),
        ));

        LoggerResponse {
            logger,
            fut: self.service.call(req),
            timestamp,
            _t: PhantomData,
            is_exclude,
        }
    }
}

#[doc(hidden)]
#[pin_project::pin_project]
pub struct LoggerResponse<S, B>
where
    B: MessageBody,
    S: Service,
{
    #[pin]
    fut: S::Future,
    // timestamp at which the request hit the service (in contrast to when the log is written, i.e. the request is done)
    timestamp: DateTime<Utc>,
    logger: Logger,
    // if to exclude this request
    is_exclude: bool,
    _t: PhantomData<(B,)>,
}

/// "handler" for the response, i.e. "action" to call once the other services are done, and the
/// response is ready
impl<S, B> Future for LoggerResponse<S, B>
where
    B: MessageBody,
    S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
{
    type Output = Result<ServiceResponse<StreamLog<B>>, Error>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let this = self.project();

        let res = match futures::ready!(this.fut.poll(cx)) {
            Ok(res) => res,
            Err(e) => return Poll::Ready(Err(e)),
        };

        if let Some(error) = res.response().error() {
            if res.response().head().status != StatusCode::INTERNAL_SERVER_ERROR {
                debug!(this.logger, "Error in response: {:?}", error);
            }
        }

        let timestamp = *this.timestamp;
        let logger = this.logger.new(o!("status" => res.status().as_u16()));
        let is_exclude: bool = *this.is_exclude;

        Poll::Ready(Ok(res.map_body(move |_, body| {
            ResponseBody::Body(StreamLog {
                logger,
                is_exclude,
                body,
                timestamp,
                size: 0,
            })
        })))
    }
}

pub struct StreamLog<B> {
    logger: Logger,
    is_exclude: bool,
    body: ResponseBody<B>,
    size: usize,
    timestamp: DateTime<Utc>,
}

impl<B> Drop for StreamLog<B> {
    fn drop(&mut self) {
        if !self.is_exclude {
            let response_time = Utc::now() - self.timestamp;
            let response_time = response_time.num_milliseconds();
            info!(self.logger, "-"; o!("bytes_sent" => self.size), "response_time" => response_time);
        }
    }
}

impl<B: MessageBody> MessageBody for StreamLog<B> {
    fn size(&self) -> BodySize {
        self.body.size()
    }

    fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Result<Bytes, Error>>> {
        match self.body.poll_next(cx) {
            Poll::Ready(Some(Ok(chunk))) => {
                self.size += chunk.len();
                Poll::Ready(Some(Ok(chunk)))
            }
            val => val,
        }
    }
}