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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use crate::LogFormatter;
use chrono::Local;
use colored::{ColoredString, Colorize};
use size::{Base, Size};
use std::{borrow::Cow, fmt::Display, sync::Arc, time::Instant};
use trillium::{Conn, Method, Status, Version};

/**
[apache combined log format][apache]

[apache]: https://httpd.apache.org/docs/current/logs.html#combined

This is defined as follows:

[`apache_combined`](`request_id`, `user_id`) [`header`]`("referrer")` [`header`]`("user-agent")`

where `request_id` and `user_id` are mandatory formatters provided at time of usage.


## usage with empty `request_id` and `user_id`
```
# use trillium_logger::{Logger, apache_combined};
Logger::new().with_formatter(apache_combined("-", "-"));
```

## usage with an app-specific `user_id`

```
# use trillium_logger::{Logger, apache_combined};
# use trillium::Conn;
# use std::borrow::Cow;
# struct User(String); impl User { fn name(&self) -> &str { &self.0 } }
fn user(conn: &Conn, color: bool) -> Cow<'static, str> {
     match conn.state::<User>() {
        Some(user) => String::from(user.name()).into(),
        None => "guest".into()
    }
}

Logger::new().with_formatter(apache_combined("-", user));
```
*/
pub fn apache_combined(
    request_id: impl LogFormatter,
    user_id: impl LogFormatter,
) -> impl LogFormatter {
    (
        apache_common(request_id, user_id),
        " ",
        header("referrer"),
        " ",
        header("user-agent"),
    )
}

/**
formatter for the conn's http method that delegates to [`Method`]'s
[`Display`] implementation
*/
pub fn method(conn: &Conn, _color: bool) -> Method {
    conn.method()
}

/**
simple development-mode formatter

composed of

`"`[`method`] [`url`] [`response_time`] [`status`]`"`
*/
pub fn dev_formatter(conn: &Conn, color: bool) -> impl Display + Send + 'static {
    (method, " ", url, " ", response_time, " ", status).format(conn, color)
}

/**
formatter for the peer ip address of the connection

**note**: this can be modified by handlers prior to logging, such as
when running a trillium application behind a reverse proxy or load
balancer that sets a `forwarded` or `x-forwarded-for` header. this
will display `"-"` if there is no available peer ip address, such as
when running on a runtime adapter that does not have access to this
information
*/
pub fn ip(conn: &Conn, _color: bool) -> Cow<'static, str> {
    match conn.inner().peer_ip() {
        Some(peer) => format!("{:?}", peer).into(),
        None => "-".into(),
    }
}

mod status_mod {
    use super::*;
    /**
    The display type for [`status`]
    */
    #[derive(Copy, Clone)]
    pub struct StatusOutput(Status, bool);
    impl Display for StatusOutput {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            let StatusOutput(status, color) = *self;
            let status_string = (status as u16).to_string();
            if color {
                f.write_fmt(format_args!(
                    "{}",
                    status_string.color(match status as u16 {
                        200..=299 => "green",
                        300..=399 => "cyan",
                        400..=499 => "yellow",
                        500..=599 => "red",
                        _ => "white",
                    })
                ))
            } else {
                f.write_str(&status_string)
            }
        }
    }

    /**
    formatter for the http status

    displays just the numeric code of the
    status. when color is enabled, it uses the following color encoding:

    | code | color  |
    |------|--------|
    | 2xx  | green  |
    | 3xx  | cyan   |
    | 4xx  | yellow |
    | 5xx  | red    |
    | ???  | white  |
    */
    pub fn status(conn: &Conn, color: bool) -> StatusOutput {
        StatusOutput(conn.status().unwrap_or(Status::NotFound), color)
    }
}

pub use status_mod::status;

/**
formatter-builder for a particular header, formatted wrapped in
quotes. `""` if the header is not present

usage:

```rust
# use trillium_logger::{Logger, formatters::header};
Logger::new().with_formatter(("user-agent: ", header("user-agent")));
```

**note**: this is not a formatter itself, but returns a formatter when
called with a header name
*/
pub fn header(header_name: &'static str) -> impl LogFormatter {
    move |conn: &Conn, _color: bool| {
        format!("{:?}", conn.headers().get_str(header_name).unwrap_or(""))
    }
}

mod timestamp_mod {
    use super::*;
    /**
    Display output for [`timestamp`]
    */
    pub struct Now;

    /**
    formatter for the current timestamp. this represents the time that the
    log is written, not the beginning timestamp of the request
    */
    pub fn timestamp(_conn: &Conn, _color: bool) -> Now {
        Now
    }

    impl Display for Now {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_fmt(format_args!(
                "{}",
                Local::now().format("%d/%b/%Y:%H:%M:%S %z")
            ))
        }
    }
}
pub use timestamp_mod::timestamp;

/**
formatter for the response body length, represented as a
human-readable string like `5 bytes` or `10.1mb`. prints `-` if there
is no response body. see [`bytes`] for the raw number of bytes
*/
pub fn body_len_human(conn: &Conn, _color: bool) -> Cow<'static, str> {
    conn.response_len()
        .map(|l| {
            Size::from_bytes(l)
                .format()
                .with_base(Base::Base10)
                .to_string()
                .into()
        })
        .unwrap_or_else(|| Cow::from("-"))
}

