update log dep and make level adjustable

master
rascul 5 years ago
parent d50763aaee
commit b99a58ff57

@ -15,4 +15,4 @@ travis-ci = { repository = "/rascul/rag" }
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
colored = "1.6" colored = "1.6"
log = "0.3" log = "0.4"

@ -5,7 +5,7 @@ extern crate colored;
extern crate log; extern crate log;
use colored::*; use colored::*;
use log::{set_logger, Log, LogLevel, LogLevelFilter, LogMetadata, LogRecord}; use log::{set_logger, set_max_level, Log, Level, LevelFilter, Metadata, Record};
pub mod error; pub mod error;
#[cfg(test)] #[cfg(test)]
@ -14,36 +14,38 @@ mod tests;
pub use error::RagError; pub use error::RagError;
pub use error::RagResult; pub use error::RagResult;
static LOGGER: Logger = Logger;
struct Logger; struct Logger;
impl Log for Logger { impl Log for Logger {
fn enabled(&self, metadata: &LogMetadata) -> bool { fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= LogLevel::Info metadata.level() <= Level::Trace
} }
fn log(&self, record: &LogRecord) { fn log(&self, record: &Record) {
if self.enabled(record.metadata()) { if self.enabled(record.metadata()) {
let now = chrono::Local::now(); let now = chrono::Local::now();
let ts = now.format("%Y-%m-%dT%H:%M:%S%.3f%z").to_string(); let ts = now.format("%Y-%m-%dT%H:%M:%S%.3f%z").to_string();
let (msg, _level) = match record.level() { let (msg, _level) = match record.level() {
LogLevel::Error => ( Level::Error => (
format!("{} {} {}", ts.white().bold(), "ERR".red(), record.args()), format!("{} {} {}", ts.white().bold(), "ERR".red(), record.args()),
3, 3,
), ),
LogLevel::Warn => ( Level::Warn => (
format!("{} {} {}", ts.white().bold(), "WRN".purple(), record.args()), format!("{} {} {}", ts.white().bold(), "WRN".purple(), record.args()),
4, 4,
), ),
LogLevel::Info => ( Level::Info => (
format!("{} {} {}", ts.white().bold(), "INF".cyan(), record.args()), format!("{} {} {}", ts.white().bold(), "INF".cyan(), record.args()),
6, 6,
), ),
LogLevel::Debug => ( Level::Debug => (
format!("{} {} {}", ts.white().bold(), "DBG".yellow(), record.args()), format!("{} {} {}", ts.white().bold(), "DBG".yellow(), record.args()),
7, 7,
), ),
LogLevel::Trace => ( Level::Trace => (
format!("{} {} {}", ts.white().bold(), "TRC".green(), record.args()), format!("{} {} {}", ts.white().bold(), "TRC".green(), record.args()),
0, 0,
), ),
@ -51,11 +53,38 @@ impl Log for Logger {
println!("{}", msg); println!("{}", msg);
} }
} }
fn flush(&self) {}
} }
pub fn init() -> RagResult<()> { pub fn init() -> RagResult<()> {
Ok(set_logger(|max_log_level| { Ok(set_logger(&LOGGER).map(|()| set_max_level(LevelFilter::Info))?)
max_log_level.set(LogLevelFilter::Info); }
Box::new(Logger)
})?) // Ok(set_logger(|max_log_level| {
// max_log_level.set(LogLevelFilter::Info);
// Box::new(Logger)
// })?)
//}
pub fn set_level_error() {
set_max_level(LevelFilter::Error);
}
pub fn set_level_warn() {
set_max_level(LevelFilter::Warn);
}
pub fn set_level_info() {
set_max_level(LevelFilter::Info);
}
pub fn set_level_debug() {
set_max_level(LevelFilter::Debug);
}
pub fn set_level_trace() {
set_max_level(LevelFilter::Trace);
} }

Loading…
Cancel
Save