From 6217832778b4b83de2b8be1738c66d63cefc18d3 Mon Sep 17 00:00:00 2001 From: Samuel Hurel Date: Fri, 15 Oct 2021 22:59:51 +0200 Subject: [PATCH] Use env_logger filters --- Cargo.lock | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 34 ++++++++++++++-- src/ndjson.rs | 25 ++++++++---- src/pretty.rs | 18 +++++---- src/wasm.rs | 12 +++--- 6 files changed, 174 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f7780b..d6198b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,27 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" +dependencies = [ + "memchr", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "backtrace" version = "0.3.32" @@ -40,6 +62,19 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" +[[package]] +name = "env_logger" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "failure" version = "0.1.5" @@ -67,6 +102,7 @@ name = "femme" version = "2.1.2" dependencies = [ "cfg-if", + "env_logger", "js-sys", "kv-log-macro", "log", @@ -86,6 +122,21 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "itoa" version = "0.4.4" @@ -165,6 +216,23 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a26af418b574bd56588335b3a3659a65725d4e636eb1016c2f9e3b38c7cc759" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + [[package]] name = "rustc-demangle" version = "0.1.15" @@ -234,6 +302,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "unicode-segmentation" version = "1.3.0" @@ -344,3 +421,34 @@ checksum = "3bb43f70885151e629e2a19ce9e50bd730fd436cfd4b666894c9ce4de9141164" dependencies = [ "nom", ] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 9cbc18f..b8a620b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ serde = "1.0.97" serde_derive = "1.0.97" serde_json = "1.0.56" cfg-if = "0.1.9" +env_logger = "0.8.3" [target.'cfg(target_arch = "wasm32")'.dependencies] web-sys = { version = "0.3.25", features = ["console"] } diff --git a/src/lib.rs b/src/lib.rs index 4d75fc6..5cdd620 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ //! log::info!("Listening on port 8080"); //! ``` +use env_logger::filter::{Builder, Filter}; pub use log::LevelFilter; #[cfg(not(target_arch = "wasm32"))] @@ -36,7 +37,30 @@ mod wasm; /// log::info!("Listening on port 8080"); /// ``` pub fn start() { - with_level(LevelFilter::Info); + let filter = Builder::from_env("RUST_LOG").build(); + with_filter(filter); +} + +/// Start logging with a log filter. +/// +/// # Examples +/// ``` +/// let filter = env_logger::filter::Builder::from_env("RUST_LOG").build(); +/// femme::with_filter(filter); +/// ``` +pub fn with_filter(filter: Filter) { + #[cfg(target_arch = "wasm32")] + wasm::start(filter); + + #[cfg(not(target_arch = "wasm32"))] + { + // Use ndjson in release mode, pretty logging while debugging. + if cfg!(debug_assertions) { + pretty::start(filter); + } else { + ndjson::start(filter); + } + } } /// Start logging with a log level. @@ -48,16 +72,18 @@ pub fn start() { /// femme::with_level(log::LevelFilter::Trace); /// ``` pub fn with_level(level: log::LevelFilter) { + let filter = Builder::new().filter_level(level).build(); + #[cfg(target_arch = "wasm32")] - wasm::start(level); + wasm::start(filter); #[cfg(not(target_arch = "wasm32"))] { // Use ndjson in release mode, pretty logging while debugging. if cfg!(debug_assertions) { - pretty::start(level); + pretty::start(filter); } else { - ndjson::start(level); + ndjson::start(filter); } } } diff --git a/src/ndjson.rs b/src/ndjson.rs index 7066a77..338ff4c 100644 --- a/src/ndjson.rs +++ b/src/ndjson.rs @@ -1,31 +1,40 @@ //! Print logs as ndjson. -use log::{kv, LevelFilter, Log, Metadata, Record}; +use env_logger::filter::Filter; +use log::{kv, Log, Metadata, Record}; use std::io::{self, StdoutLock, Write}; use std::time; /// Start logging. -pub(crate) fn start(level: LevelFilter) { - let logger = Box::new(Logger {}); +pub(crate) fn start(filter: Filter) { + let max_level = filter.filter(); + let logger = Box::new(Logger { filter }); log::set_boxed_logger(logger).expect("Could not start logging"); - log::set_max_level(level); + log::set_max_level(max_level); } #[derive(Debug)] -pub(crate) struct Logger {} +pub(crate) struct Logger { + filter: Filter, +} impl Log for Logger { fn enabled(&self, metadata: &Metadata<'_>) -> bool { - metadata.level() <= log::max_level() + self.filter.enabled(metadata) } fn log(&self, record: &Record<'_>) { - if self.enabled(record.metadata()) { + if self.filter.matches(record) { let stdout = io::stdout(); let mut handle = stdout.lock(); let level = get_level(record.level()); let time = time::UNIX_EPOCH.elapsed().unwrap().as_millis(); - write!(&mut handle, "{{\"level\":{},\"time\":{},\"msg\":", level, time).unwrap(); + write!( + &mut handle, + "{{\"level\":{},\"time\":{},\"msg\":", + level, time + ) + .unwrap(); serde_json::to_writer(&mut handle, record.args()).unwrap(); format_kv_pairs(&mut handle, &record); writeln!(&mut handle, "}}").unwrap(); diff --git a/src/pretty.rs b/src/pretty.rs index 221ee80..67bff1f 100644 --- a/src/pretty.rs +++ b/src/pretty.rs @@ -1,6 +1,7 @@ //! Pretty print logs. -use log::{kv, Level, LevelFilter, Log, Metadata, Record}; +use env_logger::filter::Filter; +use log::{kv, Level, Log, Metadata, Record}; use std::io::{self, StdoutLock, Write}; // ANSI term codes. @@ -11,22 +12,25 @@ const GREEN: &'static str = "\x1b[32m"; const YELLOW: &'static str = "\x1b[33m"; /// Start logging. -pub(crate) fn start(level: LevelFilter) { - let logger = Box::new(Logger {}); +pub(crate) fn start(filter: Filter) { + let max_level = filter.filter(); + let logger = Box::new(Logger { filter }); log::set_boxed_logger(logger).expect("Could not start logging"); - log::set_max_level(level); + log::set_max_level(max_level); } #[derive(Debug)] -pub(crate) struct Logger {} +pub(crate) struct Logger { + filter: Filter, +} impl Log for Logger { fn enabled(&self, metadata: &Metadata<'_>) -> bool { - metadata.level() <= log::max_level() + self.filter.enabled(metadata) } fn log(&self, record: &Record<'_>) { - if self.enabled(record.metadata()) { + if self.filter.matches(record) { let stdout = io::stdout(); let mut handle = stdout.lock(); format_src(&mut handle, &record); diff --git a/src/wasm.rs b/src/wasm.rs index efd717b..306d517 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -1,5 +1,6 @@ //! Print logs as ndjson. +use env_logger::filter::Filter; use js_sys::Object; use log::{kv, Level, LevelFilter, Log, Metadata, Record}; use wasm_bindgen::prelude::*; @@ -7,10 +8,11 @@ use wasm_bindgen::prelude::*; use std::collections::HashMap; /// Start logging. -pub(crate) fn start(level: LevelFilter) { - let logger = Box::new(Logger {}); +pub(crate) fn start(filter: Filter) { + let max_level = filter.filter(); + let logger = Box::new(Logger { filter }); log::set_boxed_logger(logger).expect("Could not start logging"); - log::set_max_level(level); + log::set_max_level(max_level); } #[derive(Debug)] @@ -18,11 +20,11 @@ pub(crate) struct Logger {} impl Log for Logger { fn enabled(&self, metadata: &Metadata<'_>) -> bool { - metadata.level() <= log::max_level() + self.filter.enabled(metadata) } fn log(&self, record: &Record<'_>) { - if self.enabled(record.metadata()) { + if self.filter.matches(record) { let args = format!("{}", record.args()).into(); let line = format_line(&record).into();