Skip to main content

rustvello_core/
logging.rs

1//! Logging configuration types for rustvello.
2//!
3//! Provides [`LogConfig`] for controlling log format, level, and output.
4//! The actual subscriber initialization (`init_logging`) lives in
5//! the `rustvello` application crate (`rustvello::logging`).
6
7use serde::{Deserialize, Serialize};
8
9/// Log output format.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11#[non_exhaustive]
12pub enum LogFormat {
13    /// Human-readable text with optional ANSI colors.
14    Text,
15    /// Structured JSON (one object per line).
16    Json,
17}
18
19impl Default for LogFormat {
20    fn default() -> Self {
21        Self::Text
22    }
23}
24
25/// Output stream selection.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27#[non_exhaustive]
28pub enum LogStream {
29    Stderr,
30    Stdout,
31}
32
33impl Default for LogStream {
34    fn default() -> Self {
35        Self::Stderr
36    }
37}
38
39/// Configuration for logging output.
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[non_exhaustive]
42pub struct LogConfig {
43    /// Minimum log level (trace, debug, info, warn, error).
44    pub level: String,
45    /// Output format.
46    pub format: LogFormat,
47    /// Whether to use ANSI colors in text format (None = auto-detect TTY).
48    pub use_colors: Option<bool>,
49    /// Whether to show compact context (abbreviated class names, truncated IDs).
50    pub compact_context: bool,
51    /// Output stream: stderr (default) or stdout.
52    pub stream: LogStream,
53}
54
55impl Default for LogConfig {
56    fn default() -> Self {
57        Self {
58            level: "info".to_string(),
59            format: LogFormat::Text,
60            use_colors: None,
61            compact_context: true,
62            stream: LogStream::Stderr,
63        }
64    }
65}
66
67impl LogConfig {
68    /// Build a [`LogConfig`] from an [`AppConfig`]-style set of fields.
69    pub fn from_app_fields(
70        level: &str,
71        format: LogFormat,
72        use_colors: Option<bool>,
73        compact_context: bool,
74    ) -> Self {
75        Self {
76            level: level.to_owned(),
77            format,
78            use_colors,
79            compact_context,
80            stream: LogStream::Stderr,
81        }
82    }
83}