Added timestamps
This commit is contained in:
parent
ec55232c51
commit
50ac89f3b0
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "logging-rs"
|
name = "logging-rs"
|
||||||
description = "logging-rs helps you add logging to your projects using simple macros."
|
description = "logging-rs helps you add logging to your projects using simple macros."
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
authors = [
|
authors = [
|
||||||
"ElBe-Plaq <elbe.dev.plaq@gmail.com>"
|
"ElBe-Plaq <elbe.dev.plaq@gmail.com>"
|
||||||
]
|
]
|
||||||
@ -18,4 +18,4 @@ categories = [
|
|||||||
publish = true
|
publish = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# chrono = "0.4.31"
|
chrono = "0.4.31"
|
||||||
|
|||||||
113
src/lib.rs
113
src/lib.rs
@ -36,7 +36,7 @@ pub mod errors;
|
|||||||
use std;
|
use std;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
// use chrono;
|
use chrono;
|
||||||
|
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
@ -208,7 +208,7 @@ impl Formatter {
|
|||||||
/// logging_rs::Output::default(),
|
/// logging_rs::Output::default(),
|
||||||
/// logging_rs::Level::default(),
|
/// logging_rs::Level::default(),
|
||||||
/// "Some message with an {{argument}}",
|
/// "Some message with an {{argument}}",
|
||||||
/// vec![("argument", "replaced value")]
|
/// vec![("argument", "replaced value".to_string())]
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@ -217,90 +217,75 @@ impl Formatter {
|
|||||||
/// - [`Formatter`]
|
/// - [`Formatter`]
|
||||||
/// - [`Output`]
|
/// - [`Output`]
|
||||||
/// - [`Level`]
|
/// - [`Level`]
|
||||||
pub fn format<'a>(&self, output: Output, level: Level, message: &'a str, mut arguments: Vec<(&str, &'a str)>) -> String {
|
pub fn format<'a>(&self, output: Output, level: Level, message: &'a str, mut arguments: Vec<(&str, String)>) -> String {
|
||||||
// TODO (ElBe): Make timestamps work. None of chrono, time or humantime have the things I want
|
let mut colors: Vec<(&str, String)> = vec![
|
||||||
let timestamp: &str = "TIMESTAMP";
|
|
||||||
|
|
||||||
// let time = chrono::Utc::now();
|
|
||||||
//let _timestamp = time.format("%Y-%m-%d %H:%M:%S").to_string(); //chrono::format::DelayedFormat<chrono::format::StrftimeItems<'_>>
|
|
||||||
//let parsed = chrono::NaiveDateTime::parse_from_str(&_timestamp.to_string(), "%Y-%m-%d %H:%M:%S").expect("Bad");
|
|
||||||
|
|
||||||
//let borrowed = &_timestamp;
|
|
||||||
|
|
||||||
//println!("{}", _timestamp);
|
|
||||||
//println!("{}", parsed.to_string().as_str());
|
|
||||||
|
|
||||||
// arguments.push(("timestamp", &time.format("%Y-%m-%d %H:%M:%S").to_string()));
|
|
||||||
|
|
||||||
let mut colors: Vec<(&str, &str)> = vec![
|
|
||||||
// Formatting codes
|
// Formatting codes
|
||||||
("end", "\x1b[0m"),
|
("end", "\x1b[0m".to_string()),
|
||||||
("bold", "\x1b[1m"),
|
("bold", "\x1b[1m".to_string()),
|
||||||
("italic", "\x1b[3m"),
|
("italic", "\x1b[3m".to_string()),
|
||||||
("underline", "\x1b[4m"),
|
("underline", "\x1b[4m".to_string()),
|
||||||
("overline", "\x1b[53m"),
|
("overline", "\x1b[53m".to_string()),
|
||||||
|
|
||||||
// Foreground colors
|
// Foreground colors
|
||||||
("color.black", "\x1b[30m"),
|
("color.black", "\x1b[30m".to_string()),
|
||||||
("color.red", "\x1b[31m"),
|
("color.red", "\x1b[31m".to_string()),
|
||||||
("color.green", "\x1b[32m"),
|
("color.green", "\x1b[32m".to_string()),
|
||||||
("color.yellow", "\x1b[33m"),
|
("color.yellow", "\x1b[33m".to_string()),
|
||||||
("color.blue", "\x1b[34m"),
|
("color.blue", "\x1b[34m".to_string()),
|
||||||
("color.magenta", "\x1b[35m"),
|
("color.magenta", "\x1b[35m".to_string()),
|
||||||
("color.cyan", "\x1b[36m"),
|
("color.cyan", "\x1b[36m".to_string()),
|
||||||
("color.white", "\x1b[37m"),
|
("color.white", "\x1b[37m".to_string()),
|
||||||
|
|
||||||
// Bright foreground colors
|
// Bright foreground colors
|
||||||
("color.bright_black", "\x1b[90m"),
|
("color.bright_black", "\x1b[90m".to_string()),
|
||||||
("color.bright_red", "\x1b[91m"),
|
("color.bright_red", "\x1b[91m".to_string()),
|
||||||
("color.bright_green", "\x1b[92m"),
|
("color.bright_green", "\x1b[92m".to_string()),
|
||||||
("color.bright_yellow", "\x1b[93m"),
|
("color.bright_yellow", "\x1b[93m".to_string()),
|
||||||
("color.bright_blue", "\x1b[94m"),
|
("color.bright_blue", "\x1b[94m".to_string()),
|
||||||
("color.bright_magenta", "\x1b[95m"),
|
("color.bright_magenta", "\x1b[95m".to_string()),
|
||||||
("color.bright_cyan", "\x1b[96m"),
|
("color.bright_cyan", "\x1b[96m".to_string()),
|
||||||
("color.bright_white", "\x1b[97m"),
|
("color.bright_white", "\x1b[97m".to_string()),
|
||||||
|
|
||||||
// Background colors
|
// Background colors
|
||||||
("back.black", "\x1b[40m"),
|
("back.black", "\x1b[40m".to_string()),
|
||||||
("back.red", "\x1b[41m"),
|
("back.red", "\x1b[41m".to_string()),
|
||||||
("back.green", "\x1b[42m"),
|
("back.green", "\x1b[42m".to_string()),
|
||||||
("back.yellow", "\x1b[43m"),
|
("back.yellow", "\x1b[43m".to_string()),
|
||||||
("back.blue", "\x1b[44m"),
|
("back.blue", "\x1b[44m".to_string()),
|
||||||
("back.magenta", "\x1b[45m"),
|
("back.magenta", "\x1b[45m".to_string()),
|
||||||
("back.cyan", "\x1b[46m"),
|
("back.cyan", "\x1b[46m".to_string()),
|
||||||
("back.white", "\x1b[47m"),
|
("back.white", "\x1b[47m".to_string()),
|
||||||
|
|
||||||
// Bright background colors
|
// Bright background colors
|
||||||
("back.bright_black", "\x1b[100m"),
|
("back.bright_black", "\x1b[100m".to_string()),
|
||||||
("back.bright_red", "\x1b[101m"),
|
("back.bright_red", "\x1b[101m".to_string()),
|
||||||
("back.bright_green", "\x1b[102m"),
|
("back.bright_green", "\x1b[102m".to_string()),
|
||||||
("back.bright_yellow", "\x1b[103m"),
|
("back.bright_yellow", "\x1b[103m".to_string()),
|
||||||
("back.bright_blue", "\x1b[104m"),
|
("back.bright_blue", "\x1b[104m".to_string()),
|
||||||
("back.bright_magenta", "\x1b[105m"),
|
("back.bright_magenta", "\x1b[105m".to_string()),
|
||||||
("back.bright_cyan", "\x1b[106m"),
|
("back.bright_cyan", "\x1b[106m".to_string()),
|
||||||
("back.bright_white", "\x1b[107m"),
|
("back.bright_white", "\x1b[107m".to_string()),
|
||||||
];
|
];
|
||||||
|
|
||||||
let level_string: (&str, &str) = ("level", match level {
|
let level_string: (&str, String) = ("level", match level {
|
||||||
Level::DEBUG => "DEBUG",
|
Level::DEBUG => "DEBUG",
|
||||||
Level::INFO => "INFO",
|
Level::INFO => "INFO",
|
||||||
Level::WARN => "WARNING",
|
Level::WARN => "WARNING",
|
||||||
Level::ERROR => "ERROR",
|
Level::ERROR => "ERROR",
|
||||||
Level::FATAL => "FATAL",
|
Level::FATAL => "FATAL",
|
||||||
Level::MESSAGE => "MESSAGE"
|
Level::MESSAGE => "MESSAGE"
|
||||||
});
|
}.to_string());
|
||||||
let colored_level_string: (&str, &str) = ("level", match level {
|
let colored_level_string: (&str, String) = ("level", match level {
|
||||||
Level::DEBUG => "DEBUG",
|
Level::DEBUG => "DEBUG",
|
||||||
Level::INFO => "{{color.blue}}INFO{{end}}",
|
Level::INFO => "{{color.blue}}INFO{{end}}",
|
||||||
Level::WARN => "{{color.yellow}}WARNING{{end}}",
|
Level::WARN => "{{color.yellow}}WARNING{{end}}",
|
||||||
Level::ERROR => "{{color.red}}ERROR{{end}}",
|
Level::ERROR => "{{color.red}}ERROR{{end}}",
|
||||||
Level::FATAL => "{{color.red}}FATAL{{end}}",
|
Level::FATAL => "{{color.red}}FATAL{{end}}",
|
||||||
Level::MESSAGE => "{{color.blue}}MESSAGE{{end}}"
|
Level::MESSAGE => "{{color.blue}}MESSAGE{{end}}"
|
||||||
});
|
}.to_string());
|
||||||
|
|
||||||
arguments.push(("message", message));
|
arguments.push(("message", message.to_string()));
|
||||||
arguments.push(("timestamp", timestamp));
|
arguments.push(("timestamp", chrono::Local::now().format(&self.timestamp_format).to_string()));
|
||||||
//arguments.push(("timestamp", chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string().to_owned().as_str()));
|
|
||||||
|
|
||||||
let mut result: String = match output {
|
let mut result: String = match output {
|
||||||
Output::STDOUT | Output::STDERR => {
|
Output::STDOUT | Output::STDERR => {
|
||||||
@ -316,7 +301,7 @@ impl Formatter {
|
|||||||
arguments.append(&mut colors);
|
arguments.append(&mut colors);
|
||||||
|
|
||||||
for (key, value) in arguments {
|
for (key, value) in arguments {
|
||||||
result = result.replace(("{{".to_owned() + key + "}}").as_str(), value);
|
result = result.replace(("{{".to_owned() + key + "}}").as_str(), &value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.clone();
|
return result.clone();
|
||||||
@ -428,7 +413,7 @@ impl Logger {
|
|||||||
/// - [`Level`]
|
/// - [`Level`]
|
||||||
pub fn log(&self, message: &str, level: Level, path: &str) {
|
pub fn log(&self, message: &str, level: Level, path: &str) {
|
||||||
for writable in self.writable_list.clone() {
|
for writable in self.writable_list.clone() {
|
||||||
let formatted: String = self.formatter.format(writable.clone(), level, message, vec![("path", path)]);
|
let formatted: String = self.formatter.format(writable.clone(), level, message, vec![("path", path.to_string())]);
|
||||||
|
|
||||||
match writable {
|
match writable {
|
||||||
Output::STDOUT => println!("{}", formatted),
|
Output::STDOUT => println!("{}", formatted),
|
||||||
|
|||||||
@ -26,6 +26,7 @@
|
|||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
|
use chrono;
|
||||||
use logging_rs;
|
use logging_rs;
|
||||||
|
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
formatter.format(logging_rs::Output::default(), logging_rs::Level::default(), "Test", vec![]),
|
formatter.format(logging_rs::Output::default(), logging_rs::Level::default(), "Test", vec![]),
|
||||||
"[\x1b[94mTIMESTAMP\x1b[0m] [DEBUG] {{path}}: Test"
|
format!("[\x1b[94m{}\x1b[0m] [DEBUG] {{{{path}}}}: Test", chrono::Local::now().format(&logging_rs::Formatter::default().timestamp_format))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user