Skip to main content

nginx_discovery/types/
access_log.rs

1//! Access log type
2
3use std::collections::HashMap;
4use std::path::PathBuf;
5
6/// Represents an NGINX `access_log` directive
7#[derive(Debug, Clone, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct AccessLog {
10    /// Path to the log file
11    pub path: PathBuf,
12
13    /// Format name (e.g., "combined", "main")
14    pub format_name: Option<String>,
15
16    /// Additional options (buffer, gzip, etc.)
17    pub options: HashMap<String, String>,
18
19    /// Context where this log was defined
20    pub context: LogContext,
21}
22
23/// Context where a log directive appears
24#[derive(Debug, Clone, PartialEq, Eq)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26pub enum LogContext {
27    /// Main/http context
28    Main,
29    /// Server block
30    Server(String),
31    /// Location block
32    Location(String),
33}
34
35impl AccessLog {
36    /// Create a new access log
37    #[must_use]
38    pub fn new(path: impl Into<PathBuf>) -> Self {
39        Self {
40            path: path.into(),
41            format_name: None,
42            options: HashMap::new(),
43            context: LogContext::Main,
44        }
45    }
46
47    /// Set the format name
48    #[must_use]
49    pub fn with_format(mut self, format: impl Into<String>) -> Self {
50        self.format_name = Some(format.into());
51        self
52    }
53
54    /// Set the context
55    #[must_use]
56    pub fn with_context(mut self, context: LogContext) -> Self {
57        self.context = context;
58        self
59    }
60
61    /// Add an option
62    #[must_use]
63    pub fn with_option(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
64        self.options.insert(key.into(), value.into());
65        self
66    }
67}