From 965c90afc90b739d450742451e30202b1887a011 Mon Sep 17 00:00:00 2001 From: Beatriz Rizental Date: Tue, 3 Jul 2018 13:47:17 -0700 Subject: [PATCH] Add 'log = off' config option to disable all logging. --- contrib/lib/tests/templates.rs | 2 +- core/lib/src/config/mod.rs | 7 +++++++ core/lib/src/lib.rs | 4 ++-- core/lib/src/logger.rs | 33 ++++++++++++++++++++++++--------- core/lib/src/request/request.rs | 2 +- core/lib/src/request/tests.rs | 2 +- core/lib/src/rocket.rs | 15 +++++++-------- core/lib/src/router/collider.rs | 4 ++-- core/lib/src/router/mod.rs | 4 ++-- core/lib/tests/limits.rs | 2 +- 10 files changed, 48 insertions(+), 27 deletions(-) diff --git a/contrib/lib/tests/templates.rs b/contrib/lib/tests/templates.rs index b553c93f..4bf7f414 100644 --- a/contrib/lib/tests/templates.rs +++ b/contrib/lib/tests/templates.rs @@ -20,7 +20,7 @@ mod templates_tests { .extra("template_dir", template_root().to_str().expect("template directory")) .expect("valid configuration"); - ::rocket::custom(config, true).attach(Template::fairing()) + ::rocket::custom(config).attach(Template::fairing()) } #[cfg(feature = "tera_templates")] diff --git a/core/lib/src/config/mod.rs b/core/lib/src/config/mod.rs index 6f80ec17..0fff9d13 100644 --- a/core/lib/src/config/mod.rs +++ b/core/lib/src/config/mod.rs @@ -997,6 +997,13 @@ mod test { "#.to_string(), TEST_CONFIG_FILENAME), { default_config(Staging).log_level(LoggingLevel::Critical) }); + + check_config!(RocketConfig::parse(r#" + [stage] + log = "off" + "#.to_string(), TEST_CONFIG_FILENAME), { + default_config(Staging).log_level(LoggingLevel::Off) + }); } #[test] diff --git a/core/lib/src/lib.rs b/core/lib/src/lib.rs index dce3107f..9848564c 100644 --- a/core/lib/src/lib.rs +++ b/core/lib/src/lib.rs @@ -160,6 +160,6 @@ pub fn ignite() -> Rocket { /// Alias to [Rocket::custom()](/rocket/struct.Rocket.html#method.custom). /// Creates a new instance of `Rocket` with a custom configuration. -pub fn custom(config: config::Config, log: bool) -> Rocket { - Rocket::custom(config, log) +pub fn custom(config: config::Config) -> Rocket { + Rocket::custom(config) } diff --git a/core/lib/src/logger.rs b/core/lib/src/logger.rs index 30823755..ef7fac97 100644 --- a/core/lib/src/logger.rs +++ b/core/lib/src/logger.rs @@ -17,15 +17,18 @@ pub enum LoggingLevel { Normal, /// Shows everything. Debug, + /// Shows nothing. + Off, } impl LoggingLevel { #[inline(always)] - fn max_log_level(&self) -> log::Level { + fn to_level_filter(&self) -> log::LevelFilter { match *self { - LoggingLevel::Critical => log::Level::Warn, - LoggingLevel::Normal => log::Level::Info, - LoggingLevel::Debug => log::Level::Trace, + LoggingLevel::Critical => log::LevelFilter::Warn, + LoggingLevel::Normal => log::LevelFilter::Info, + LoggingLevel::Debug => log::LevelFilter::Trace, + LoggingLevel::Off => log::LevelFilter::Off } } } @@ -38,7 +41,8 @@ impl FromStr for LoggingLevel { "critical" => LoggingLevel::Critical, "normal" => LoggingLevel::Normal, "debug" => LoggingLevel::Debug, - _ => return Err("a log level (debug, normal, critical)") + "off" => LoggingLevel::Off, + _ => return Err("a log level (off, debug, normal, critical)") }; Ok(level) @@ -51,6 +55,7 @@ impl fmt::Display for LoggingLevel { LoggingLevel::Critical => "critical", LoggingLevel::Normal => "normal", LoggingLevel::Debug => "debug", + LoggingLevel::Off => "off" }; write!(f, "{}", string) @@ -77,7 +82,10 @@ macro_rules! warn_ { ($($args:expr),+) => { log_!(warn: $($args),+); }; } impl log::Log for RocketLogger { #[inline(always)] fn enabled(&self, record: &log::Metadata) -> bool { - record.target().starts_with("launch") || record.level() <= self.0.max_log_level() + match self.0.to_level_filter().to_level() { + Some(max) => record.level() <= max || record.target().starts_with("launch"), + None => false + } } fn log(&self, record: &log::Record) { @@ -134,7 +142,11 @@ impl log::Log for RocketLogger { } } -pub(crate) fn try_init(level: LoggingLevel, verbose: bool) { +pub(crate) fn try_init(level: LoggingLevel, verbose: bool) -> bool { + if level == LoggingLevel::Off { + return false; + } + if !::isatty::stdout_isatty() || (cfg!(windows) && !Paint::enable_windows_ascii()) { Paint::disable(); } @@ -146,7 +158,10 @@ pub(crate) fn try_init(level: LoggingLevel, verbose: bool) { } pop_max_level(); + return false; } + + true } use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering}; @@ -180,7 +195,7 @@ fn usize_to_filter(num: usize) -> log::LevelFilter { pub(crate) fn push_max_level(level: LoggingLevel) { LAST_LOG_FILTER.store(filter_to_usize(log::max_level()), Ordering::Release); PUSHED.store(true, Ordering::Release); - log::set_max_level(level.max_log_level().to_level_filter()); + log::set_max_level(level.to_level_filter()); } pub(crate) fn pop_max_level() { @@ -190,6 +205,6 @@ pub(crate) fn pop_max_level() { } #[doc(hidden)] -pub fn init(level: LoggingLevel) { +pub fn init(level: LoggingLevel) -> bool { try_init(level, true) } diff --git a/core/lib/src/request/request.rs b/core/lib/src/request/request.rs index 7c0a1986..b819fb64 100644 --- a/core/lib/src/request/request.rs +++ b/core/lib/src/request/request.rs @@ -73,7 +73,7 @@ impl<'r> Request<'r> { #[doc(hidden)] pub fn example(method: Method, uri: &str, f: F) { - let rocket = Rocket::custom(Config::development().unwrap(), true); + let rocket = Rocket::custom(Config::development().unwrap()); let mut request = Request::new(&rocket, method, uri); f(&mut request); } diff --git a/core/lib/src/request/tests.rs b/core/lib/src/request/tests.rs index 7d820961..3b997556 100644 --- a/core/lib/src/request/tests.rs +++ b/core/lib/src/request/tests.rs @@ -21,7 +21,7 @@ macro_rules! assert_headers { // Dispatch the request and check that the headers are what we expect. let config = Config::development().unwrap(); - let r = Rocket::custom(config, true); + let r = Rocket::custom(config); let req = Request::from_hyp(&r, h_method, h_headers, h_uri, h_addr).unwrap(); let actual_headers = req.headers(); for (key, values) in expected.iter() { diff --git a/core/lib/src/rocket.rs b/core/lib/src/rocket.rs index 0eb5aa16..2ac4273d 100644 --- a/core/lib/src/rocket.rs +++ b/core/lib/src/rocket.rs @@ -344,7 +344,7 @@ impl Rocket { #[inline] pub fn ignite() -> Rocket { // Note: init() will exit the process under config errors. - Rocket::configured(config::init(), true) + Rocket::configured(config::init()) } /// Creates a new `Rocket` application using the supplied custom @@ -368,20 +368,19 @@ impl Rocket { /// .finalize()?; /// /// # #[allow(unused_variables)] - /// let app = rocket::custom(config, false); + /// let app = rocket::custom(config); /// # Ok(()) /// # } /// ``` #[inline] - pub fn custom(config: Config, log: bool) -> Rocket { - Rocket::configured(config, log) + pub fn custom(config: Config) -> Rocket { + Rocket::configured(config) } #[inline] - fn configured(config: Config, log: bool) -> Rocket { - if log { - // Initialize logger. Temporary weaken log level for launch info. - logger::try_init(config.log_level, false); + fn configured(config: Config) -> Rocket { + if logger::try_init(config.log_level, false) { + // Temporary weaken log level for launch info. logger::push_max_level(logger::LoggingLevel::Normal); } diff --git a/core/lib/src/router/collider.rs b/core/lib/src/router/collider.rs index 62c514c0..ee8f3f05 100644 --- a/core/lib/src/router/collider.rs +++ b/core/lib/src/router/collider.rs @@ -364,7 +364,7 @@ mod tests { fn req_route_mt_collide(m: Method, mt1: S1, mt2: S2) -> bool where S1: Into>, S2: Into> { - let rocket = Rocket::custom(Config::development().unwrap(), true); + let rocket = Rocket::custom(Config::development().unwrap()); let mut req = Request::new(&rocket, m, "/"); if let Some(mt_str) = mt1.into() { if m.supports_payload() { @@ -422,7 +422,7 @@ mod tests { } fn req_route_path_collide(a: &'static str, b: &'static str) -> bool { - let rocket = Rocket::custom(Config::development().unwrap(), true); + let rocket = Rocket::custom(Config::development().unwrap()); let req = Request::new(&rocket, Get, a.to_string()); let route = Route::ranked(0, Get, b.to_string(), dummy_handler); route.collides_with(&req) diff --git a/core/lib/src/router/mod.rs b/core/lib/src/router/mod.rs index 7ee17156..dbf57b6d 100644 --- a/core/lib/src/router/mod.rs +++ b/core/lib/src/router/mod.rs @@ -165,7 +165,7 @@ mod test { } fn route<'a>(router: &'a Router, method: Method, uri: &str) -> Option<&'a Route> { - let rocket = Rocket::custom(Config::development().unwrap(), true); + let rocket = Rocket::custom(Config::development().unwrap()); let request = Request::new(&rocket, method, Uri::new(uri)); let matches = router.route(&request); if matches.len() > 0 { @@ -176,7 +176,7 @@ mod test { } fn matches<'a>(router: &'a Router, method: Method, uri: &str) -> Vec<&'a Route> { - let rocket = Rocket::custom(Config::development().unwrap(), true); + let rocket = Rocket::custom(Config::development().unwrap()); let request = Request::new(&rocket, method, Uri::new(uri)); router.route(&request) } diff --git a/core/lib/tests/limits.rs b/core/lib/tests/limits.rs index d0f8d3fa..cdd723ec 100644 --- a/core/lib/tests/limits.rs +++ b/core/lib/tests/limits.rs @@ -26,7 +26,7 @@ mod limits_tests { .limits(Limits::default().limit("forms", limit)) .unwrap(); - rocket::custom(config, true).mount("/", routes![super::index]) + rocket::custom(config).mount("/", routes![super::index]) } #[test]