Skip to content

Commit 50ac89f

Browse files
committed
Added timestamps
1 parent ec55232 commit 50ac89f

File tree

3 files changed

+53
-67
lines changed

3 files changed

+53
-67
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "logging-rs"
33
description = "logging-rs helps you add logging to your projects using simple macros."
4-
version = "1.0.0"
4+
version = "1.1.0"
55
authors = [
66
"ElBe-Plaq <elbe.dev.plaq@gmail.com>"
77
]
@@ -18,4 +18,4 @@ categories = [
1818
publish = true
1919

2020
[dependencies]
21-
# chrono = "0.4.31"
21+
chrono = "0.4.31"

src/lib.rs

+49-64
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub mod errors;
3636
use std;
3737
use std::io::Write;
3838

39-
// use chrono;
39+
use chrono;
4040

4141

4242
////////////////
@@ -208,7 +208,7 @@ impl Formatter {
208208
/// logging_rs::Output::default(),
209209
/// logging_rs::Level::default(),
210210
/// "Some message with an {{argument}}",
211-
/// vec![("argument", "replaced value")]
211+
/// vec![("argument", "replaced value".to_string())]
212212
/// );
213213
/// ```
214214
///
@@ -217,90 +217,75 @@ impl Formatter {
217217
/// - [`Formatter`]
218218
/// - [`Output`]
219219
/// - [`Level`]
220-
pub fn format<'a>(&self, output: Output, level: Level, message: &'a str, mut arguments: Vec<(&str, &'a str)>) -> String {
221-
// TODO (ElBe): Make timestamps work. None of chrono, time or humantime have the things I want
222-
let timestamp: &str = "TIMESTAMP";
223-
224-
// let time = chrono::Utc::now();
225-
//let _timestamp = time.format("%Y-%m-%d %H:%M:%S").to_string(); //chrono::format::DelayedFormat<chrono::format::StrftimeItems<'_>>
226-
//let parsed = chrono::NaiveDateTime::parse_from_str(&_timestamp.to_string(), "%Y-%m-%d %H:%M:%S").expect("Bad");
227-
228-
//let borrowed = &_timestamp;
229-
230-
//println!("{}", _timestamp);
231-
//println!("{}", parsed.to_string().as_str());
232-
233-
// arguments.push(("timestamp", &time.format("%Y-%m-%d %H:%M:%S").to_string()));
234-
235-
let mut colors: Vec<(&str, &str)> = vec![
220+
pub fn format<'a>(&self, output: Output, level: Level, message: &'a str, mut arguments: Vec<(&str, String)>) -> String {
221+
let mut colors: Vec<(&str, String)> = vec![
236222
// Formatting codes
237-
("end", "\x1b[0m"),
238-
("bold", "\x1b[1m"),
239-
("italic", "\x1b[3m"),
240-
("underline", "\x1b[4m"),
241-
("overline", "\x1b[53m"),
223+
("end", "\x1b[0m".to_string()),
224+
("bold", "\x1b[1m".to_string()),
225+
("italic", "\x1b[3m".to_string()),
226+
("underline", "\x1b[4m".to_string()),
227+
("overline", "\x1b[53m".to_string()),
242228

243229
// Foreground colors
244-
("color.black", "\x1b[30m"),
245-
("color.red", "\x1b[31m"),
246-
("color.green", "\x1b[32m"),
247-
("color.yellow", "\x1b[33m"),
248-
("color.blue", "\x1b[34m"),
249-
("color.magenta", "\x1b[35m"),
250-
("color.cyan", "\x1b[36m"),
251-
("color.white", "\x1b[37m"),
230+
("color.black", "\x1b[30m".to_string()),
231+
("color.red", "\x1b[31m".to_string()),
232+
("color.green", "\x1b[32m".to_string()),
233+
("color.yellow", "\x1b[33m".to_string()),
234+
("color.blue", "\x1b[34m".to_string()),
235+
("color.magenta", "\x1b[35m".to_string()),
236+
("color.cyan", "\x1b[36m".to_string()),
237+
("color.white", "\x1b[37m".to_string()),
252238

253239
// Bright foreground colors
254-
("color.bright_black", "\x1b[90m"),
255-
("color.bright_red", "\x1b[91m"),
256-
("color.bright_green", "\x1b[92m"),
257-
("color.bright_yellow", "\x1b[93m"),
258-
("color.bright_blue", "\x1b[94m"),
259-
("color.bright_magenta", "\x1b[95m"),
260-
("color.bright_cyan", "\x1b[96m"),
261-
("color.bright_white", "\x1b[97m"),
240+
("color.bright_black", "\x1b[90m".to_string()),
241+
("color.bright_red", "\x1b[91m".to_string()),
242+
("color.bright_green", "\x1b[92m".to_string()),
243+
("color.bright_yellow", "\x1b[93m".to_string()),
244+
("color.bright_blue", "\x1b[94m".to_string()),
245+
("color.bright_magenta", "\x1b[95m".to_string()),
246+
("color.bright_cyan", "\x1b[96m".to_string()),
247+
("color.bright_white", "\x1b[97m".to_string()),
262248

263249
// Background colors
264-
("back.black", "\x1b[40m"),
265-
("back.red", "\x1b[41m"),
266-
("back.green", "\x1b[42m"),
267-
("back.yellow", "\x1b[43m"),
268-
("back.blue", "\x1b[44m"),
269-
("back.magenta", "\x1b[45m"),
270-
("back.cyan", "\x1b[46m"),
271-
("back.white", "\x1b[47m"),
250+
("back.black", "\x1b[40m".to_string()),
251+
("back.red", "\x1b[41m".to_string()),
252+
("back.green", "\x1b[42m".to_string()),
253+
("back.yellow", "\x1b[43m".to_string()),
254+
("back.blue", "\x1b[44m".to_string()),
255+
("back.magenta", "\x1b[45m".to_string()),
256+
("back.cyan", "\x1b[46m".to_string()),
257+
("back.white", "\x1b[47m".to_string()),
272258

273259
// Bright background colors
274-
("back.bright_black", "\x1b[100m"),
275-
("back.bright_red", "\x1b[101m"),
276-
("back.bright_green", "\x1b[102m"),
277-
("back.bright_yellow", "\x1b[103m"),
278-
("back.bright_blue", "\x1b[104m"),
279-
("back.bright_magenta", "\x1b[105m"),
280-
("back.bright_cyan", "\x1b[106m"),
281-
("back.bright_white", "\x1b[107m"),
260+
("back.bright_black", "\x1b[100m".to_string()),
261+
("back.bright_red", "\x1b[101m".to_string()),
262+
("back.bright_green", "\x1b[102m".to_string()),
263+
("back.bright_yellow", "\x1b[103m".to_string()),
264+
("back.bright_blue", "\x1b[104m".to_string()),
265+
("back.bright_magenta", "\x1b[105m".to_string()),
266+
("back.bright_cyan", "\x1b[106m".to_string()),
267+
("back.bright_white", "\x1b[107m".to_string()),
282268
];
283269

284-
let level_string: (&str, &str) = ("level", match level {
270+
let level_string: (&str, String) = ("level", match level {
285271
Level::DEBUG => "DEBUG",
286272
Level::INFO => "INFO",
287273
Level::WARN => "WARNING",
288274
Level::ERROR => "ERROR",
289275
Level::FATAL => "FATAL",
290276
Level::MESSAGE => "MESSAGE"
291-
});
292-
let colored_level_string: (&str, &str) = ("level", match level {
277+
}.to_string());
278+
let colored_level_string: (&str, String) = ("level", match level {
293279
Level::DEBUG => "DEBUG",
294280
Level::INFO => "{{color.blue}}INFO{{end}}",
295281
Level::WARN => "{{color.yellow}}WARNING{{end}}",
296282
Level::ERROR => "{{color.red}}ERROR{{end}}",
297283
Level::FATAL => "{{color.red}}FATAL{{end}}",
298284
Level::MESSAGE => "{{color.blue}}MESSAGE{{end}}"
299-
});
285+
}.to_string());
300286

301-
arguments.push(("message", message));
302-
arguments.push(("timestamp", timestamp));
303-
//arguments.push(("timestamp", chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string().to_owned().as_str()));
287+
arguments.push(("message", message.to_string()));
288+
arguments.push(("timestamp", chrono::Local::now().format(&self.timestamp_format).to_string()));
304289

305290
let mut result: String = match output {
306291
Output::STDOUT | Output::STDERR => {
@@ -316,7 +301,7 @@ impl Formatter {
316301
arguments.append(&mut colors);
317302

318303
for (key, value) in arguments {
319-
result = result.replace(("{{".to_owned() + key + "}}").as_str(), value);
304+
result = result.replace(("{{".to_owned() + key + "}}").as_str(), &value);
320305
}
321306

322307
return result.clone();
@@ -428,7 +413,7 @@ impl Logger {
428413
/// - [`Level`]
429414
pub fn log(&self, message: &str, level: Level, path: &str) {
430415
for writable in self.writable_list.clone() {
431-
let formatted: String = self.formatter.format(writable.clone(), level, message, vec![("path", path)]);
416+
let formatted: String = self.formatter.format(writable.clone(), level, message, vec![("path", path.to_string())]);
432417

433418
match writable {
434419
Output::STDOUT => println!("{}", formatted),

tests/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
////////////////////////////////
2727

2828
#[allow(unused_imports)]
29+
use chrono;
2930
use logging_rs;
3031

3132

@@ -85,7 +86,7 @@ mod tests {
8586

8687
assert_eq!(
8788
formatter.format(logging_rs::Output::default(), logging_rs::Level::default(), "Test", vec![]),
88-
"[\x1b[94mTIMESTAMP\x1b[0m] [DEBUG] {{path}}: Test"
89+
format!("[\x1b[94m{}\x1b[0m] [DEBUG] {{{{path}}}}: Test", chrono::Local::now().format(&logging_rs::Formatter::default().timestamp_format))
8990
);
9091
}
9192

0 commit comments

Comments
 (0)