From 8ed85b6f72b699504a4577bfb33a0806b202859f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Tue, 29 Aug 2023 21:34:01 +0200 Subject: [PATCH] tracing --- rust/Cargo.lock | 5 +++-- rust/Cargo.toml | 2 +- rust/src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++------- rust/src/sqlite.rs | 4 ++-- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index eee7cc7..dff970e 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -618,9 +618,9 @@ checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" [[package]] name = "h2" -version = "0.3.20" +version = "0.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97ec8491ebaf99c8eaa73058b045fe58073cd6be7f596ac993ced0b0a0c01049" +checksum = "91fc23aa11be92976ef4729127f1a74adf36d8436f7816b185d18df956790833" dependencies = [ "bytes", "fnv", @@ -1997,6 +1997,7 @@ dependencies = [ "pin-project-lite", "socket2 0.5.3", "tokio-macros", + "tracing", "windows-sys", ] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 21169e2..4a3de8c 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -24,7 +24,7 @@ features = ["headers"] [dependencies.tokio] version = "1" -features = ["macros", "rt-multi-thread"] +features = ["macros", "rt-multi-thread", "tracing"] [dependencies.hyper] version = "0.14" diff --git a/rust/src/main.rs b/rust/src/main.rs index ab1890d..46589bc 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -73,18 +73,52 @@ impl From for MainResult { } } +fn init_tracing() { + use std::io::stdout; + use tracing::Level; + use tracing_subscriber::{ + filter::Targets, + fmt::{format::Format, Layer}, + prelude::*, + registry::Registry, + }; + // default is the Full format, there is no way to specify this, but it can be + // overridden via builder methods + let console_format = Format::default() + .with_ansi(true) + .with_target(true) + .with_level(true) + .json(); + + let console_layer = Layer::default() + .event_format(console_format) + .with_writer(stdout); + + let console_level = Level::DEBUG; + + let console_filter = Targets::new().with_target(env!("CARGO_PKG_NAME"), console_level); + + let console_layer = if true { + console_layer.boxed() + } else { + console_layer.with_filter(console_filter).boxed() + }; + + let registry = Registry::default() + // just an example, you can actuall pass Options here for layers that might be + // set/unset at runtime + .with(Some(console_layer)) + .with(None::>); + + tracing::subscriber::set_global_default(registry).unwrap(); +} + #[tokio::main] async fn main() -> MainResult { + init_tracing(); let args = Args::parse(); match args.command { Command::Serve(serve_args) => { - tracing_subscriber::fmt() - // .with_max_level(tracing::Level::DEBUG) - // .with_target(false) - // .with_env_filter("none,packager=debug,request=debug,sqlx=debug") - .compact() - .init(); - if let Err(e) = sqlite::migrate(&args.database_url).await { return <_ as Into>::into(e).into(); } diff --git a/rust/src/sqlite.rs b/rust/src/sqlite.rs index 24dfef8..999d77a 100644 --- a/rust/src/sqlite.rs +++ b/rust/src/sqlite.rs @@ -13,7 +13,7 @@ pub async fn init_database_pool(url: &str) -> Result, StartError> { .max_connections(5) .connect_with( SqliteConnectOptions::from_str(url)? - .log_statements(log::LevelFilter::Off) + .log_statements(log::LevelFilter::Debug) .log_slow_statements(log::LevelFilter::Warn, time::Duration::from_millis(100)) .pragma("foreign_keys", "1"), ) @@ -26,7 +26,7 @@ pub async fn migrate(url: &str) -> Result<(), StartError> { .connect_with( SqliteConnectOptions::from_str(url)? .pragma("foreign_keys", "0") - .log_statements(log::LevelFilter::Off), + .log_statements(log::LevelFilter::Debug), ) .await?;