censer 0.1.0

Beautiful, structured logging for the terminal ⚒️
Documentation
//! Log styling options.

use glyphs::Color;

/// Styling options for log output.
#[derive(Debug, Clone)]
pub struct LogStyle {
    /// Show timestamps.
    pub timestamp: bool,
    /// Timestamp format.
    pub timestamp_format: String,
    /// Show log level.
    pub show_level: bool,
    /// Use short level names.
    pub short_level: bool,
    /// Show icons.
    pub show_icons: bool,
    /// Timestamp color.
    pub timestamp_color: Color,
    /// Key color.
    pub key_color: Color,
    /// Value color.
    pub value_color: Color,
    /// Message color.
    pub message_color: Color,
    /// Separator between key-value pairs.
    pub separator: String,
    /// Key-value separator.
    pub kv_separator: String,
}

impl Default for LogStyle {
    fn default() -> Self {
        Self {
            timestamp: false,
            timestamp_format: "%H:%M:%S".to_string(),
            show_level: true,
            short_level: false,
            show_icons: false,
            timestamp_color: Color::from_hex("#71717A"),
            key_color: Color::from_hex("#A1A1AA"),
            value_color: Color::from_hex("#E4E4E7"),
            message_color: Color::from_hex("#FAFAFA"),
            separator: " ".to_string(),
            kv_separator: "=".to_string(),
        }
    }
}

impl LogStyle {
    /// Create a new style.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable timestamps.
    #[must_use]
    pub fn with_timestamp(mut self) -> Self {
        self.timestamp = true;
        self
    }

    /// Set timestamp format.
    #[must_use]
    pub fn timestamp_format(mut self, format: impl Into<String>) -> Self {
        self.timestamp_format = format.into();
        self
    }

    /// Use short level names.
    #[must_use]
    pub fn short_level(mut self) -> Self {
        self.short_level = true;
        self
    }

    /// Show icons.
    #[must_use]
    pub fn with_icons(mut self) -> Self {
        self.show_icons = true;
        self
    }

    /// Hide level.
    #[must_use]
    pub fn hide_level(mut self) -> Self {
        self.show_level = false;
        self
    }

    /// Set key color.
    #[must_use]
    pub fn key_color(mut self, color: Color) -> Self {
        self.key_color = color;
        self
    }

    /// Minimal style - just message.
    pub fn minimal() -> Self {
        Self {
            show_level: false,
            ..Default::default()
        }
    }

    /// Full style - everything.
    pub fn full() -> Self {
        Self {
            timestamp: true,
            show_icons: true,
            ..Default::default()
        }
    }

    /// JSON-like style.
    pub fn json_like() -> Self {
        Self {
            kv_separator: ": ".to_string(),
            separator: ", ".to_string(),
            ..Default::default()
        }
    }
}