Skip to content

Commit 768f65d

Browse files
committed
Add color scheme prefix based on log levels
1 parent b552fa6 commit 768f65d

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

src/logger.rs

+70-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,66 @@
1+
/// Struct for implementing a simple logger
2+
pub struct SimpleLogger;
3+
14
use chrono::Local;
25

3-
pub struct SimpleLogger;
6+
/// ANSI encoded text styling for bold, underline and italic
7+
struct Format;
8+
#[allow(dead_code)]
9+
impl Format {
10+
const BOLD: &'static str = "\x1b[1m";
11+
const UNDERLINE: &'static str = "\x1b[4m";
12+
const ITALIC: &'static str = "\x1b[3m";
13+
}
14+
15+
/// ANSI encoded color scheme for text
16+
struct Colors;
17+
#[allow(dead_code)]
18+
impl Colors {
19+
const VIOLET: &'static str = "\x1b[95m";
20+
const BLUE: &'static str = "\x1b[94m";
21+
const CYAN: &'static str = "\x1b[96m";
22+
const GREEN: &'static str = "\x1b[92m";
23+
const YELLOW: &'static str = "\x1b[93m";
24+
const RED: &'static str = "\x1b[91m";
25+
const END: &'static str = "\x1b[0m";
26+
const LIGHT_GREEN: &'static str = "\x1b[32m";
27+
const LIGHT_YELLOW: &'static str = "\x1b[2;33m";
28+
const LIGHT_RED: &'static str = "\x1b[31m";
29+
}
30+
31+
/// Struct controller for different log levels
32+
struct Echo;
33+
#[allow(dead_code)]
34+
impl Echo {
35+
fn error(msg: &String) {
36+
println!("{}ERROR{}:{:<6}{}", Colors::RED, Colors::END, "", msg);
37+
}
38+
fn warning(msg: &String) {
39+
println!("{}WARNING{}:{:<4}{}", Colors::YELLOW, Colors::END, "", msg);
40+
}
41+
fn info(msg: &String) {
42+
println!("{}INFO{}:{:<7}{}", Colors::GREEN, Colors::END, "", msg);
43+
}
44+
fn debug(msg: &String) {
45+
println!(
46+
"{}DEBUG{}:{:<6}{}",
47+
Colors::LIGHT_GREEN,
48+
Colors::END,
49+
"",
50+
msg
51+
);
52+
}
53+
fn trace(msg: &String) {
54+
println!(
55+
"{}{}CRITICAL{}:{:<1}{}",
56+
Colors::LIGHT_GREEN,
57+
Format::BOLD,
58+
Colors::END,
59+
"",
60+
msg
61+
);
62+
}
63+
}
464

565
/// Implementation of the `Log` trait for the `SimpleLogger` struct
666
///
@@ -17,15 +77,21 @@ impl log::Log for SimpleLogger {
1777
fn log(&self, record: &log::Record) {
1878
let local_time = Local::now();
1979
let formatted_time = local_time.format("%b-%d-%Y %I:%M:%S %p");
20-
println!(
21-
"{} - {} - [{}:{}:{}] - {}",
80+
let msg = format!(
81+
"{} [{}:{}:{}] {}",
2282
formatted_time,
23-
record.level(),
2483
record.target(),
2584
record.file().unwrap_or_default(),
2685
record.line().unwrap_or_default(),
2786
record.args()
2887
);
88+
match record.level() {
89+
log::Level::Debug => Echo::debug(&msg),
90+
log::Level::Info => Echo::info(&msg),
91+
log::Level::Warn => Echo::warning(&msg),
92+
log::Level::Error => Echo::error(&msg),
93+
log::Level::Trace => Echo::trace(&msg),
94+
}
2995
}
3096

3197
fn flush(&self) {}

0 commit comments

Comments
 (0)