mirror of https://github.com/rwf2/Rocket.git
Add 'log = off' config option to disable all logging.
This commit is contained in:
parent
7b4b0646c5
commit
965c90afc9
|
@ -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")]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ impl<'r> Request<'r> {
|
|||
|
||||
#[doc(hidden)]
|
||||
pub fn example<F: Fn(&mut Request)>(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);
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -364,7 +364,7 @@ mod tests {
|
|||
fn req_route_mt_collide<S1, S2>(m: Method, mt1: S1, mt2: S2) -> bool
|
||||
where S1: Into<Option<&'static str>>, S2: Into<Option<&'static str>>
|
||||
{
|
||||
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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue