bunyan_view 0.3.3

A full-featured port of the Node Bunyan structured log file viewer
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
use crate::divider_writer::DividerWriter;
use crate::errors::{BunyanLogParseError, ParseIntFromJsonError, ParseResult};
use crate::BASE_INDENT_SIZE;
use crate::{BunyanLine, LogLevel, Logger, LoggerOutputConfig};

use std::io::Write;

use httpstatus::StatusCode;

use serde_json::map::Map;
use serde_json::Value;

use colored::*;

use chrono::{Local, SecondsFormat};

/// Maximum characters for a string value in the extra parameters section
const LONG_LINE_SIZE: usize = 50;
/// Reserved keywords for requests records
const REQ_RESERVED: [&str; 6] = [
    "method",
    "url",
    "httpVersion",
    "body",
    "headers",
    "trailers",
];
/// Reserved keywords for client requests records
const CLIENT_REQ_RESERVED: [&str; 8] = [
    "method",
    "url",
    "httpVersion",
    "body",
    "headers",
    "trailers",
    "address",
    "port",
];
/// Reserved keywords for responses records
const RES_RESERVED: [&str; 6] = [
    "statusCode",
    "header",
    "headers",
    "trailer",
    "body",
    "trailer",
];
/// Reserved keywords for client responses records
const CLIENT_RES_RESERVED: [&str; 5] = ["statusCode", "body", "header", "headers", "trailer"];
/// Reserved keywords for error records
const ERR_RESERVED: [&str; 3] = ["message", "name", "stack"];
/// Reserved keywords for the `other` map in `BunyanLine`
const GENERAL_RESERVED: [&str; 5] = ["req", "client_req", "res", "client_res", "err"];
/// Default assumed HTTP version
const DEFAULT_HTTP_VERSION: &str = "1.1";

/// Writes the src information of the log line if it is present.
///
/// # Arguments
///
/// * `writer` - Write implementation to output data to
/// * `other` - Mutable map containing JSON optional JSON data. Keys will be removed as processed.
///
fn write_src<W: Write>(writer: &mut W, other: &mut Map<String, Value>) {
    if let Some(ref src) = other.remove("src") {
        match src {
            Value::Object(map) => {
                // We only display the src information if [src.file] is present
                if let Some(ref file) = map.get("file") {
                    w!(writer, "{}", " (".green());
                    w!(writer, "{}", string_or_value!(file).green());

                    if let Some(ref line) = map.get("line") {
                        w!(writer, ":{}", string_or_value!(line).green());
                    }

                    if let Some(ref func) = map.get("func") {
                        w!(writer, "{}", " in ");
                        w!(writer, "{}", string_or_value!(func).bright_green());
                    }

                    w!(writer, "{}", ")".green());
                }
            }
            Value::String(text) => w!(writer, " ({})", text),
            _ => (),
        }
    }
}

