From ad1ee52d769f49cef9299ee52d56e45238a57ede Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 14 Nov 2021 01:02:48 +0100 Subject: [PATCH] server: Prevent sqlx from logging unless verbose --- Cargo.lock | 44 ++++--------------------------------- server/Cargo.toml | 2 +- server/src/infra/logging.rs | 28 +++++++++++++---------- 3 files changed, 21 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 002d4ae..ac30cde 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1911,15 +1911,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" -[[package]] -name = "matchers" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" -dependencies = [ - "regex-automata", -] - [[package]] name = "matches" version = "0.1.9" @@ -2623,15 +2614,6 @@ dependencies = [ "regex-syntax", ] -[[package]] -name = "regex-automata" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" -dependencies = [ - "regex-syntax", -] - [[package]] name = "regex-syntax" version = "0.6.25" @@ -3425,9 +3407,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.19" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ca517f43f0fb96e0c3072ed5c275fe5eece87e8cb52f4a77b69226d3b1c9df8" +checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" dependencies = [ "lazy_static", ] @@ -3453,36 +3435,18 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tracing-serde" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb65ea441fbb84f9f6748fd496cf7f63ec9af5bca94dd86456978d055e8eb28b" -dependencies = [ - "serde", - "tracing-core", -] - [[package]] name = "tracing-subscriber" -version = "0.2.20" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9cbe87a2fa7e35900ce5de20220a582a9483a7063811defce79d7cbd59d4cfe" +checksum = "80a4ddde70311d8da398062ecf6fc2c309337de6b0f77d6c27aff8d53f6fca52" dependencies = [ "ansi_term", - "chrono", - "lazy_static", - "matchers", - "regex", - "serde", - "serde_json", "sharded-slab", "smallvec", "thread_local", - "tracing", "tracing-core", "tracing-log", - "tracing-serde", ] [[package]] diff --git a/server/Cargo.toml b/server/Cargo.toml index b9bf089..c437d4b 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -42,7 +42,7 @@ tokio-stream = "*" tracing = "*" tracing-actix-web = "0.4.0-beta.7" tracing-log = "*" -tracing-subscriber = "*" +tracing-subscriber = "0.3" rand = { version = "0.8", features = ["small_rng", "getrandom"] } juniper_actix = "0.4.0" juniper = "0.15.6" diff --git a/server/src/infra/logging.rs b/server/src/infra/logging.rs index 8dbc294..a6f2d14 100644 --- a/server/src/infra/logging.rs +++ b/server/src/infra/logging.rs @@ -1,19 +1,15 @@ use crate::infra::configuration::Configuration; -use anyhow::Context; -use tracing::subscriber::set_global_default; -use tracing_log::LogTracer; +use tracing_subscriber::prelude::*; pub fn init(config: &Configuration) -> anyhow::Result<()> { let max_log_level = log_level_from_config(config); - let subscriber = tracing_subscriber::fmt() - .with_timer(tracing_subscriber::fmt::time::time()) - .with_target(false) - .with_level(true) - .with_max_level(max_log_level) - .finish(); - LogTracer::init().context("Failed to set logger")?; - set_global_default(subscriber).context("Failed to set subscriber")?; - // TODO: Only log SQL statements >= warn unless verbose. + let sqlx_max_log_level = sqlx_log_level_from_config(config); + let filter = tracing_subscriber::filter::Targets::new() + .with_target("lldap", max_log_level) + .with_target("sqlx", sqlx_max_log_level); + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer().with_filter(filter)) + .init(); Ok(()) } @@ -24,3 +20,11 @@ fn log_level_from_config(config: &Configuration) -> tracing::Level { tracing::Level::INFO } } + +fn sqlx_log_level_from_config(config: &Configuration) -> tracing::Level { + if config.verbose { + tracing::Level::INFO + } else { + tracing::Level::WARN + } +}