taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

commit 321eb9c9f99617c6febaa34fc797be815515d0b4
parent 259bc29a703d0e0c9b8a3b3d5e6c1b89bfa0bd8b
Author: Antoine A <>
Date:   Mon, 28 Jul 2025 18:00:23 +0200

common: improve logging

Diffstat:
Mcommon/taler-common/src/db.rs | 9+++++----
Mcommon/taler-common/src/lib.rs | 22+++++++---------------
Mcommon/taler-common/src/log.rs | 21++++++++++++++++++++-
Mcommon/taler-test-utils/src/lib.rs | 13+++----------
4 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/common/taler-common/src/db.rs b/common/taler-common/src/db.rs @@ -73,7 +73,7 @@ pub async fn dbinit( let path = sql_dir.join(file); match std::fs::read_to_string(&path) { Ok(content) => { - info!("applying {action}"); + info!(target: "dbinit", "applying {action}"); sqlx::raw_sql(&content).execute(conn).await?; Ok(()) } @@ -82,11 +82,11 @@ pub async fn dbinit( }; if reset { - info!("DB reset, sqlqir '{}'", sql_dir.to_string_lossy()); + info!(target: "dbinit", "reset, sqlqir '{}'", sql_dir.to_string_lossy()); exec_sql_file(&mut *tx, &format!("{prefix}-drop.sql"), "drop").await?; } - info!("DB initialization, sqlqir '{}'", sql_dir.to_string_lossy()); + info!(target: "dbinit", "initialization, sqlqir '{}'", sql_dir.to_string_lossy()); exec_sql_file(&mut *tx, "versioning.sql", "versioning").await?; @@ -97,7 +97,7 @@ pub async fn dbinit( for n in 1..9999 { let patch = format!("{prefix}-{n:0>4}"); if applied.contains(&patch) { - debug!("patch {patch} already applied"); + debug!(target: "dbinit", "patch {patch} already applied"); continue; } @@ -107,6 +107,7 @@ pub async fn dbinit( if let MigrationErr::Io(path, e) = &e { if e.kind() == ErrorKind::NotFound { debug!( + target: "dbinit", "path '{}' doesn't exist anymore, stopping", path.to_string_lossy() ); diff --git a/common/taler-common/src/lib.rs b/common/taler-common/src/lib.rs @@ -14,12 +14,13 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -use std::{future::Future, io::IsTerminal, path::PathBuf, time::Duration}; +use std::{future::Future, path::PathBuf, time::Duration}; use config::{Config, parser::ConfigSource}; -use log::TalerTime; -use tracing::{Level, error}; -use tracing_subscriber::{FmtSubscriber, util::SubscriberInitExt as _}; +use tracing::error; +use tracing_subscriber::util::SubscriberInitExt; + +use crate::log::taler_logger; pub mod api_common; pub mod api_params; @@ -30,7 +31,7 @@ pub mod config; pub mod db; pub mod error_code; pub mod json_file; -mod log; +pub mod log; pub mod types; #[derive(clap::Parser, Debug, Clone)] @@ -51,16 +52,7 @@ pub fn taler_main<F: Future<Output = Result<(), anyhow::Error>>>( args: CommonArgs, app: impl FnOnce(Config) -> F, ) { - // Setup logger - let level = args.log.unwrap_or(Level::INFO); - FmtSubscriber::builder() - .with_timer(TalerTime::new()) - .with_max_level(level) - .with_writer(std::io::stderr) - .with_ansi(std::io::stderr().is_terminal()) - .finish() - .init(); - + taler_logger(args.log).init(); let cfg = match Config::from_file(src, args.config) { Ok(cfg) => cfg, Err(err) => { diff --git a/common/taler-common/src/log.rs b/common/taler-common/src/log.rs @@ -14,8 +14,11 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +use std::io::IsTerminal; + use jiff::{Timestamp, fmt::StdFmtWrite, tz::TimeZone}; -use tracing_subscriber::fmt::time::FormatTime; +use tracing::Level; +use tracing_subscriber::{fmt::time::FormatTime, layer::SubscriberExt, util::SubscriberInitExt}; pub struct TalerTime { zone: Option<TimeZone>, @@ -53,3 +56,19 @@ impl FormatTime for TalerTime { Ok(()) } } + +pub fn taler_logger(max_level: Option<Level>) -> impl SubscriberInitExt { + let max_level = max_level.unwrap_or(Level::INFO); + tracing_subscriber::registry() + .with( + tracing_subscriber::fmt::layer() + .compact() + .with_writer(std::io::stderr) + .with_timer(TalerTime::new()) + .with_ansi(std::io::stderr().is_terminal()), + ) + .with(tracing_subscriber::filter::filter_fn(move |metadata| { + max_level == Level::TRACE + || (*metadata.level() <= max_level && !metadata.target().starts_with("sqlx")) + })) +} diff --git a/common/taler-test-utils/src/lib.rs b/common/taler-test-utils/src/lib.rs @@ -32,9 +32,9 @@ use taler_api::config::DbCfg; use taler_common::{ config::{Config, parser::ConfigSource}, db::{dbinit, pool}, + log::taler_logger, }; -use tracing::Level; -use tracing_subscriber::{FmtSubscriber, util::SubscriberInitExt}; +use tracing_subscriber::util::SubscriberInitExt; pub use axum::Router; pub mod json; @@ -92,13 +92,6 @@ async fn test_db() -> PgConnectOptions { PgConnectOptions::from_str(&format!("postgresql:/{name}")).unwrap() } -const TEST_TRACING_LEVEL: tracing::Level = Level::INFO; - fn setup_tracing() { - FmtSubscriber::builder() - .with_max_level(TEST_TRACING_LEVEL) - .with_writer(std::io::stderr) - .finish() - .try_init() - .ok(); + taler_logger(None).try_init().ok(); }