/**
[apache common log format][apache]

[apache]: https://httpd.apache.org/docs/current/logs.html#common

This is defined as follows:

[`ip`] `request_id` `user_id` `\[`[`timestamp`]`\]` "[`method`] [`url`] [`version`]" [`status`] [`bytes`]

where `request_id` and `user_id` are mandatory formatters provided at time of usage.

## usage without `request_id` or `user_id`
```
# use trillium_logger::{Logger, apache_common};
Logger::new().with_formatter(apache_common("-", "-"));
```

## usage with app-specific `user_id`
```
# use trillium_logger::{Logger, apache_common};
# use trillium::Conn;
# use std::borrow::Cow;
# struct User(String); impl User { fn name(&self) -> &str { &self.0 } }
fn user(conn: &Conn, color: bool) -> Cow<'static, str> {
     match conn.state::<User>() {
        Some(user) => String::from(user.name()).into(),
        None => "guest".into()
    }
}

Logger::new().with_formatter(apache_common("-", user));
```
*/
pub fn apache_common(
    request_id: impl LogFormatter,
    user_id: impl LogFormatter,
) -> impl LogFormatter {
    (
        ip, " ", request_id, " ", user_id, " [", timestamp, "] \"", method, " ", url, " ", version,
        "\" ", status, " ", bytes,
    )
}

/**
formatter that prints the number of response body bytes as a
number. see [`body_len_human`] for a human-readable response body
length with units
*/
pub fn bytes(conn: &Conn, _color: bool) -> u64 {
    conn.response_len().unwrap_or_default()
}

/**
formatter that prints an emoji if the request is secure as determined
by [`Conn::is_secure`]
*/
pub fn secure(conn: &Conn, _: bool) -> &'static str {
    if conn.is_secure() {
        "🔒"
    } else {
        "  "
    }
}

/**
formatter for the current url or path of the request, including query
*/
pub fn url(conn: &Conn, _color: bool) -> String {
    match conn.querystring() {
        "" => conn.path().into(),
        query => format!("{}?{}", conn.path(), query),
    }
}

mod response_time_mod {
    use super::*;
    /**
    display output type for the [`response_time`] formatter
    */
    pub struct ResponseTimeOutput(Instant);
    impl Display for ResponseTimeOutput {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_fmt(format_args!("{:?}", Instant::now() - self.0))
        }
    }

    /**
    formatter for the wall-time duration with units that this http
    request-response cycle took, from the first bytes read to the
    completion of the response.
    */
    pub fn response_time(conn: &Conn, _color: bool) -> ResponseTimeOutput {
        ResponseTimeOutput(conn.inner().start_time())
    }
}

pub use response_time_mod::response_time;

/**
formatter for the http version, as delegated to the display
implementation of [`Version`]
*/
pub fn version(conn: &Conn, _color: bool) -> Version {
    conn.inner().http_version()
}

impl LogFormatter for &'static str {
    type Output = Self;
    fn format(&self, _conn: &Conn, _color: bool) -> Self::Output {
        self
    }
}

impl LogFormatter for Arc<str> {
    type Output = Self;
    fn format(&self, _conn: &Conn, _color: bool) -> Self::Output {
        Arc::clone(self)
    }
}

impl LogFormatter for ColoredString {
    type Output = String;
    fn format(&self, _conn: &Conn, color: bool) -> Self::Output {
        if color {
            self.to_string()
        } else {
            (**self).to_string()
        }
    }
}

impl<F, O> LogFormatter for F
where
    F: Fn(&Conn, bool) -> O + Send + Sync + 'static,
    O: Display + Send + Sync + 'static,
{
    type Output = O;
    fn format(&self, conn: &Conn, color: bool) -> Self::Output {
        self(conn, color)
    }
}

mod tuples {
    use super::*;
    /**
    display output for the tuple implementation

    The Display type of each tuple element is contained in this type, and
    it implements [`Display`] for 2-26-arity tuples.

    Please open an issue if you find yourself needing to do something with
    this other than [`Display`] it.
    */
    pub struct TupleOutput<O>(O);
    macro_rules! impl_formatter_tuple {
        ($($name:ident)+) => (
            #[allow(non_snake_case)]
            impl<$($name,)*> Display for TupleOutput<($($name,)*)> where $($name: Display + Send + Sync + 'static,)* {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    let ($(ref $name,)*) = self.0;
                    f.write_fmt(format_args!(
                        concat!($(
                            concat!("{",stringify!($name) ,":}")
                        ),*),
                        $($name = ($name)),*
                    ))
                }
            }

            #[allow(non_snake_case)]
            impl<$($name),*> LogFormatter for ($($name,)*) where $($name: LogFormatter),* {
                type Output = TupleOutput<($($name::Output,)*)>;
                fn format(&self, conn: &Conn, color: bool) -> Self::Output {
                    let ($(ref $name,)*) = *self;
                    TupleOutput(($(($name).format(conn, color),)*))
                }
            }
        )
    }

    impl_formatter_tuple! { A B }
    impl_formatter_tuple! { A B C }
    impl_formatter_tuple! { A B C D }
    impl_formatter_tuple! { A B C D E }
    impl_formatter_tuple! { A B C D E F }
    impl_formatter_tuple! { A B C D E F G }
    impl_formatter_tuple! { A B C D E F G H }
    impl_formatter_tuple! { A B C D E F G H I }
    impl_formatter_tuple! { A B C D E F G H I J }
    impl_formatter_tuple! { A B C D E F G H I J K }
    impl_formatter_tuple! { A B C D E F G H I J K L }
    impl_formatter_tuple! { A B C D E F G H I J K L M }
    impl_formatter_tuple! { A B C D E F G H I J K L M N }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R S }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R S T }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R S T U }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R S T U V }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R S T U V W }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R S T U V W X }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R S T U V W X Y }
    impl_formatter_tuple! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }
}