-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_messages.rs
134 lines (125 loc) · 3.03 KB
/
log_messages.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
use log::debug;
use log::{Level, Log, Metadata, Record};
/// Log a debug message to the console
///
/// # Arguments
///
/// * `message` - A string slice
///
/// # Examples
///
/// ```
/// use development_tools::log_debug_message;
///
/// log_debug_message("This is a debug message");
/// ```
pub fn log_debug_message(message: &str) {
// RUST_LOG=debug cargo run
debug!("DEBUG: {}", message);
}
/// Log an error message to the console
///
/// # Arguments
///
/// * `message` - A string slice
///
/// # Returns
///
/// * A Result object
///
/// # Examples
///
/// ```
/// use development_tools::log_error_message;
///
/// env_logger::init();
///
/// let result = log_error_message("This is an error message");
/// if let Err(err) = result {
/// eprintln!("Failed to execute query: {}", err);
/// }
/// ```
pub fn log_error_message(message: &str) -> Result<(), String> {
Err(format!("ERROR: {}", message))
}
/// A custom logger that logs messages to the console
///
/// # Examples
///
/// ```
/// use development_tools::ConsoleLogger;
/// use log::{set_logger, set_max_level, LevelFilter, info, error, warn};
///
/// static CONSOLE_LOGGER: ConsoleLogger = ConsoleLogger;
///
/// if set_logger(&CONSOLE_LOGGER).is_ok() {
/// set_max_level(LevelFilter::Info);
/// }
///
/// info!("This is an info message");
/// warn!("This is a warning message");
/// error!("This is an error message");
/// ```
pub struct ConsoleLogger;
impl Log for ConsoleLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Info
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
println!("Rust says: {} - {}", record.level(), record.args());
}
}
fn flush(&self) {}
}
/// A custom logger that logs messages to multiple loggers
///
/// # Examples
///
/// ```
/// use development_tools::{ConsoleLogger, MultiLogger};
/// use log::{set_boxed_logger, set_max_level, LevelFilter, info, error, warn};
///
/// let mut logger = MultiLogger::new();
///
/// let logger1 = ConsoleLogger;
/// logger.add_logger(Box::new(logger1));
///
/// if set_boxed_logger(Box::new(logger)).is_ok() {
/// set_max_level(LevelFilter::Info);
/// }
///
/// info!("This is an info message");
/// warn!("This is a warning message");
/// error!("This is an error message");
/// ```
pub struct MultiLogger {
loggers: Vec<Box<dyn Log + Sync + Send>>,
}
impl MultiLogger {
pub fn new() -> Self {
Self {
loggers: Vec::new(),
}
}
pub fn add_logger(&mut self, logger: Box<dyn Log + Sync + Send>) {
self.loggers.push(logger);
}
}
impl Log for MultiLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
self.loggers.iter().any(|logger| logger.enabled(metadata))
}
fn log(&self, record: &Record) {
for logger in &self.loggers {
if logger.enabled(record.metadata()) {
logger.log(record);
}
}
}
fn flush(&self) {
for logger in &self.loggers {
logger.flush();
}
}
}