/// Writes all of the extra parameters to the top line of output by iterating through the `others`
/// map provided.
///
/// # Arguments
/// * `writer` - Write implementation to output data to
/// * `other` - Map containing all non-explicitly deserialized keys and values
/// * `details` - Mutable vector containing strings to be written as output later
///
fn write_all_extra_params<W: Write>(
    writer: &mut W,
    other: &mut Map<String, Value>,
    details: &mut Vec<String>,
) {
    /// Returns the passed value as a pretty printed JSON string with indents.
    ///
    /// # Arguments
    /// * `key` - Key associated with value being processed
    /// * `value` - Value to be converted to a pretty printed string
    /// * `caller_option` - Optional name of top-level record (eg `req`, `res`, `err`, etc)
    ///
    fn detail_pretty_print(key: &str, value: &Value, caller_option: Option<&str>) -> String {
        let pretty =
            ::serde_json::to_string_pretty(value).unwrap_or_else(|_| "[malformed]".to_string());

        match caller_option {
            Some(caller) => format!("{}.{}: {}", caller, key, pretty),
            None => format!("{}: {}", key, pretty),
        }
    }

    /// Returns true if the given JSON value is a JSON string and it has a newline character or it
    /// is longer than the LONG_LINE_SIZE (50 characterS).
    ///
    /// # Arguments
    ///
    /// * `text` - string to test to see if it qualifies for multiline output
    fn is_multiline_string(text: &str) -> bool {
        text.contains('\n') || text.len() > LONG_LINE_SIZE
    }

    /// Returns the passed value as a string optionally with enclosing quotes. If
    /// the conversion to a string of the value yields a string with spaces, then
    /// the returned string will be enclosed with double quotes.
    ///
    /// # Arguments
    ///
    /// * `value` - a serde JSON value object to convert to a String
    fn quoteify(value: &Value) -> String {
        if let Some(text) = value.as_str() {
            if text.contains(' ') {
                return value.to_string();
            }
        }

        string_or_value!(value)
    }

    /// Returns an optional string representing the string presentation of an extra parameter. When
    /// a `None` value is returned, the value has been added to the `details` vector.
    ///
    /// # Arguments
    /// * `key` - Key associated with value being processed
    /// * `value` - Value to be converted to a pretty printed string
    /// * `caller_option` - Optional name of top-level record (eg `req`, `res`, `err`, etc)
    /// * `details` - Mutable vector containing strings to be written as output later
    fn stringify(
        key: &str,
        value: &Value,
        caller_option: Option<&str>,
        details: &mut Vec<String>,
    ) -> Option<String> {
        match value {
            Value::String(text) => {
                // Add long strings to details
                if is_multiline_string(text) {
                    let detail = match caller_option {
                        Some(caller) => format!("{}.{}: {}", caller, key, text),
                        None => format!("{}: {}", key, text),
                    };

                    details.push(detail);

                    None
                // Wrap strings with spaces in quotation marks
                } else {
                    Some(quoteify(value))
                }
            }
            Value::Number(_) => Some(string_or_value!(value)),
            Value::Bool(_) => Some(string_or_value!(value)),
            Value::Null => Some(string_or_value!(value)),
            Value::Object(map) => {
                if map.is_empty() {
                    Some("{}".to_string())
                } else {
                    details.push(detail_pretty_print(key, value, caller_option));
                    None
                }
            }
            Value::Array(array) => {
                if array.is_empty() {
                    Some("[]".to_string())
                } else {
                    details.push(detail_pretty_print(key, value, caller_option));
                    None
                }
            }
        }
    }

    /// Writes the loading open parentheses if `if_first` is true. Otherwise, if
    /// `is_first` is false, then it writes a leading command and space.
    ///
    /// # Arguments
    ///
    /// * `writer` - Write implementation to output data to
    /// * `is_first` - Mutable boolean indicating if the first parameter has been processed
    fn write_formatting<W: Write>(writer: &mut W, is_first: &mut bool) {
        if *is_first {
            w!(writer, " (");
            *is_first = false;
        } else {
            w!(writer, ", ");
        }
    }

    /// Writes the extra parameters for the passed `optional_node` value if not `None`.
    ///
    /// # Arguments
    /// * `writer` - Write implementation to output data to
    /// * `caller_option` - Optional name of top-level record (eg `req`, `res`, `err`, etc)
    /// * `is_first` - Mutable boolean indicating if the first parameter has been processed
    /// * `optional_node` - Optional Json object represented as `Value` containing parameters to be processed
    /// * `details` - Mutable vector containing strings to be written as output later
    /// * `exclude` - Closure in which when evaluated is true will exclude a given parameter
    ///
    fn write_params_for_object<W: Write>(
        writer: &mut W,
        caller_option: Option<&str>,
        is_first: &mut bool,
        node_option: Option<&Value>,
        details: &mut Vec<String>,
        exclude: &dyn Fn(&str) -> bool,
    ) {
        if node_option.is_none() {
            return;
        }

        let node = node_option.unwrap();

        // Display strings, numbers and null values, as-is
        if caller_option.is_some()
            && (node.is_string() || node.is_number() || node.is_null() || node.is_boolean())
        {
            write_formatting(writer, is_first);
            w!(writer, "{}={}", caller_option.unwrap(), quoteify(node));
            return;
        }

        if let Some(caller_option_value) = caller_option {
            if node.is_array() {
                let value = stringify(caller_option_value, node, None, details);
                if let Some(text) = value {
                    write_formatting(writer, is_first);
                    w!(writer, "{}={}\n", caller_option_value, text);
                }
                return;
            }
        }

        let map = node.as_object().unwrap();

        for (k, v) in map.iter() {
            if exclude(k.as_str()) {
                continue;
            }

            let value: Option<String> = stringify(k, v, caller_option, details);

            if let Some(text) = value {
                write_formatting(writer, is_first);

                match caller_option {
                    Some(caller) => w!(writer, "{}.{}={}", caller, k, text),
                    None => w!(writer, "{}={}", k, text),
                }
            }
        }
    }

    let mut is_first: bool = true;

    // REQUEST ID [req_id] - special case we always write this first for visibility
    if let Some(req_id) = other.remove("req_id") {
        write_formatting(writer, &mut is_first);
        w!(writer, "req_id={}", string_or_value!(req_id));
    }

    /* Note: based on logic in write_params_for_object, parameters that do not fit
    properly within the extra parameters section, will be added to the details
    vector. */

    // Write out all keys and values that is are in GENERAL_RESERVED.
    let other_value = ::serde_json::to_value(&other).unwrap();
    write_params_for_object(
        writer,
        None,
        &mut is_first,
        Some(&other_value),
        details,
        &|k: &str| GENERAL_RESERVED.contains(&k),
    );

    /* Below, we write out the parameters of all JSON keys that are present in
    GENERAL_RESERVED in order to output any of the contents that need to be
    present in the extra parameters section of the logs. */

    // REQUEST [rec]
    write_params_for_object(
        writer,
        Some("req"),
        &mut is_first,
        other.get("req"),
        details,
        &|k: &str| REQ_RESERVED.contains(&k),
    );

    // CLIENT REQUEST [client_req]
    write_params_for_object(
        writer,
        Some("client_req"),
        &mut is_first,
        other.get("client_req"),
        details,
        &|k: &str| CLIENT_REQ_RESERVED.contains(&k),
    );

    // RESPONSE [res]
    write_params_for_object(
        writer,
        Some("res"),
        &mut is_first,
        other.get("res"),
        details,
        &|k: &str| RES_RESERVED.contains(&k),
    );

    // CLIENT RESPONSE [client_res]
    write_params_for_object(
        writer,
        Some("client_res"),
        &mut is_first,
        other.get("client_res"),
        details,
        &|k: &str| CLIENT_RES_RESERVED.contains(&k),
    );

    // ERROR INFORMATION [err]
    write_params_for_object(
        writer,
        Some("err"),
        &mut is_first,
        other.get("err"),
        details,
        &|k: &str| ERR_RESERVED.contains(&k),
    );

    if !is_first {
        w!(writer, ")");
    }
}

/// Writes the HTTP request information logged for the line.
///
/// # Arguments
///
/// * `writer` - Write implementation to output data to
/// * `other` - Map containing all non-explicitly deserialized keys and values
///
fn write_req<W: Write>(writer: &mut W, key: &str, other: &mut Map<String, Value>) {
    /// Writes the method, url and HTTP version associated with a request.
    ///
    /// # Arguments
    ///
    /// * `writer` - Write implementation to output data to
    /// * `caller_name` - text indicating if we have been invoked from a "req" or "client_req" code path
    /// * `req_map` - Mutable map request data. Keys will be removed as processed.
    ///
    /// # Errors
    ///
    /// This function will return None if no errors have been encountered. In the case of parsing logic
    /// errors where the JSON data is not in the expected format, it will return a
    /// `ParseResult`.
    ///
    fn write_req_summary<W: Write>(
        writer: &mut W,
        caller: &str,
        req_map: &mut Map<String, Value>,
    ) -> ParseResult {
        w!(writer, "{:indent$}", "", indent = BASE_INDENT_SIZE);

        if let Some(method) = req_map.remove("method") {
            if let Some(method_text) = method.as_str() {
                w!(writer, "{} ", method_text.yellow());
            } else {
                return Err(BunyanLogParseError::new(format!(
                    "[{}.method] is not a JSON string",
                    caller
                )));
            }
        } else {
            return Err(BunyanLogParseError::new(format!(
                "[{}.method] is not present",
                caller
            )));
        }

        if let Some(url) = req_map.remove("url") {
            if let Some(url_text) = url.as_str() {
                w!(writer, "{} ", url_text.bright_blue());
            } else {
                return Err(BunyanLogParseError::new(format!(
                    "[{}.url] is not a JSON string",
                    caller
                )));
            }
        } else {
            return Err(BunyanLogParseError::new(format!(
                "[{}.url] is not present",
                caller
            )));
        }

        if let Some(http_version) = req_map.remove("httpVersion") {
            match http_version {
                Value::String(text) => w!(writer, "HTTP/{}", text),
                Value::Number(number) => w!(writer, "HTTP/{}", number),
                Value::Null => w!(writer, "HTTP/{}", DEFAULT_HTTP_VERSION),
                _ => {
                    return Err(BunyanLogParseError::new(format!(
                        "[{}.httpVersion] is not a string or number",
                        caller
                    )))
                }
            };
        } else {
            // we default to 1.1 if value is not present because that's what node bunyan does
            w!(writer, "HTTP/{}", DEFAULT_HTTP_VERSION);
        }

        wln!(writer);
        Ok(())
    }

    let req_option = other.remove(key);

    if req_option.is_none() {
        return;
    }

    let mut req = req_option.unwrap();

    if !req.is_object() {
        return;
    }

    let req_map = req.as_object_mut().unwrap();

    // METHOD, URL, HTTP VERSION
    // If we can't parse a method, URL or Http Version from the request, output in JSON as is
    if write_req_summary(writer, key, req_map).is_err() {
        wln!(writer, "undefined undefined HTTP/1.1");
        return;
    }

    // CONNECTING HOST FOR CLIENT REQUEST
    if key.eq("client_req") {
        if let Some(address) = req_map.remove("address") {
            w!(
                writer,
                "{:indent$}Connecting Host: {}",
                "",
                string_or_value!(address),
                indent = BASE_INDENT_SIZE
            );

            if let Some(port) = req_map.remove("port") {
                w!(writer, ":{}", string_or_value!(port));
            }

            wln!(writer);
        }
    }

    // HTTP HEADERS
    if let Some(headers) = req_map.remove("headers") {
        write_headers(writer, &headers);
    }

    // HTTP BODY
    if let Some(body) = req_map.remove("body") {
        if let Some(body_map) = body.as_object() {
            let pretty = ::serde_json::to_string_pretty(&body_map)
                .unwrap_or_else(|_| "[malformed]".to_string());
            for line in pretty.lines() {
                wln!(writer, "{:indent$}{}", "", line, indent = BASE_INDENT_SIZE);
            }
        } else {
            let body_text = string_or_value!(body);
            wln!(
                writer,
                "{:indent$}{}",
                "",
                body_text,
                indent = BASE_INDENT_SIZE
            );
        }
    }

    // HTTP TRAILER HEADERS
    if let Some(trailers) = req_map.remove("trailers") {
        write_headers(writer, &trailers);
    }
}

