rustfoundry/telemetry/settings/
logging.rs

1use crate::telemetry::settings::rate_limit::RateLimitingSettings;
2use crate::utils::feature_use;
3
4use std::path::PathBuf;
5
6feature_use!(cfg(feature = "settings"), {
7    use crate::settings::settings;
8});
9
10// NOTE: we technically don't need a feature gate here, but if we don't add it then docs don't
11// mark this re-export as available on when `logging` is enabled.
12#[cfg(feature = "logging")]
13pub use slog::Level;
14
15/// Logging settings.
16#[cfg_attr(feature = "settings", settings(crate_path = "crate"))]
17#[cfg_attr(not(feature = "settings"), derive(Clone, Default, Debug))]
18pub struct LoggingSettings {
19    /// Specifies log output.
20    pub output: LogOutput,
21
22    /// The format to use for log messages.
23    pub format: LogFormat,
24
25    /// Set the logging verbosity level.
26    pub verbosity: LogVerbosity,
27
28    /// A list of field keys to redact when emitting logs.
29    ///
30    /// This might be useful to hide certain fields in production logs as they may
31    /// contain sensitive information, but allow them in testing environment.
32    pub redact_keys: Vec<String>,
33
34    /// Settings for rate limiting emission of log events
35    pub rate_limit: RateLimitingSettings,
36
37    /// Configure log volume metrics.
38    pub log_volume_metrics: LogVolumeMetricSettings,
39}
40
41/// Log output destination.
42#[cfg_attr(feature = "settings", settings(crate_path = "crate"))]
43#[cfg_attr(not(feature = "settings"), derive(Clone, Debug, Default))]
44pub enum LogOutput {
45    /// Write log to terminal.
46    #[default]
47    Terminal,
48    /// Write log to file with the specified path.
49    ///
50    /// File will be created if it doesn't exist and overwritten otherwise.
51    File(PathBuf),
52}
53
54/// Format of the log output.
55#[cfg_attr(feature = "settings", settings(crate_path = "crate"))]
56#[cfg_attr(not(feature = "settings"), derive(Clone, Default, Debug))]
57#[derive(Copy)]
58pub enum LogFormat {
59    /// Plain text
60    #[default]
61    Text,
62    /// JSON
63    Json,
64}
65
66/// Log verbosity levels which match 1:1 with [`slog::Level`].
67#[cfg_attr(
68    feature = "settings",
69    settings(crate_path = "crate", impl_default = false)
70)]
71#[cfg_attr(not(feature = "settings"), derive(Clone, Debug))]
72#[derive(Copy, Default)]
73pub enum LogVerbosity {
74    /// See [`slog::Level::Critical`].
75    #[cfg_attr(feature = "settings", serde(rename = "CRITICAL"))]
76    Critical,
77    /// See [`slog::Level::Error`].
78    #[cfg_attr(feature = "settings", serde(rename = "ERROR"))]
79    Error,
80    /// See [`slog::Level::Warning`].
81    #[cfg_attr(feature = "settings", serde(rename = "WARN"))]
82    Warning,
83    /// See [`slog::Level::Info`].
84    #[default]
85    #[cfg_attr(feature = "settings", serde(rename = "INFO"))]
86    Info,
87    /// See [`slog::Level::Debug`].
88    #[cfg_attr(feature = "settings", serde(rename = "DEBUG"))]
89    Debug,
90    /// See [`slog::Level::Trace`].
91    #[cfg_attr(feature = "settings", serde(rename = "TRACE"))]
92    Trace,
93}
94
95impl From<slog::Level> for LogVerbosity {
96    fn from(level: slog::Level) -> Self {
97        match level {
98            Level::Critical => Self::Critical,
99            Level::Warning => Self::Warning,
100            Level::Error => Self::Error,
101            Level::Info => Self::Info,
102            Level::Debug => Self::Debug,
103            Level::Trace => Self::Trace,
104        }
105    }
106}
107
108impl From<LogVerbosity> for slog::Level {
109    fn from(level: LogVerbosity) -> Self {
110        match level {
111            LogVerbosity::Critical => Self::Critical,
112            LogVerbosity::Warning => Self::Warning,
113            LogVerbosity::Error => Self::Error,
114            LogVerbosity::Info => Self::Info,
115            LogVerbosity::Debug => Self::Debug,
116            LogVerbosity::Trace => Self::Trace,
117        }
118    }
119}
120
121/// Log volume metrics settings
122///
123/// If enabled, a counter metric will be exposed as <app_name>_rustfoundry_log_record_count
124/// with a tag "level" indicating the log level.
125#[cfg_attr(feature = "settings", settings(crate_path = "crate"))]
126#[cfg_attr(not(feature = "settings"), derive(Clone, Debug, Default))]
127pub struct LogVolumeMetricSettings {
128    /// Whether to enable log volume metrics
129    pub enabled: bool,
130}