rlg/macros.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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
// macros.rs
// Copyright © 2024 RustLogs (RLG). All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
// ======================
// Macros for Log Creation
// ======================
/// This macro simplifies the creation of log entries with specific parameters.
/// It returns a new `Log` instance based on the provided session ID, time, level,
/// component, description, and format.
///
/// # Parameters
/// - `session_id`: A unique identifier for the log session.
/// - `time`: The timestamp of the log entry.
/// - `level`: The severity level of the log.
/// - `component`: The system component that generated the log.
/// - `description`: A textual description of the log event.
/// - `format`: The format in which the log will be recorded.
///
/// # Example
/// ```
/// use rlg::{macro_log, log_level::LogLevel, log_format::LogFormat};
/// let log = macro_log!("id", "2022-01-01", &LogLevel::INFO, "app", "message", &LogFormat::JSON);
/// ```
/// Usage:
/// let log = macro_log!(session_id, time, level, component, description, format);
#[macro_export]
#[doc = "Macro to create a new log easily"]
macro_rules! macro_log {
($session_id:expr, $time:expr, $level:expr, $component:expr, $description:expr, $format:expr) => {
$crate::log::Log::new(
$session_id,
$time,
$level,
$component,
$description,
$format,
)
};
}
/// This macro creates an `INFO` level log entry with a default session ID and format.
/// The session ID is generated randomly and the log format defaults to CLF.
///
/// # Parameters
/// - `time`: The timestamp of the log entry.
/// - `component`: The system component that generated the log.
/// - `description`: A textual description of the log event.
///
/// # Example
/// ```
/// use rlg::macro_info_log;
/// let log = macro_info_log!("2024-08-29T12:00:00Z", "Auth", "User login");
/// ```
/// Usage:
/// let log = macro_info_log!(time, component, description);
#[macro_export]
#[doc = "Macro for info log with default session id and format"]
macro_rules! macro_info_log {
($time:expr, $component:expr, $description:expr) => {
$crate::log::Log::new(
&vrd::random::Random::default()
.int(0, 1_000_000_000)
.to_string(),
$time,
&$crate::log_level::LogLevel::INFO,
$component,
$description,
&$crate::log_format::LogFormat::CLF,
)
};
}
/// This macro asynchronously logs a message to a file.
/// It returns the result of the logging operation, which could be
/// used to check the success or failure of the logging action.
///
/// # Parameters
/// - `log`: The log entry to be saved to a file.
///
/// # Example
/// ```
/// use rlg::{macro_log,macro_log_to_file,macro_info_log};
/// use rlg::log_format::LogFormat;
/// use rlg::log_level::LogLevel;
/// let log = macro_info_log!("2022-01-01", "app", "message");
/// async {
/// let result = macro_log_to_file!(log);
/// };
/// ```
/// Usage:
/// let result = macro_log_to_file!(log);
#[macro_export]
#[doc = "Async log message to file"]
macro_rules! macro_log_to_file {
($log:expr) => {{
let result = $log.log().await;
result
}};
}
/// This macro creates a `WARN` level log entry with a default session ID and format.
/// The session ID is generated randomly and the log format defaults to CLF.
///
/// # Parameters
/// - `time`: The timestamp of the log entry.
/// - `component`: The system component that generated the log.
/// - `description`: A textual description of the log event.
///
/// # Example
/// ```
/// use rlg::{macro_warn_log, macro_log};
/// use rlg::log_level::LogLevel;
/// use rlg::log_format::LogFormat;
/// let log = macro_warn_log!("2024-08-29T12:00:00Z", "Auth", "Invalid password attempt");
/// ```
/// Usage:
/// let log = macro_warn_log!(time, component, description);
#[macro_export]
#[doc = "Macro for warn log with default session id and format"]
macro_rules! macro_warn_log {
($time:expr, $component:expr, $description:expr) => {
$crate::macro_log!(
&vrd::random::Random::default()
.int(0, 1_000_000_000)
.to_string(),
$time,
&$crate::log_level::LogLevel::WARN,
$component,
$description,
&$crate::log_format::LogFormat::CLF
)
};
}
/// This macro creates an `ERROR` level log entry with a default session ID and format.
/// The session ID is generated randomly and the log format defaults to CLF.
///
/// # Parameters
/// - `time`: The timestamp of the log entry.
/// - `component`: The system component that generated the log.
/// - `description`: A textual description of the log event.
///
/// # Example
/// ```
/// use rlg::{macro_error_log, macro_log};
/// use rlg::log_level::LogLevel;
/// use rlg::log_format::LogFormat;
/// let log = macro_error_log!("2024-08-29T12:00:00Z", "Database", "Connection failed");
/// ```
/// Usage:
/// let log = macro_error_log!(time, component, description);
#[macro_export]
#[doc = "Macro for error log with default session id and format"]
macro_rules! macro_error_log {
($time:expr, $component:expr, $description:expr) => {
$crate::macro_log!(
&vrd::random::Random::default()
.int(0, 1_000_000_000)
.to_string(),
$time,
&$crate::log_level::LogLevel::ERROR,
$component,
$description,
&$crate::log_format::LogFormat::CLF
)
};
}
/// This macro creates a `TRACE` level log entry with a default session ID and format.
/// The session ID is generated randomly and the log format defaults to CLF.
///
/// # Parameters
/// - `time`: The timestamp of the log entry.
/// - `component`: The system component that generated the log.
/// - `description`: A textual description of the log event.
///
/// # Example
/// ```
/// use rlg::{macro_trace_log, macro_log};
/// use rlg::log_level::LogLevel;
/// use rlg::log_format::LogFormat;
/// let log = macro_trace_log!("2024-08-29T12:00:00Z", "Auth", "Tracing user activity");
/// ```
/// Usage:
/// let log = macro_trace_log!(time, component, description);
#[macro_export]
#[doc = "Macro for trace log with default session id and format"]
macro_rules! macro_trace_log {
($time:expr, $component:expr, $description:expr) => {
$crate::macro_log!(
&vrd::random::Random::default()
.int(0, 1_000_000_000)
.to_string(),
$time,
&$crate::log_level::LogLevel::TRACE,
$component,
$description,
&$crate::log_format::LogFormat::CLF
)
};
}
/// This macro creates a `FATAL` level log entry with a default session ID and format.
/// The session ID is generated randomly and the log format defaults to CLF.
///
/// # Parameters
/// - `time`: The timestamp of the log entry.
/// - `component`: The system component that generated the log.
/// - `description`: A textual description of the log event.
///
/// # Example
/// ```
/// use rlg::log_level::LogLevel;
/// use rlg::log_format::LogFormat;
/// use rlg::{macro_fatal_log, macro_log};
/// let log = macro_fatal_log!("2024-08-29T12:00:00Z", "System", "Critical failure");
/// ```
/// Usage:
/// let log = macro_fatal_log!(time, component, description);
#[macro_export]
#[doc = "Macro for fatal log with default session id and format"]
macro_rules! macro_fatal_log {
($time:expr, $component:expr, $description:expr) => {
$crate::macro_log!(
&vrd::random::Random::default()
.int(0, 1_000_000_000)
.to_string(),
$time,
&$crate::log_level::LogLevel::FATAL,
$component,
$description,
&$crate::log_format::LogFormat::CLF
)
};
}
// ========================
// Macros for Log Formatting
// ========================
/// This macro sets the log format to CLF if it is not already defined.
///
/// # Parameters
/// - `log`: The log entry whose format is to be set.
///
/// # Example
/// ```
/// use rlg::macro_set_log_format_clf;
/// use rlg::macro_info_log;
/// let mut log = macro_info_log!("2022-01-01", "app", "message");
/// macro_set_log_format_clf!(log);
/// ```
/// Usage:
/// macro_set_log_format_clf!(log);
#[macro_export]
#[doc = "Set log format to CLF if not already defined"]
macro_rules! macro_set_log_format_clf {
($log:expr) => {
if $log.format != $crate::log_format::LogFormat::CLF {
$log.format = $crate::log_format::LogFormat::CLF;
}
};
}
/// This macro logs with metadata.
/// It replaces specific keys in the log message with consistent ones.
///
/// # Parameters
/// - `session_id`: A unique identifier for the log session.
/// - `time`: The timestamp of the log entry.
/// - `level`: The severity level of the log.
/// - `component`: The system component that generated the log.
/// - `description`: A textual description of the log event.
/// - `format`: The format in which the log will be recorded.
///
/// # Example
/// ```
/// use rlg::{macro_log_with_metadata, log_level::LogLevel, log_format::LogFormat};
/// let log = macro_log_with_metadata!("id", "2022-01-01", &LogLevel::INFO, "app", "message", &LogFormat::JSON);
/// println!("{log} | Metadata: <metadata>");
/// ```
/// Usage:
/// let log = macro_log_with_metadata!(session_id, time, level, component, description, format);
#[macro_export]
#[doc = "Macro for logging with metadata"]
macro_rules! macro_log_with_metadata {
($session_id:expr, $time:expr, $level:expr, $component:expr, $description:expr, $format:expr) => {{
let log = $crate::log::Log::new(
$session_id,
$time,
$level,
$component,
$description,
$format,
);
// Replace keys in the log message with consistent ones
let log_message = log
.to_string()
.replace("\"component\"", "\"component\"")
.replace("\"session_id\"", "\"session_id\"");
log_message
}};
}
// =========================
// Macros for Log Conditions
// =========================
/// This macro conditionally logs a message based on a predicate.
///
/// # Parameters
/// - `predicate`: A boolean expression that determines whether to log.
/// - `log`: The log entry to be conditionally logged.
///
/// # Example
/// ```
/// use rlg::{macro_log_if, macro_print_log};
/// use rlg::macro_info_log;
/// let log = macro_info_log!("2022-01-01", "app", "message");
/// macro_log_if!(true, log);
/// ```
/// Usage:
/// macro_log_if!(predicate, log);
#[macro_export]
#[doc = "Conditional logging based on a predicate"]
macro_rules! macro_log_if {
($predicate:expr, $log:expr) => {
if $predicate {
macro_print_log!($log);
}
};
}
/// This macro conditionally logs a debug message if the `debug_enabled` feature flag is set.
///
/// # Parameters
/// - `log`: The log entry to be conditionally logged.
///
/// # Example
/// ```
/// use rlg::macro_info_log;
/// use rlg::macro_debug_log;
/// use rlg::macro_print_log;
/// let log = macro_info_log!("2022-01-01", "app", "message");
/// macro_debug_log!(log);
/// ```
/// Usage:
/// macro_debug_log!(log);
#[cfg(feature = "debug_enabled")]
#[macro_export]
#[doc = "Conditional debug logging based on feature flag"]
macro_rules! macro_debug_log {
($log:expr) => {
macro_print_log!($log);
};
}
#[cfg(not(feature = "debug_enabled"))]
#[macro_export]
#[doc = "Conditional debug logging does nothing if feature flag is not set"]
macro_rules! macro_debug_log {
($log:expr) => {
// Do nothing if `debug_enabled` feature flag is not set
};
}
// =======================
// Macros for Log Output
// =======================
/// This macro prints a log entry to the standard output (stdout).
/// It is useful for debugging or simple logging to the console.
///
/// # Parameters
/// - `log`: The log entry to be printed.
///
/// # Example
/// ```
/// use rlg::{macro_print_log,macro_info_log};
/// let log = macro_info_log!("2022-01-01", "app", "message");
/// macro_print_log!(log);
/// ```
/// Usage:
/// macro_print_log!(log);
#[macro_export]
#[doc = "Print log to stdout"]
macro_rules! macro_print_log {
($log:expr) => {
println!("{}", $log.description);
};
}