commit
e099c9108c
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
root = True
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 4
|
||||||
|
trim_trailing_whitespace = True
|
||||||
|
insert_final_newline = True
|
||||||
|
|
||||||
|
[*.yml]
|
||||||
|
indent_style = space
|
@ -0,0 +1,3 @@
|
|||||||
|
/target/
|
||||||
|
**/*.rs.bk
|
||||||
|
Cargo.lock
|
@ -0,0 +1,18 @@
|
|||||||
|
[package]
|
||||||
|
name = "rag"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["rascul <rascul3@gmail.com>"]
|
||||||
|
description = "Simple logger"
|
||||||
|
license = "MIT"
|
||||||
|
repository = "https://github.com/rascul/rag"
|
||||||
|
documentation = "https://docs.rs/rag"
|
||||||
|
readme = "README.md"
|
||||||
|
keywords = ["log"]
|
||||||
|
|
||||||
|
[badges]
|
||||||
|
travis-ci = { repository = "/rascul/rag" }
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
chrono = "0.4"
|
||||||
|
colored = "1.6"
|
||||||
|
log = "0.3"
|
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2017 Ray Schulz <rascul3@gmail.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
@ -0,0 +1,32 @@
|
|||||||
|
use std::fmt::{Display, Formatter, Result as FmtResult};
|
||||||
|
|
||||||
|
use log::SetLoggerError;
|
||||||
|
|
||||||
|
pub type RagResult<T> = Result<T, RagError>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum RagError {
|
||||||
|
SetLogger(SetLoggerError),
|
||||||
|
Empty,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<()> for RagError {
|
||||||
|
fn from(_: ()) -> RagError {
|
||||||
|
RagError::Empty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SetLoggerError> for RagError {
|
||||||
|
fn from(e: SetLoggerError) -> RagError {
|
||||||
|
RagError::SetLogger(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for RagError {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
||||||
|
match *self {
|
||||||
|
RagError::SetLogger(ref e) => Display::fmt(e, f),
|
||||||
|
RagError::Empty => Display::fmt("Empty", f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
/// Logger implementation for colors in the terminal and in the future
|
||||||
|
/// logging to a file and to a remote log server
|
||||||
|
|
||||||
|
extern crate chrono;
|
||||||
|
extern crate colored;
|
||||||
|
extern crate log;
|
||||||
|
|
||||||
|
use colored::*;
|
||||||
|
use log::{Log, LogLevel, LogLevelFilter, LogMetadata, LogRecord, set_logger};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
mod error;
|
||||||
|
|
||||||
|
use error::RagResult;
|
||||||
|
|
||||||
|
struct Logger;
|
||||||
|
|
||||||
|
impl Log for Logger {
|
||||||
|
fn enabled(&self, metadata: &LogMetadata) -> bool {
|
||||||
|
metadata.level() <= LogLevel::Info
|
||||||
|
}
|
||||||
|
|
||||||
|
fn log(&self, record: &LogRecord) {
|
||||||
|
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 => {
|
||||||
|
(format!("{} {} {}", ts.white().bold(), "ERR".red(), record.args()), 3)
|
||||||
|
}
|
||||||
|
LogLevel::Warn => {
|
||||||
|
(format!("{} {} {}", ts.white().bold(), "WRN".purple(), record.args()), 4)
|
||||||
|
}
|
||||||
|
LogLevel::Info => {
|
||||||
|
(format!("{} {} {}", ts.white().bold(), "INF".cyan(), record.args()), 6)
|
||||||
|
}
|
||||||
|
LogLevel::Debug => {
|
||||||
|
(format!("{} {} {}", ts.white().bold(), "DBG".yellow(), record.args()), 7)
|
||||||
|
}
|
||||||
|
LogLevel::Trace => {
|
||||||
|
(format!("{} {} {}", ts.white().bold(), "TRC".green(), record.args()), 0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
println!("{}", msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init() -> RagResult<()> {
|
||||||
|
Ok(set_logger(|max_log_level| {
|
||||||
|
max_log_level.set(LogLevelFilter::Info);
|
||||||
|
Box::new(Logger)
|
||||||
|
})?)
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn initialize() {
|
||||||
|
if init().is_err() {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue