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
use std::str::FromStr;

use serde::Deserialize;
use thiserror::Error;
use tracing::Subscriber;
use tracing_subscriber::{
    field::RecordFields,
    fmt::{
        format::{
            Compact, DefaultFields, Format, Full, Json, JsonFields, Pretty, PrettyFields, Writer,
        },
        FormatEvent, FormatFields,
    },
    registry::LookupSpan,
};

#[derive(Debug, Clone)]
pub enum LogFormat {
    Full(Format<Full>),
    Compact(Format<Compact>),
    Pretty(Format<Pretty>),
    Json(Format<Json>),
}

impl LogFormat {
    pub fn get_format_fields(&self) -> LogFormatFields {
        match self {
            LogFormat::Json(_) => LogFormatFields::Json(JsonFields::default()),
            LogFormat::Pretty(_) => LogFormatFields::Pretty(PrettyFields::default()),
            _ => LogFormatFields::Default(DefaultFields::default()),
        }
    }
}

impl<'de> Deserialize<'de> for LogFormat {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let data: String = Deserialize::deserialize(deserializer)?;
        LogFormat::from_str(&data).map_err(serde::de::Error::custom)
    }
}

impl Default for LogFormat {
    fn default() -> Self {
        LogFormat::Full(Default::default())
    }
}

#[derive(Error, Debug)]
#[error("Invalid log format, must be one of full, compact, pretty, json")]
pub struct InvalidLogFormat;

impl FromStr for LogFormat {
    type Err = InvalidLogFormat;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "full" => Ok(LogFormat::Full(Default::default())),
            "compact" => Ok(LogFormat::Compact(Format::default().compact())),
            "pretty" => Ok(LogFormat::Pretty(Format::default().pretty())),
            "json" => Ok(LogFormat::Json(Format::default().json().with_ansi(false))),
            _ => Err(InvalidLogFormat),
        }
    }
}

impl<S, N> FormatEvent<S, N> for LogFormat
where
    S: Subscriber + for<'a> LookupSpan<'a>,
    N: for<'a> FormatFields<'a> + 'static,
{
    fn format_event(
        &self,
        ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
        writer: tracing_subscriber::fmt::format::Writer<'_>,
        event: &tracing::Event<'_>,
    ) -> std::fmt::Result {
        match self {
            LogFormat::Full(f) => f.format_event(ctx, writer, event),
            LogFormat::Compact(f) => f.format_event(ctx, writer, event),
            LogFormat::Pretty(f) => f.format_event(ctx, writer, event),
            LogFormat::Json(f) => f.format_event(ctx, writer, event),
        }
    }
}

pub enum LogFormatFields {
    Json(JsonFields),
    Pretty(PrettyFields),
    Default(DefaultFields),
}

impl<'a> FormatFields<'a> for LogFormatFields {
    fn format_fields<R: RecordFields>(&self, writer: Writer<'a>, fields: R) -> std::fmt::Result {
        match self {
            LogFormatFields::Json(f) => f.format_fields(writer, fields),
            LogFormatFields::Pretty(f) => f.format_fields(writer, fields),
            LogFormatFields::Default(f) => f.format_fields(writer, fields),
        }
    }
}