Skip to main content

reth_tracing/
formatter.rs

1use crate::layers::BoxedLayer;
2use clap::ValueEnum;
3use std::{fmt, fmt::Display};
4use tracing_appender::non_blocking::NonBlocking;
5use tracing_subscriber::{layer::Filter, Layer, Registry};
6
7/// Represents the logging format.
8///
9/// This enum defines the supported formats for logging output.
10/// It is used to configure the format layer of a tracing subscriber.
11#[derive(Debug, Copy, Clone, ValueEnum, Eq, PartialEq)]
12pub enum LogFormat {
13    /// Represents JSON formatting for logs.
14    /// This format outputs log records as JSON objects,
15    /// making it suitable for structured logging.
16    Json,
17
18    /// Represents logfmt (key=value) formatting for logs.
19    /// This format is concise and human-readable,
20    /// typically used in command-line applications.
21    LogFmt,
22
23    /// Represents terminal-friendly formatting for logs.
24    Terminal,
25}
26
27impl LogFormat {
28    /// Applies the specified logging format to create a new layer.
29    ///
30    /// # Arguments
31    /// * `filter` - A filter layer (either `EnvFilter` or a `reload::Layer` wrapping one).
32    /// * `color` - An optional string that enables or disables ANSI color codes in the logs.
33    /// * `show_target` - Whether to show the target module path in log output.
34    /// * `file_writer` - An optional [`NonBlocking`] writer for directing logs to a file. When
35    ///   `None`, logs are written to stdout.
36    ///
37    /// # Returns
38    /// A `BoxedLayer<Registry>` that can be added to a tracing subscriber.
39    pub fn apply<F>(
40        &self,
41        filter: F,
42        color: Option<String>,
43        show_target: bool,
44        file_writer: Option<NonBlocking>,
45    ) -> BoxedLayer<Registry>
46    where
47        F: Filter<Registry> + Send + Sync + 'static,
48    {
49        let ansi = if let Some(color) = color {
50            std::env::var("RUST_LOG_STYLE").map(|val| val != "never").unwrap_or(color != "never")
51        } else {
52            false
53        };
54
55        let target = std::env::var("RUST_LOG_TARGET").map(|val| val != "0").unwrap_or(show_target);
56
57        match self {
58            Self::Json => {
59                let layer =
60                    tracing_subscriber::fmt::layer().json().with_ansi(ansi).with_target(target);
61                if let Some(writer) = file_writer {
62                    layer.with_writer(writer).with_filter(filter).boxed()
63                } else {
64                    layer.with_filter(filter).boxed()
65                }
66            }
67            Self::LogFmt => {
68                let layer = tracing_logfmt::builder().layer();
69                if let Some(writer) = file_writer {
70                    layer.with_writer(writer).with_filter(filter).boxed()
71                } else {
72                    layer.with_filter(filter).boxed()
73                }
74            }
75            Self::Terminal => {
76                let layer = tracing_subscriber::fmt::layer().with_ansi(ansi).with_target(target);
77                if let Some(writer) = file_writer {
78                    layer.with_writer(writer).with_filter(filter).boxed()
79                } else {
80                    layer.with_filter(filter).boxed()
81                }
82            }
83        }
84    }
85}
86
87impl Display for LogFormat {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        match self {
90            Self::Json => write!(f, "json"),
91            Self::LogFmt => write!(f, "logfmt"),
92            Self::Terminal => write!(f, "terminal"),
93        }
94    }
95}