Propagate 'log_level' to SQLx logs.

Closes #1798.
This commit is contained in:
Sergio Benitez 2021-08-08 14:03:20 -07:00
parent 1a8574e491
commit a16c66eae9
2 changed files with 22 additions and 12 deletions

View File

@ -188,6 +188,7 @@ mod deadpool_postgres {
mod sqlx { mod sqlx {
use sqlx::ConnectOptions; use sqlx::ConnectOptions;
use super::{Duration, Error, Config, Figment}; use super::{Duration, Error, Config, Figment};
use rocket::config::LogLevel;
type Options<D> = <<D as sqlx::Database>::Connection as sqlx::Connection>::Options; type Options<D> = <<D as sqlx::Database>::Connection as sqlx::Connection>::Options;
@ -210,9 +211,16 @@ mod sqlx {
async fn init(figment: &Figment) -> Result<Self, Self::Error> { async fn init(figment: &Figment) -> Result<Self, Self::Error> {
let config = figment.extract::<Config>()?; let config = figment.extract::<Config>()?;
let mut opts = config.url.parse::<Options<D>>().map_err(Error::Init)?; let mut opts = config.url.parse::<Options<D>>().map_err(Error::Init)?;
opts.disable_statement_logging();
specialize(&mut opts, &config); specialize(&mut opts, &config);
opts.disable_statement_logging();
if let Ok(level) = figment.extract_inner::<LogLevel>(rocket::Config::LOG_LEVEL) {
if !matches!(level, LogLevel::Normal | LogLevel::Off) {
opts.log_statements(level.into())
.log_slow_statements(level.into(), Duration::default());
}
}
sqlx::pool::PoolOptions::new() sqlx::pool::PoolOptions::new()
.max_connections(config.max_connections as u32) .max_connections(config.max_connections as u32)
.connect_timeout(Duration::from_secs(config.connect_timeout)) .connect_timeout(Duration::from_secs(config.connect_timeout))

View File

@ -79,7 +79,7 @@ impl log::Log for RocketLogger {
let max = log::max_level(); let max = log::max_level();
let from = |path| record.module_path().map_or(false, |m| m.starts_with(path)); let from = |path| record.module_path().map_or(false, |m| m.starts_with(path));
let debug_only = from("hyper") || from("rustls") || from("r2d2"); let debug_only = from("hyper") || from("rustls") || from("r2d2");
if LogLevel::Debug.to_level_filter() > max && debug_only { if log::LevelFilter::from(LogLevel::Debug) > max && debug_only {
return; return;
} }
@ -153,7 +153,18 @@ pub(crate) fn init(config: &crate::Config) {
Paint::disable(); Paint::disable();
} }
log::set_max_level(config.log_level.to_level_filter()); log::set_max_level(config.log_level.into());
}
}
impl From<LogLevel> for log::LevelFilter {
fn from(level: LogLevel) -> Self {
match level {
LogLevel::Critical => log::LevelFilter::Warn,
LogLevel::Normal => log::LevelFilter::Info,
LogLevel::Debug => log::LevelFilter::Trace,
LogLevel::Off => log::LevelFilter::Off
}
} }
} }
@ -166,15 +177,6 @@ impl LogLevel {
LogLevel::Off => "off", LogLevel::Off => "off",
} }
} }
fn to_level_filter(self) -> log::LevelFilter {
match self {
LogLevel::Critical => log::LevelFilter::Warn,
LogLevel::Normal => log::LevelFilter::Info,
LogLevel::Debug => log::LevelFilter::Trace,
LogLevel::Off => log::LevelFilter::Off
}
}
} }
impl FromStr for LogLevel { impl FromStr for LogLevel {