From e26bdf412e3f0dc931175549276372872c69ca44 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 27 Feb 2019 10:40:43 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ Cargo.toml | 12 ++++++++++ LICENSE | 12 +++++----- README.md | 31 +++++++++++++++++++++++-- examples/example.rs | 14 ++++++++++++ src/lib.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 examples/example.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6936990 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f2d38c3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "beautylog" +version = "0.1.0" +authors = ["Thomas Forgione "] +edition = "2018" +description = "A beautiful logger in rust" +license = "LGPL-3.0-only" +repository = "https://gitea.tforgione.fr/tforgione/beautylog" + +[dependencies] +log = "0.4.6" +colored = "1.7.0" diff --git a/LICENSE b/LICENSE index 1344add..8cacfd4 100644 --- a/LICENSE +++ b/LICENSE @@ -13,38 +13,38 @@ by the additional permissions listed below. 0. Additional Definitions. - + As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. - + "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. - + An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. - + A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". - + The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. - + The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility diff --git a/README.md b/README.md index 3d0ba69..620485f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,30 @@ -# beautylog +# beautylof + +*A beautiful logger in rust*. + +This repository contains the beauty logger. It's based on the [log](https://crates.io/crates/log) crate. + +It is made to be super simple and to mimic the output of rustup and cargo. + +### Usage +``` rust +#[macro_use] +extern crate log; + +fn main() { + + beautylog::init(log::LevelFilter::Trace).ok(); + + info!("some information"); + warn!("something went wrong"); + debug!("some debug info"); + trace!("yo"); + error!("something went horribly wrong"); + +} +``` + +### How it looks like +![how it renders](https://gitea.tforgione.fr/attachments/3417667c-746d-4f7f-bb79-c4ffa4913281) + -A beautiful logger in rust \ No newline at end of file diff --git a/examples/example.rs b/examples/example.rs new file mode 100644 index 0000000..cb68206 --- /dev/null +++ b/examples/example.rs @@ -0,0 +1,14 @@ +#[macro_use] +extern crate log; + +fn main() { + + beautylog::init(log::LevelFilter::Trace).ok(); + + info!("some information"); + warn!("something went wrong"); + debug!("some debug info"); + trace!("yo"); + error!("something went horribly wrong"); + +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..03cef40 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,56 @@ +//! This crate contains the beauty logger. +//! +//! It is made to be super simple and to mimic the output of rustup and cargo. +//! ### Usage: +//! ``` +//! #[macro_use] +//! extern crate log; +//! +//! fn main() { +//! +//! beautylog::init(log::LevelFilter::Trace).ok(); +//! +//! info!("some information"); +//! warn!("something went wrong"); +//! debug!("some debug info"); +//! trace!("yo"); +//! error!("something went horribly wrong"); +//! +//! } +//! ``` +//! +//! ### How it looks like +//! ![how it renders](https://gitea.tforgione.fr/attachments/3417667c-746d-4f7f-bb79-c4ffa4913281) +//! + +use log::{Record, Level, Metadata, SetLoggerError, LevelFilter}; +use colored::*; + +struct BeautyLogger; +static LOGGER: BeautyLogger = BeautyLogger; + +/// Initializes the beauty logger with the corresponding level filter. +pub fn init(level: LevelFilter) -> Result<(), SetLoggerError> { + log::set_logger(&LOGGER) + .map(|()| log::set_max_level(level)) +} + +impl log::Log for BeautyLogger { + fn enabled(&self, _: &Metadata) -> bool { + true + } + + fn log(&self, record: &Record) { + if self.enabled(record.metadata()) { + match record.level() { + Level::Error => eprintln!("{} {}", "error:".bold().red(), record.args()), + Level::Warn => eprintln!("{} {}", "warning:".bold().yellow(), record.args()), + Level::Info => eprintln!("{} {}", "info:".bold(), record.args()), + Level::Debug => eprintln!("{} {}", "debug:".bold().blue(), record.args()), + Level::Trace => eprintln!("{} {}", "trace:".bold().cyan(), record.args()), + } + } + } + + fn flush(&self) {} +}