Skip to main content

rs_zero/core/logging/
config.rs

1use crate::core::logging::{redaction::RedactionConfig, writer::LogWriterConfig};
2
3/// Log output format.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum LogFormat {
6    /// Human-readable text logs.
7    Text,
8    /// Structured JSON logs.
9    Json,
10}
11
12/// Span lifecycle events emitted by the formatter.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum LogSpanEvents {
15    /// Do not emit span lifecycle events.
16    None,
17    /// Emit span close events.
18    Close,
19    /// Emit span creation and close events.
20    NewAndClose,
21    /// Emit all supported span lifecycle events.
22    Full,
23}
24
25/// Logging configuration for rs-zero services.
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct LogConfig {
28    /// Env filter directive, for example `info,tower_http=debug`.
29    pub filter: String,
30    /// Whether ANSI colors should be emitted for terminal writers.
31    pub ansi: bool,
32    /// Output format.
33    pub format: LogFormat,
34    /// Whether tracing targets should be emitted.
35    pub with_target: bool,
36    /// Whether the current span should be included in JSON logs.
37    pub include_current_span: bool,
38    /// Whether the full span list should be included in JSON logs.
39    pub include_span_list: bool,
40    /// Span lifecycle event verbosity.
41    pub span_events: LogSpanEvents,
42    /// Optional logical service name.
43    pub service: Option<String>,
44    /// Writer configuration.
45    pub writer: LogWriterConfig,
46    /// Subscriber-layer redaction applied before records reach the writer.
47    pub redaction: RedactionConfig,
48}
49
50impl Default for LogConfig {
51    fn default() -> Self {
52        Self {
53            filter: "info".to_string(),
54            ansi: true,
55            format: LogFormat::Text,
56            with_target: true,
57            include_current_span: true,
58            include_span_list: false,
59            span_events: LogSpanEvents::Close,
60            service: None,
61            writer: LogWriterConfig::Stdout,
62            redaction: RedactionConfig::default(),
63        }
64    }
65}
66
67impl LogConfig {
68    /// Returns a config using `RUST_LOG` when available, otherwise `info`.
69    pub fn from_env() -> Self {
70        Self {
71            filter: std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string()),
72            ..Self::default()
73        }
74    }
75
76    /// Sets the output format.
77    pub fn with_format(mut self, format: LogFormat) -> Self {
78        self.format = format;
79        self
80    }
81
82    /// Sets the logical service name.
83    pub fn with_service(mut self, service: impl Into<String>) -> Self {
84        self.service = Some(service.into());
85        self
86    }
87
88    /// Sets the log writer.
89    pub fn with_writer(mut self, writer: LogWriterConfig) -> Self {
90        self.writer = writer;
91        self
92    }
93
94    /// Sets subscriber-layer redaction.
95    pub fn with_redaction(mut self, redaction: RedactionConfig) -> Self {
96        self.redaction = redaction;
97        self
98    }
99}