server: Prevent sqlx from logging unless verbose

This commit is contained in:
Valentin Tolmer 2021-11-14 01:02:48 +01:00 committed by nitnelave
parent 9124339b96
commit ad1ee52d76
3 changed files with 21 additions and 53 deletions

44
Cargo.lock generated
View File

@ -1911,15 +1911,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" 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]] [[package]]
name = "matches" name = "matches"
version = "0.1.9" version = "0.1.9"
@ -2623,15 +2614,6 @@ dependencies = [
"regex-syntax", "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]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.25" version = "0.6.25"
@ -3425,9 +3407,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing-core" name = "tracing-core"
version = "0.1.19" version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ca517f43f0fb96e0c3072ed5c275fe5eece87e8cb52f4a77b69226d3b1c9df8" checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
] ]
@ -3453,36 +3435,18 @@ dependencies = [
"tracing-core", "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]] [[package]]
name = "tracing-subscriber" name = "tracing-subscriber"
version = "0.2.20" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9cbe87a2fa7e35900ce5de20220a582a9483a7063811defce79d7cbd59d4cfe" checksum = "80a4ddde70311d8da398062ecf6fc2c309337de6b0f77d6c27aff8d53f6fca52"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"chrono",
"lazy_static",
"matchers",
"regex",
"serde",
"serde_json",
"sharded-slab", "sharded-slab",
"smallvec", "smallvec",
"thread_local", "thread_local",
"tracing",
"tracing-core", "tracing-core",
"tracing-log", "tracing-log",
"tracing-serde",
] ]
[[package]] [[package]]

View File

@ -42,7 +42,7 @@ tokio-stream = "*"
tracing = "*" tracing = "*"
tracing-actix-web = "0.4.0-beta.7" tracing-actix-web = "0.4.0-beta.7"
tracing-log = "*" tracing-log = "*"
tracing-subscriber = "*" tracing-subscriber = "0.3"
rand = { version = "0.8", features = ["small_rng", "getrandom"] } rand = { version = "0.8", features = ["small_rng", "getrandom"] }
juniper_actix = "0.4.0" juniper_actix = "0.4.0"
juniper = "0.15.6" juniper = "0.15.6"

View File

@ -1,19 +1,15 @@
use crate::infra::configuration::Configuration; use crate::infra::configuration::Configuration;
use anyhow::Context; use tracing_subscriber::prelude::*;
use tracing::subscriber::set_global_default;
use tracing_log::LogTracer;
pub fn init(config: &Configuration) -> anyhow::Result<()> { pub fn init(config: &Configuration) -> anyhow::Result<()> {
let max_log_level = log_level_from_config(config); let max_log_level = log_level_from_config(config);
let subscriber = tracing_subscriber::fmt() let sqlx_max_log_level = sqlx_log_level_from_config(config);
.with_timer(tracing_subscriber::fmt::time::time()) let filter = tracing_subscriber::filter::Targets::new()
.with_target(false) .with_target("lldap", max_log_level)
.with_level(true) .with_target("sqlx", sqlx_max_log_level);
.with_max_level(max_log_level) tracing_subscriber::registry()
.finish(); .with(tracing_subscriber::fmt::layer().with_filter(filter))
LogTracer::init().context("Failed to set logger")?; .init();
set_global_default(subscriber).context("Failed to set subscriber")?;
// TODO: Only log SQL statements >= warn unless verbose.
Ok(()) Ok(())
} }
@ -24,3 +20,11 @@ fn log_level_from_config(config: &Configuration) -> tracing::Level {
tracing::Level::INFO tracing::Level::INFO
} }
} }
fn sqlx_log_level_from_config(config: &Configuration) -> tracing::Level {
if config.verbose {
tracing::Level::INFO
} else {
tracing::Level::WARN
}
}