/// Converts the passed JSON value to an unsigned integer converting a numeric string or a
/// JSON numeric type.
///
/// # Errors
///
/// If there are any problems converting the value to an unsigned 16 bit integer, then
/// `None` will be returned.
///
fn json_string_or_number_as_u16(val: &Value) -> Result<u16, ParseIntFromJsonError> {
    match val {
        Value::Number(number) => {
            if let Some(code) = number.as_u64() {
                if code > u64::from(u16::MAX) {
                    let err = BunyanLogParseError::new(format!(
                        "Number is greater than u16 bounds: {}",
                        code
                    ));
                    Err(ParseIntFromJsonError::Structural(err))
                } else {
                    Ok(code as u16)
                }
            } else {
                let err = BunyanLogParseError::new(format!(
                    "Number can't be converted to u64: {}",
                    string_or_value!(val)
                ));
                Err(ParseIntFromJsonError::Structural(err))
            }
        }
        Value::String(string) => {
            let code = string.parse::<u16>();

            match code {
                Ok(val) => Ok(val),
                Err(e) => Err(ParseIntFromJsonError::Numeric(e)),
            }
        }
        Value::Null => {
            let err = BunyanLogParseError::new("Integers can't be parsed from null nodes");
            Err(ParseIntFromJsonError::Structural(err))
        }
        Value::Object(_) => {
            let err = BunyanLogParseError::new("Integers can't be parsed from JSON objects");
            Err(ParseIntFromJsonError::Structural(err))
        }
        Value::Array(_) => {
            let err = BunyanLogParseError::new("Integers can't be parsed from JSON arrays");
            Err(ParseIntFromJsonError::Structural(err))
        }
        Value::Bool(_) => {
            let err = BunyanLogParseError::new("Integers can't be parsed from boolean values");
            Err(ParseIntFromJsonError::Structural(err))
        }
    }
}

/// Writes the HTTP response information logged for the line.
///
/// # Arguments
///
/// * `writer` - Write implementation to output data to
/// * `other` - Map containing all non-explicitly deserialized keys and values
///
fn write_res<W: Write>(writer: &mut W, key: &str, other: &mut Map<String, Value>) {
    /// Searches the passed map for the key `headers` and then `header` returning whichever
    /// is found first and is a valid string or JSON object. Otherwise, `None` is returned.
    fn find_headers(map: &mut Map<String, Value>) -> Option<Value> {
        if let Some(headers) = map.remove("headers") {
            if headers.is_string() || headers.is_object() {
                return Some(headers);
            }
        }

        if let Some(headers) = map.remove("header") {
            if headers.is_string() || headers.is_object() {
                return Some(headers);
            }
        }

        None
    }

    /// Converts the passed JSON value and writes it out as a HTTP status code.
    ///
    /// # Errors
    ///
    /// If we can't parse the status code, then we behave as if there was no status code logged.
    ///
    fn write_res_status_code<W: Write>(
        writer: &mut W,
        optional_code: Option<Value>,
        option_http_version: Option<&str>,
    ) {
        let numeric_status_code = if let Some(json_value) = optional_code {
            match json_string_or_number_as_u16(&json_value) {
                Err(_) => None,
                Ok(number) => Some(number),
            }
        } else {
            None
        };

        if let Some(code) = numeric_status_code {
            let http_version = option_http_version.unwrap_or(DEFAULT_HTTP_VERSION);
            let http_status = format!("HTTP/{}", http_version);

            w!(
                writer,
                "{:indent$}{}",
                "",
                http_status.cyan(),
                indent = BASE_INDENT_SIZE
            );

            let color = if (100..=199).contains(&code) {
                "blue"
            } else if (200..=299).contains(&code) {
                "green"
            } else if (300..=399).contains(&code) {
                "magenta"
            } else if (400..=499).contains(&code) {
                "yellow"
            } else if (500..=599).contains(&code) {
                "red"
            } else {
                "white"
            };

            let status_code = StatusCode::from(code);
            let response_status = format!(" {} {}", code, status_code.reason_phrase());
            w!(writer, "{}", response_status.color(color));
            wln!(writer);
        }
    }

    let res_option = other.remove(key);

    // If there is no res key, then just exit right away because there is nothing to do
    if res_option.is_none() {
        return;
    }

    let mut res = res_option.unwrap();

    if !res.is_object() {
        return;
    }

    let res_map = res.as_object_mut().unwrap();

    // HEADERS
    if let Some(ref headers) = find_headers(res_map) {
        match headers {
            Value::String(headers_str) => {
                let http_version = if headers_str.starts_with("HTTP/") {
                    Some(&headers_str[5..8])
                } else {
                    None
                };

                write_res_status_code(writer, res_map.remove("statusCode"), http_version);

                let lines = headers_str.lines();

                for line in lines {
                    if line.is_empty() {
                        continue;
                    }
                    wln!(writer, "{:indent$}{}", "", line, indent = BASE_INDENT_SIZE);
                }
            }
            Value::Object(_) => {
                write_res_status_code(writer, res_map.remove("statusCode"), None);
                write_headers(writer, headers);
            }
            _ => (),
        }
    // Attempt to write out the status code line, even if we don't have headers
    } else {
        write_res_status_code(writer, res_map.remove("statusCode"), None);
    }

    // BODY
    if let Some(body_val) = res_map.remove("body") {
        let body = string_or_value!(body_val);

        if !body.is_empty() {
            wln!(writer);
            for line in body.lines() {
                wln!(writer, "{:indent$}{}", "", line, indent = BASE_INDENT_SIZE);
            }
        }
    }
}

/// Writes the contents of `headers` or `header` in the passed map.
///
/// # Arguments
///
/// * `writer` - Write implementation to output data to
/// * `caller_name` - text indicating if we have been invoked from a "req" or "client_req" code path
/// * `headers` - Mutable map containing header(s) keys. Keys will be removed as processed.
///
fn write_headers<W: Write>(writer: &mut W, headers: &Value) {
    match headers {
        Value::String(headers_string) => {
            for line in headers_string.lines() {
                if line.trim().is_empty() {
                    continue;
                }

                wln!(writer, "{:indent$}{}", "", line, indent = BASE_INDENT_SIZE);
            }
        }
        Value::Object(headers_map) => {
            for (k, v) in headers_map.iter() {
                w!(writer, "{:indent$}{}:", "", k, indent = BASE_INDENT_SIZE);

                let mut is_first = true;

                for line in string_or_value!(v).lines() {
                    if is_first {
                        wln!(writer, " {}", line);
                        is_first = false;
                    } else {
                        wln!(writer, "{:indent$}{}", "", line, indent = BASE_INDENT_SIZE);
                    }
                }
            }
        }
        _ => (),
    }
}

/// Writes the error information for the log line.
///
/// # Arguments
///
/// * `writer` - Write implementation to output data to
/// * `other` - Map containing all non-explicitly deserialized keys and values
///
fn write_err<W: Write>(writer: &mut W, other: &mut Map<String, Value>) {
    let err_option = other.remove("err");

    if err_option.is_none() {
        return;
    }

    let mut err = err_option.unwrap();

    if !err.is_object() {
        return;
    }

    let err_map = err.as_object_mut().unwrap();

    if let Some(ref stack_val) = err_map.remove("stack") {
        match stack_val {
            Value::String(stack_str) => {
                for line in stack_str.lines() {
                    wln!(writer, "{:indent$}{}", "", line, indent = BASE_INDENT_SIZE);
                }
            }
            Value::Array(stack_array) => {
                for line in stack_array.iter() {
                    wln!(
                        writer,
                        "{:indent$}{}",
                        "",
                        string_or_value!(line),
                        indent = BASE_INDENT_SIZE
                    );
                }
            }
            _ => {
                let pretty = ::serde_json::to_string_pretty(&stack_val)
                    .unwrap_or_else(|_| "[malformed]".to_string());
                for line in pretty.lines() {
                    wln!(writer, "{:indent$}{}", "", line, indent = BASE_INDENT_SIZE);
                }
            }
        }
    }
}

/// Writes the accumulated "details parameters" that do not properly fit in the "extra parameters"
/// section of the output.
///
/// # Arguments
///
/// * `writer` - Write implementation to output data to
/// * `details` - Vector containing the parameters to write out each in their own section
///
fn write_details<W: Write>(divider_writer: &mut DividerWriter<W>, details: Vec<String>) {
    for item in details {
        for line in item.lines() {
            wln!(
                divider_writer,
                "{:indent$}{}",
                "",
                line,
                indent = BASE_INDENT_SIZE
            );
        }

        if divider_writer.has_been_written {
            divider_writer.mark_divider_as_unwritten();
        }
    }
}

/// Validates that the passed `BunyanLine` is of the correct structure where it can be parsed
/// without problems.
///
/// # Arguments
///
/// * `line` - log line to validate
///
/// # Errors
///
/// This function will return None if no errors have been encountered. In the case of parsing logic
/// errors where the JSON data is not in the expected format, it will return a
/// `Option<BunyanLogParseError>`.
fn validate_log_data_structure(line: &BunyanLine) -> Option<BunyanLogParseError> {
    fn find_headers(map: &Map<String, Value>) -> Option<&Value> {
        if let Some(headers) = map.get("headers") {
            if headers.is_string() || headers.is_object() {
                return Some(headers);
            }
        }

        if let Some(headers) = map.get("header") {
            if headers.is_string() || headers.is_object() {
                return Some(headers);
            }
        }

        None
    }

    // Validate src
    if let Some(ref src) = line.other.get("src") {
        match src {
            Value::Object(src_map) => {
                if let Some(file) = src_map.get("file") {
                    if !file.is_string() {
                        return Some(BunyanLogParseError::new("[src.file] must be a string"));
                    }
                }
                if let Some(line) = src_map.get("line") {
                    if !(line.is_string() || line.is_number()) {
                        return Some(BunyanLogParseError::new(
                            "[src.line] must be a number or string",
                        ));
                    }
                }
                if let Some(func) = src_map.get("func") {
                    if !func.is_string() {
                        return Some(BunyanLogParseError::new("[src.func] must be a string"));
                    }
                }
            }
            Value::String(_) => (),
            _ => {
                return Some(BunyanLogParseError::new(
                    "[src] value must be a JSON object or string",
                ));
            }
        }
    }

    // Validate req_id
    if let Some(req_id) = line.other.get("req_id") {
        if !(req_id.is_string() || req_id.is_number() || req_id.is_null()) {
            return Some(BunyanLogParseError::new(
                "[req_id] must be a string or number",
            ));
        }
    }

    // Validate req
    if let Some(client_req) = line.other.get("req") {
        if let Some(body) = client_req.get("body") {
            if body.is_array() {
                return Some(BunyanLogParseError::new(
                    "[req.body] value must be not be an array",
                ));
            }
        }
    }

    // Validate client_req
    if let Some(client_req) = line.other.get("client_req") {
        if let Some(address) = client_req.get("address") {
            if !(address.is_string() || address.is_number() || address.is_null()) {
                return Some(BunyanLogParseError::new(
                    "[client_req.address] value must be a string",
                ));
            }
        }
        if let Some(port) = client_req.get("port") {
            if !(port.is_string() || port.is_number() || port.is_null()) {
                return Some(BunyanLogParseError::new(
                    "[client_req.port] value must be a string or number",
                ));
            }
        }

        if let Some(body) = client_req.get("body") {
            if body.is_array() {
                return Some(BunyanLogParseError::new(
                    "[client_req.body] value must be not be an array",
                ));
            }
        }
    }

    // Validate res
    if let Some(res) = line.other.get("res") {
        if let Some(res_map) = res.as_object() {
            if let Some(headers) = find_headers(res_map) {
                if !(headers.is_object() || headers.is_string()) {
                    return Some(BunyanLogParseError::new(
                        "[res.header(s)] must be a JSON object or string",
                    ));
                }
            }
        }

        if let Some(status_code) = res.get("statusCode") {
            if let Err(e) = json_string_or_number_as_u16(status_code) {
                let msg = format!("Invalid status code on res: {}", e);
                return Some(BunyanLogParseError::new(msg));
            }
        }
    }

    // Validate client_res
    if let Some(res) = line.other.get("client_res") {
        if let Some(res_map) = res.as_object() {
            if let Some(headers) = find_headers(res_map) {
                if !(headers.is_object() || headers.is_string()) {
                    return Some(BunyanLogParseError::new(
                        "[client_res.header(s)] must be a JSON object or string",
                    ));
                }
            }
            if let Some(body) = res.get("body") {
                if body.is_array() {
                    return Some(BunyanLogParseError::new(
                        "[client_res.body] value must be not be an array",
                    ));
                }
                if body.is_object() {
                    return Some(BunyanLogParseError::new(
                        "[client_res.body] value must be not be a JSON object",
                    ));
                }
            }
        }

        if let Some(status_code) = res.get("statusCode") {
            if let Err(e) = json_string_or_number_as_u16(status_code) {
                let msg = format!("Invalid status code on client_res: {}", e);
                return Some(BunyanLogParseError::new(msg));
            }
        }
    }

    None
}

impl Logger for BunyanLine {
    fn write_long_format<W: Write>(
        &self,
        writer: &mut W,
        _output_config: &LoggerOutputConfig,
    ) -> ParseResult {
        fn colorize_log_level(level: LogLevel) -> String {
            match level {
                LogLevel::TRACE => level.to_string(),
                LogLevel::DEBUG => level.to_string().yellow().to_string(),
                LogLevel::INFO => level.to_string().cyan().to_string(),
                LogLevel::WARN => level.to_string().magenta().to_string(),
                LogLevel::ERROR => level.to_string().red().to_string(),
                LogLevel::FATAL => level.to_string().reverse().to_string(),
                LogLevel::OTHER(_code) => level.to_string(),
            }
        }

        if let Some(err) = validate_log_data_structure(self) {
            return Err(err);
        }

        let log_level: LogLevel = self.level.into();

        // Write the [time]
        let time = if _output_config.display_local_time {
            self.time
                .with_timezone(&Local)
                .to_rfc3339_opts(SecondsFormat::Millis, true)
        } else {
            self.time.to_rfc3339_opts(SecondsFormat::Millis, true)
        };

        w!(
            writer,
            "{}{}{}",
            "[".blue(),
            time.bright_white(),
            "]".blue()
        );

        // write the log [level] and app [name]
        w!(writer, " {}: {}/", colorize_log_level(log_level), self.name);

        // If present, write the [component]
        if let Some(ref component) = self.component {
            w!(writer, "{}/", component);
        }

        // Write the [pid] and [hostname]
        w!(writer, "{} on {}", self.pid, self.hostname);

        let other = &mut self.other.clone();

        // If present, write the source line reference [src]
        write_src(writer, other);

        let mut details: Vec<String> = Vec::new();

        // If our log message [msg] contains a line break, we display it in the details section
        if self.msg.contains('\n') {
            let indented_msg = format!("{:indent$}{}", "", self.msg, indent = BASE_INDENT_SIZE);
            details.push(indented_msg)
        // Write the log message [msg] as is because there is no line break
        } else if !self.msg.is_empty() {
            w!(writer, ": {}", self.msg.cyan());
        } else {
            w!(writer, ":");
        }

        write_all_extra_params(writer, other, &mut details);

        // Write line feed finishing the first line
        wln!(writer);

        let wrapped_writer = &mut DividerWriter::new(writer, true);

        // If present, write the request [req]
        write_req(wrapped_writer, "req", other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // If present, write the client request [client_req]
        write_req(wrapped_writer, "client_req", other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // If present, write the response [res]
        write_res(wrapped_writer, "res", other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // If present, write the response [client_res]
        write_res(wrapped_writer, "client_res", other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // If present, write the error information [err]
        write_err(wrapped_writer, other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // Write out all of the values stored in the details vector
        write_details(wrapped_writer, details);

        Ok(())
    }

    fn write_short_format<W: Write>(
        &self,
        writer: &mut W,
        _output_config: &LoggerOutputConfig,
    ) -> ParseResult {
        pub fn right_align_and_colorize_log_level(level: LogLevel) -> String {
            match level {
                LogLevel::TRACE => format!("{: >5}", level),
                LogLevel::DEBUG => format!("{: >5}", level).yellow().to_string(),
                LogLevel::INFO => format!("{: >5}", level).cyan().to_string(),
                LogLevel::WARN => format!("{: >5}", level).magenta().to_string(),
                LogLevel::ERROR => format!("{: >5}", level).red().to_string(),
                LogLevel::FATAL => format!("{: >5}", level).reverse().to_string(),
                LogLevel::OTHER(_code) => level.to_string(),
            }
        }

        if let Some(err) = validate_log_data_structure(self) {
            return Err(err);
        }

        let log_level: LogLevel = self.level.into();

        // Write the [time]
        let time = if _output_config.display_local_time {
            self.time.with_timezone(&Local).format("%H:%M:%S%.3f")
        } else {
            self.time.format("%H:%M:%S%.3fZ")
        }
        .to_string();

        w!(writer, "{}", time.bright_white());

        // write the log [level] and app [name]
        let level_right_indented = right_align_and_colorize_log_level(log_level);
        w!(writer, " {} {}", level_right_indented, self.name);

        let other = &mut self.other.clone();

        // If present, write the source line reference [src]
        write_src(writer, other);

        let mut details: Vec<String> = Vec::new();

        // If our log message [msg] contains a line break, we display it in the details section
        if self.msg.contains('\n') {
            let indented_msg = format!("{:indent$}{}", "", self.msg, indent = BASE_INDENT_SIZE);
            details.push(indented_msg)
        // Write the log message [msg] as is because there is no line break
        } else if !self.msg.is_empty() {
            w!(writer, ": {}", self.msg.cyan());
        } else {
            w!(writer, ":");
        }

        write_all_extra_params(writer, other, &mut details);

        // Write line feed finishing the first line
        wln!(writer);

        let wrapped_writer = &mut DividerWriter::new(writer, true);

        // If present, write the request [req]
        write_req(wrapped_writer, "req", other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // If present, write the client request [client_req]
        write_req(wrapped_writer, "client_req", other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // If present, write the response [res]
        write_res(wrapped_writer, "res", other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // If present, write the response [client_res]
        write_res(wrapped_writer, "client_res", other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // If present, write the error information [err]
        write_err(wrapped_writer, other);

        if wrapped_writer.has_been_written {
            wrapped_writer.mark_divider_as_unwritten();
        }

        // Write out all of the values stored in the details vector
        write_details(wrapped_writer, details);

        Ok(())
    }

    fn write_simple_format<W: Write>(
        &self,
        writer: &mut W,
        _output_config: &LoggerOutputConfig,
    ) -> ParseResult {
        if let Some(err) = validate_log_data_structure(self) {
            return Err(err);
        }

        let log_level: LogLevel = self.level.into();

        // write the log [level]
        wln!(writer, "{} - {}", log_level.as_string(), self.msg);

        Ok(())
    }
}