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
use colored::Colorize;
use chrono::prelude::Local;
pub enum LogLevel {
All,
Info,
Warning,
Error,
Fatal,
}
pub enum LogMessage<'a> {
Message(&'a str),
InfoMessage(&'a str),
WarningMessage(&'a str),
ErrorMessage(&'a str),
FatalMessage(&'a str),
}
pub struct Logger<'a> {
pub log_name: &'a str,
pub log_level: LogLevel,
}
impl<'a> Logger<'a> {
pub fn new(log_name: &'a str, log_level: LogLevel) -> Logger {
Logger {
log_name,
log_level: log_level,
}
}
pub fn set_level(&mut self, log_level: LogLevel) {
self.log_level = log_level;
}
pub fn log(&self, message: LogMessage) {
let t_timestamp = format!("{}", Local::now());
let mut t_timestamp = t_timestamp.split(" ");
let datetime = t_timestamp.next().unwrap();
let time = t_timestamp.next().unwrap();
let mut time_split = time.split(".");
let timestamp = format!("{} {}.{:.4}", datetime, time_split.next().unwrap(), time_split.next().unwrap());
let result = match self.log_level {
LogLevel::All => {
match message {
LogMessage::Message(text) => {
(String::from(format!("\n({})[{}] Any: {}\n", self.log_name, timestamp, text))).blue()
},
LogMessage::InfoMessage(text) => {
(String::from(format!("\n({})[{}] Info: {}\n", self.log_name, timestamp, text))).green()
},
LogMessage::WarningMessage(text) => {
(String::from(format!("\n({})[{}] Warning: {}\n", self.log_name, timestamp, text))).yellow()
},
LogMessage::ErrorMessage(text) => {
(String::from(format!("\n({})[{}] Error: {}\n", self.log_name, timestamp, text))).red()
},
LogMessage::FatalMessage(text) => {
(String::from(format!("\n({})[{}] Fatal: {}\n", self.log_name, timestamp, text))).black().on_red()
}
}
},
LogLevel::Info => {
match message {
LogMessage::InfoMessage(text) => {
(String::from(format!("\n({})[{}] Info: {}\n", self.log_name, timestamp, text))).green()
},
LogMessage::WarningMessage(text) => {
(String::from(format!("\n({})[{}] Warning: {}\n", self.log_name, timestamp, text))).yellow()
},
LogMessage::ErrorMessage(text) => {
(String::from(format!("\n({})[{}] Error: {}\n", self.log_name, timestamp, text))).red()
},
LogMessage::FatalMessage(text) => {
(String::from(format!("\n({})[{}] Fatal: {}\n", self.log_name, timestamp, text))).black().on_red()
}
_ => "".normal(),
}
},
LogLevel::Warning => {
match message {
LogMessage::WarningMessage(text) => {
(String::from(format!("\n({})[{}] Warning: {}\n", self.log_name, timestamp, text))).yellow()
},
LogMessage::ErrorMessage(text) => {
(String::from(format!("\n({})[{}] Error: {}\n", self.log_name, timestamp, text))).red()
},
LogMessage::FatalMessage(text) => {
(String::from(format!("\n({})[{}] Fatal: {}\n", self.log_name, timestamp, text))).black().on_red()
},
_ => "".normal(),
}
},
LogLevel::Error => {
match message {
LogMessage::ErrorMessage(text) => {
(String::from(format!("\n({})[{}] Error: {}\n", self.log_name, timestamp, text))).red()
},
LogMessage::FatalMessage(text) => {
(String::from(format!("\n({})[{}] Fatal: {}\n", self.log_name, timestamp, text))).black().on_red()
},
_ => "".normal(),
}
},
LogLevel::Fatal => {
match message {
LogMessage::FatalMessage(text) => {
(String::from(format!("\n({})[{}] Fatal: {}\n", self.log_name, timestamp, text))).black().on_red()
},
_ => "".normal(),
}
}
};
print!("{}", result);
}
}