-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.rs
More file actions
65 lines (56 loc) · 1.42 KB
/
logger.rs
File metadata and controls
65 lines (56 loc) · 1.42 KB
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
use std::fmt::Arguments;
#[cfg(test)]
use std::io::{self, Write};
#[cfg(test)]
use std::sync::{Arc, Mutex};
use tracing::{error, info, warn};
pub struct Logger {
label: String,
}
impl Logger {
pub fn new<S: Into<String>>(label: S) -> Self {
Self {
label: label.into(),
}
}
pub fn info(&self, args: Arguments) {
info!("[{}]: {}", self.label, args);
}
pub fn warn(&self, args: Arguments) {
warn!("[{}]: {}", self.label, args);
}
pub fn error(&self, args: Arguments) {
error!("[{}]: {}", self.label, args);
}
}
pub fn initial_logs(
queue_name: &str,
concurrency: usize,
redis_url: &str,
tasks_module_path: &str,
tasks: Vec<String>,
contexts: Vec<String>,
) {
info!("Queue: {}", queue_name);
info!("Concurrency: {}", concurrency);
info!("Redis: {}", redis_url);
info!("Tasks module: {}", tasks_module_path);
info!("Tasks found: {:?}", tasks);
info!("Contexts found: {:?}", contexts);
info!("Starting up the executors...");
}
#[cfg(test)]
pub struct TestWriter {
pub logs: Arc<Mutex<Vec<String>>>,
}
#[cfg(test)]
impl Write for TestWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let line = String::from_utf8_lossy(buf).to_string();
self.logs.lock().unwrap().push(line);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}