rlg/
log.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// log.rs
// Copyright © 2024 RustLogs (RLG). All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use crate::{Config, LogFormat, LogLevel, RlgError, RlgResult};
use dtt::datetime::DateTime;
use hostname;
use serde::{Deserialize, Serialize};
use std::{
    fmt::{self, Write as FmtWrite},
    io,
};
use tokio::{fs::OpenOptions, io::AsyncWriteExt};
use vrd::random::Random;

/// The `Log` struct provides an easy way to log a message to the console.
/// It contains a set of defined fields to create a simple log message with a readable output format.
#[derive(
    Debug,
    Clone,
    PartialEq,
    PartialOrd,
    Serialize,
    Deserialize,
    Eq,
    Hash,
)]
pub struct Log {
    /// The session ID for the log entry.
    pub session_id: String,
    /// The time the log entry was created.
    pub time: String,
    /// The log level of the message.
    pub level: LogLevel,
    /// The component that generated the log message.
    pub component: String,
    /// The description of the log message.
    pub description: String,
    /// The format of the log message.
    pub format: LogFormat,
}

impl Default for Log {
    fn default() -> Log {
        Log {
            session_id: String::default(),
            time: String::default(),
            level: LogLevel::INFO,
            component: String::default(),
            description: String::default(),
            format: LogFormat::CLF,
        }
    }
}

impl Log {
    /// Logs a message asynchronously using a pre-allocated buffer to reduce memory allocation.
    ///
    /// This function formats the log message according to the specified log format and writes it to
    /// both the log file and standard output. It ensures that the log file is flushed after every write
    /// to guarantee data persistence.
    ///
    /// # Returns
    /// * `RlgResult<()>` - Result with `Ok(())` if the logging succeeds, or `RlgError` if any errors occur.
    pub async fn log(&self) -> RlgResult<()> {
        let mut log_message = String::with_capacity(256);

        // Format the log message based on the specified log format.
        let write_result = match self.format {
        LogFormat::CLF => writeln!(
            log_message,
            "SessionID={} Timestamp={} Description={} Level={} Component={} Format=CLF",
            self.session_id, self.time, self.description, self.level, self.component
        ),
        LogFormat::JSON => writeln!(
            log_message,
            "{{\"SessionID\":\"{}\",\"Timestamp\":\"{}\",\"Level\":\"{}\",\"Component\":\"{}\",\"Description\":\"{}\",\"Format\":\"JSON\"}}",
            self.session_id, self.time, self.level, self.component, self.description
        ),
        LogFormat::CEF => writeln!(
            log_message,
            "CEF:0|{}|{}|{}|{}|{}|CEF",
            self.session_id, self.time, self.level, self.component, self.description
        ),
        _ => writeln!(log_message, "Unsupported format"),  // Handle unsupported formats
    };

        write_result.map_err(|e| {
            RlgError::FormattingError(format!(
                "Formatting error: {}",
                e
            ))
        })?;

        // Extract the log file path from the configuration.
        let log_file_path;
        {
            let config = Config::load_async(None::<&str>)
                .await
                .map_err(|e| {
                    RlgError::IoError(io::Error::new(
                        io::ErrorKind::Other,
                        e,
                    ))
                })?;
            log_file_path = config.read().log_file_path.clone();
        }

        // Open the log file for appending, or create it if it does not exist.
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&log_file_path)
            .await
            .map_err(|e| {
                RlgError::IoError(io::Error::new(
                    io::ErrorKind::Other,
                    format!("Failed to open log file: {}", e),
                ))
            })?;

        file.write_all(log_message.as_bytes()).await.map_err(|e| {
            RlgError::IoError(io::Error::new(
                io::ErrorKind::Other,
                format!("Failed to write to log file: {}", e),
            ))
        })?;

        file.flush().await.map_err(|e| {
            RlgError::IoError(io::Error::new(
                io::ErrorKind::Other,
                format!("Failed to flush log file: {}", e),
            ))
        })?;

        Ok(())
    }

    /// Creates a new log entry with provided details.
    pub fn new(
        session_id: &str,
        time: &str,
        level: &LogLevel,
        component: &str,
        description: &str,
        format: &LogFormat,
    ) -> Self {
        Self {
            session_id: session_id.to_string(),
            time: time.to_string(),
            level: *level,
            component: component.to_string(),
            description: description.to_string(),
            format: *format,
        }
    }

    /// Writes a log entry to the log file using the provided details.
    pub async fn write_log_entry(
        log_level: LogLevel,
        process: &str,
        message: &str,
        log_format: LogFormat,
    ) -> RlgResult<()> {
        let config = Config::load_async(None::<&str>).await?;

        // Open or create the log file
        let log_file_path = config.read().log_file_path.clone();
        let mut log_file = OpenOptions::new()
            .append(true)
            .create(true)
            .open(&log_file_path)
            .await
            .map_err(|e| {
                RlgError::IoError(io::Error::new(
                    io::ErrorKind::Other,
                    format!(
                        "Failed to open or create log file '{}': {}",
                        log_file_path.display(),
                        e
                    ),
                ))
            })?;

        // Create the log entry
        let log_entry = Log::new(
            &Random::default().int(0, 1_000_000_000).to_string(),
            &DateTime::new().to_string(),
            &log_level,
            process,
            message,
            &log_format,
        );

        // Format the log entry according to the specified log format
        let formatted_entry = log_entry.to_string();

        // Write the formatted log entry to the file asynchronously
        log_file
            .write_all(formatted_entry.as_bytes())
            .await
            .map_err(|e| {
                RlgError::IoError(io::Error::new(
                    io::ErrorKind::Other,
                    format!("Failed to write log entry: {}", e),
                ))
            })?;

        // Optionally, flush the file to ensure all data is written
        log_file.flush().await.map_err(|e| {
            RlgError::IoError(io::Error::new(
                io::ErrorKind::Other,
                format!("Failed to flush log file: {}", e),
            ))
        })?;

        Ok(())
    }
}

impl fmt::Display for Log {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.format {
            LogFormat::CLF => write!(
                f,
                "SessionID={} Timestamp={} Description={} Level={} Component={}",
                self.session_id, self.time, self.description, self.level, self.component
            ),
            LogFormat::JSON => write!(
                f,
                "{{\"SessionID\":\"{}\",\"Timestamp\":\"{}\",\"Level\":\"{}\",\"Component\":\"{}\",\"Description\":\"{}\",\"Format\":\"JSON\"}}",
                self.session_id, self.time, self.level, self.component, self.description
            ),
            LogFormat::CEF => write!(
                f,
                "CEF:0|{}|{}|{}|{}|{}|CEF",
                self.session_id, self.time, self.level, self.component, self.description
            ),
            LogFormat::ELF => write!(
                f,
                "ELF:0|{}|{}|{}|{}|{}|ELF",
                self.session_id, self.time, self.level, self.component, self.description
            ),
            LogFormat::W3C => write!(
                f,
                "W3C:0|{}|{}|{}|{}|{}|W3C",
                self.session_id, self.time, self.level, self.component, self.description
            ),
            LogFormat::GELF => write!(
                f,
                r#"{{
                    "version": "1.1",
                    "host": "{}",
                    "short_message": "{}",
                    "level": "{:?}",
                    "timestamp": "{}",
                    "component": "{}",
                    "session_id": "{}"
                }}"#,
                self.component, self.description, self.level, self.time, self.component, self.session_id
            ),
            LogFormat::ApacheAccessLog => write!(
                f,
                "{} - - [{}] \"{}\" {} {}",
                hostname::get().map_err(|_| fmt::Error)?.to_string_lossy(),
                self.time,
                self.description,
                self.level,
                self.component
            ),
            LogFormat::Logstash => write!(
                f,
                r#"{{
                    "@timestamp": "{}",
                    "level": "{}",
                    "component": "{}",
                    "message": "{}"
                }}"#,
                self.time, self.level, self.component, self.description
            ),
            LogFormat::Log4jXML => write!(
                f,
                r#"<log4j:event logger="{}" timestamp="{}" level="{}" thread="{}"><log4j:message>{}</log4j:message></log4j:event>"#,
                self.component, self.time, self.level, self.session_id, self.description
            ),
            LogFormat::NDJSON => write!(
                f,
                r#"{{
                    "timestamp": "{}",
                    "level": "{}",
                    "component": "{}",
                    "message": "{}"
                }}"#,
                self.time, self.level, self.component, self.description
            ),
        }
    }
}