src/handlers/formatters/iso8601_formatter.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource
1
//
2
// File Name:    iso8601_formatter.rs
3
// Directory:    src/handlers/formatters
4
// Project Name: flogging
5
//
6
// Copyright (C) 2025 Bradley Willcott
7
//
8
// SPDX-License-Identifier: GPL-3.0-or-later
9
//
10
// This library (crate) is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
14
//
15
// This library (crate) is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
// GNU General Public License for more details.
19
//
20
// You should have received a copy of the GNU General Public License
21
// along with this library (crate).  If not, see <https://www.gnu.org/licenses/>.
22
//
23
24
//!
25
//! # Iso8601 Formatter
26
//!
27
28
use std::fmt;
29
use crate::FormatTrait;
30
31
///
32
/// ISO 8601 / RFC 3339 date & time format.
33
///
34
/// Example:
35
/// ```text
36
/// 2001-07-08T00:34:60.026490+09:30
37
/// ```
38
/// Template:
39
/// - `dt` in the template would be the datetime string, similar to the above.
40
/// - `mod_path`, `fn_name`, `level`, and `message` all come out of the `LogEntry`
41
///   provided to the [`format()`][Iso8601Formatter::format] method.
42
///
43
/// ```ignore
44
/// format!("{dt:35} {mod_path}->{fn_name} [{level:7}] {message}");
45
/// ```
46
/// Sample output:
47
/// ```text
48
/// 2025-07-18T14:01:01.051532664+08:00 flogging->main [WARNING] Rain is wet!
49
/// ```
50
///
51
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
52
pub struct Iso8601Formatter {
53
    dt_fmt: String,
54
    fmt_string: String,
55
}
56
57
impl Iso8601Formatter {
58
    ///
59
    /// Creates a new instance of `Iso8601Formatter`.
60
    ///
6124
    pub fn new() -> Self {
6224
        Self {
6324
            dt_fmt: "%+".to_string(),
6424
            fmt_string: "{dt:35} {mod_path}->{fn_name} [{level:7}] {message}".to_string(),
6524
        }
6624
    }
67
68
    ///
69
    /// Returns the date/time format string.
70
    ///
7157
    pub fn dt_fmt(&self) -> String {
7257
        self.dt_fmt.clone()
7357
    }
74
75
    ///
76
    /// Returns the primary format string.
77
    ///
7857
    pub fn fmt_string(&self) -> String {
7957
        self.fmt_string.clone()
8057
    }
81
}
82
83
impl Default for Iso8601Formatter {
8424
    fn default() -> Self {
8524
        Self::new()
8624
    }
87
}
88
89
impl fmt::Display for Iso8601Formatter {
903
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
913
        write!(
923
            f,
933
            "dt_fmt: \"{}\" - fmt_string: \"{}\"",
94
            self.dt_fmt, self.fmt_string
95
        )
963
    }
97
}
98
99
impl FormatTrait for Iso8601Formatter {
10057
    fn format(&self, log_entry: &crate::LogEntry) -> String {
10157
        self.ft_fmt(self.dt_fmt(), self.fmt_string(), log_entry)
10257
    }
103
}