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]
chrono = "0.4"
colored = "1.6"
log = "0.3"
log = "0.4"

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