implement logger

master
rasul 5 years ago
parent d1f45584de
commit 98389a33ec

14
Cargo.lock generated

@ -109,16 +109,6 @@ dependencies = [
"proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rag"
version = "0.0.1"
source = "git+https://gitlab.com/rascul/rag#b99a58ff578eb84529d9007e332fb94e3b9b87fc"
dependencies = [
"chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
"colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand"
version = "0.4.6"
@ -181,8 +171,9 @@ dependencies = [
name = "sup"
version = "0.1.0"
dependencies = [
"chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
"colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rag 0.0.1 (git+https://gitlab.com/rascul/rag)",
"serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -267,7 +258,6 @@ dependencies = [
"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"
"checksum proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afdc77cc74ec70ed262262942ebb7dac3d479e9e5cfa2da1841c0806f6cdabcc"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum rag 0.0.1 (git+https://gitlab.com/rascul/rag)" = "<none>"
"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
"checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"

@ -5,8 +5,9 @@ authors = ["rascul <rascul3@gmail.com>"]
edition = "2018"
[dependencies]
chrono = "0.4"
colored = "1.8"
log = "0.4"
rag = { git = "https://gitlab.com/rascul/rag" }
serde = "1.0"
serde_derive = "1.0"
toml = "0.5"

@ -0,0 +1,93 @@
use std::boxed::Box;
use std::error::Error;
use std::fmt;
use colored::Colorize;
use log::*;
use crate::result::Result;
static LOGGER: Logger = Logger;
#[derive(Debug)]
pub enum LoggerError {
SetLogger(SetLoggerError),
}
impl From<SetLoggerError> for LoggerError {
fn from(e: SetLoggerError) -> Self {
LoggerError::SetLogger(e)
}
}
impl Error for LoggerError {
fn description(&self) -> &str {
match *self {
LoggerError::SetLogger(_) => "Logger already initialized",
}
}
fn cause(&self) -> Option<&dyn Error> {
match *self {
LoggerError::SetLogger(_) => Some(self),
}
}
}
impl fmt::Display for LoggerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use std::error::Error as FmtError;
write!(f, "{}", self.description())
}
}
struct Logger;
impl Log for Logger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Trace
}
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() {
Level::Error => (
format!("{} {} {}", ts.white().bold(), "ERR".red(), record.args()),
3,
),
Level::Warn => (
format!("{} {} {}", ts.white().bold(), "WRN".purple(), record.args()),
4,
),
Level::Info => (
format!("{} {} {}", ts.white().bold(), "INF".cyan(), record.args()),
6,
),
Level::Debug => (
format!("{} {} {}", ts.white().bold(), "DBG".yellow(), record.args()),
7,
),
Level::Trace => (
format!("{} {} {}", ts.white().bold(), "TRC".green(), record.args()),
0,
),
};
println!("{}", msg);
}
}
fn flush(&self) {}
}
pub fn init() -> Result<()> {
match set_logger(&LOGGER).map(|()| set_max_level(LevelFilter::Info)) {
Ok(_) => Ok(()),
Err(e) => Err(Box::new(LoggerError::from(e))),
}
}
pub fn set_level(level: LevelFilter) {
set_max_level(level);
}

@ -1,6 +1,7 @@
extern crate chrono;
extern crate colored;
#[macro_use]
extern crate log;
extern crate rag;
extern crate serde;
#[macro_use]
extern crate serde_derive;
@ -8,6 +9,7 @@ extern crate toml;
mod app;
mod apps;
mod logger;
mod proc;
mod result;
@ -15,7 +17,7 @@ use apps::Apps;
use result::Result;
fn main() {
if let Err(e) = rag::init() {
if let Err(e) = logger::init() {
println!("FATAL: Error initializing logger: {:?}", e);
std::process::exit(1);
}

Loading…
Cancel
Save