From fc46649e2e900beca15477c395478ea715c91d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 15 May 2024 07:00:18 +0100 Subject: [PATCH 1/5] rust: use the same logging mechanism in both servers --- rust/agama-server/src/agama-dbus-server.rs | 20 ++------------------ rust/agama-server/src/agama-web-server.rs | 2 ++ rust/agama-server/src/lib.rs | 1 + rust/agama-server/src/logs.rs | 17 +++++++++++++++++ 4 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 rust/agama-server/src/logs.rs diff --git a/rust/agama-server/src/agama-dbus-server.rs b/rust/agama-server/src/agama-dbus-server.rs index 0637a90a2d..fb44523038 100644 --- a/rust/agama-server/src/agama-dbus-server.rs +++ b/rust/agama-server/src/agama-dbus-server.rs @@ -1,11 +1,11 @@ use agama_server::{ l10n::{self, helpers}, + logs::start_logging, questions, }; use agama_lib::connection_to; use anyhow::Context; -use log::{self, LevelFilter}; use std::future::pending; const ADDRESS: &str = "unix:path=/run/agama/bus"; @@ -14,23 +14,7 @@ const SERVICE_NAME: &str = "org.opensuse.Agama1"; #[tokio::main] async fn main() -> Result<(), Box> { let locale = helpers::init_locale()?; - - // be smart with logging and log directly to journal if connected to it - if systemd_journal_logger::connected_to_journal() { - // unwrap here is intentional as we are sure no other logger is active yet - systemd_journal_logger::JournalLog::default() - .install() - .unwrap(); - log::set_max_level(LevelFilter::Info); // log only info for journal logger - } else { - simplelog::TermLogger::init( - LevelFilter::Info, // lets use info, trace provides too much output from libraries - simplelog::Config::default(), - simplelog::TerminalMode::Stderr, // only stderr output for easier filtering - simplelog::ColorChoice::Auto, - ) - .unwrap(); // unwrap here as we are sure no other logger active - } + start_logging().context("Could not initialize the logger")?; let connection = connection_to(ADDRESS) .await diff --git a/rust/agama-server/src/agama-web-server.rs b/rust/agama-server/src/agama-web-server.rs index cd749f4ee8..3a434166c4 100644 --- a/rust/agama-server/src/agama-web-server.rs +++ b/rust/agama-server/src/agama-web-server.rs @@ -10,6 +10,7 @@ use std::{ use agama_lib::connection_to; use agama_server::{ l10n::helpers, + logs::start_logging, web::{self, generate_token, run_monitor}, }; use anyhow::Context; @@ -292,6 +293,7 @@ async fn start_server(address: String, service: Router, ssl_acceptor: SslAccepto /// Start serving the API. /// `options`: command-line arguments. async fn serve_command(args: ServeArgs) -> anyhow::Result<()> { + start_logging().context("Could not initialize the logger")?; let journald = tracing_journald::layer().context("could not connect to journald")?; tracing_subscriber::registry().with(journald).init(); diff --git a/rust/agama-server/src/lib.rs b/rust/agama-server/src/lib.rs index fe2efcdef8..31e0023a70 100644 --- a/rust/agama-server/src/lib.rs +++ b/rust/agama-server/src/lib.rs @@ -2,6 +2,7 @@ pub mod cert; pub mod dbus; pub mod error; pub mod l10n; +pub mod logs; pub mod manager; pub mod network; pub mod questions; diff --git a/rust/agama-server/src/logs.rs b/rust/agama-server/src/logs.rs new file mode 100644 index 0000000000..4242097849 --- /dev/null +++ b/rust/agama-server/src/logs.rs @@ -0,0 +1,17 @@ +use log::{self, LevelFilter, SetLoggerError}; + +pub fn start_logging() -> Result<(), SetLoggerError> { + if systemd_journal_logger::connected_to_journal() { + // unwrap here is intentional as we are sure no other logger is active yet + systemd_journal_logger::JournalLog::default().install()?; + log::set_max_level(LevelFilter::Info); // log only info for journal logger + } else { + simplelog::TermLogger::init( + LevelFilter::Info, // lets use info, trace provides too much output from libraries + simplelog::Config::default(), + simplelog::TerminalMode::Stderr, // only stderr output for easier filtering + simplelog::ColorChoice::Auto, + )?; + } + Ok(()) +} From 09618815097164b2da166aa1064a7eab2257152e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 15 May 2024 12:58:06 +0100 Subject: [PATCH 2/5] rust: use 'tracing' as the logs system --- rust/Cargo.lock | 69 ++--------------------- rust/agama-server/Cargo.toml | 7 ++- rust/agama-server/src/agama-web-server.rs | 3 - rust/agama-server/src/logs.rs | 25 ++++---- 4 files changed, 22 insertions(+), 82 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 3fccd864bf..f0606a143a 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -111,6 +111,7 @@ dependencies = [ "hyper 1.2.0", "hyper-util", "jsonwebtoken", + "libsystemd", "log", "macaddr", "once_cell", @@ -123,8 +124,6 @@ dependencies = [ "serde_json", "serde_with", "serde_yaml", - "simplelog", - "systemd-journal-logger", "thiserror", "tokio", "tokio-openssl", @@ -1925,14 +1924,14 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libsystemd" -version = "0.6.0" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88b9597a67aa1c81a6624603e6bd0bcefb9e0f94c9c54970ec53771082104b4e" +checksum = "c592dc396b464005f78a5853555b9f240bc5378bf5221acc4e129910b2678869" dependencies = [ "hmac", "libc", "log", - "nix 0.26.4", + "nix 0.27.1", "nom", "once_cell", "serde", @@ -1999,9 +1998,6 @@ name = "log" version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" -dependencies = [ - "value-bag", -] [[package]] name = "macaddr" @@ -2132,6 +2128,7 @@ dependencies = [ "bitflags 2.5.0", "cfg-if", "libc", + "memoffset 0.9.0", ] [[package]] @@ -2251,15 +2248,6 @@ dependencies = [ "libc", ] -[[package]] -name = "num_threads" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" -dependencies = [ - "libc", -] - [[package]] name = "number_prefix" version = "0.4.0" @@ -3230,17 +3218,6 @@ dependencies = [ "time", ] -[[package]] -name = "simplelog" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0" -dependencies = [ - "log", - "termcolor", - "time", -] - [[package]] name = "siphasher" version = "0.3.11" @@ -3361,16 +3338,6 @@ dependencies = [ "libc", ] -[[package]] -name = "systemd-journal-logger" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "356b5cb52ce54916cbfaee19b07d305c7ea8ce5435a088c58743d4a0211f3eff" -dependencies = [ - "libsystemd", - "log", -] - [[package]] name = "temp-dir" version = "0.1.13" @@ -3389,15 +3356,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "terminal_size" version = "0.3.0" @@ -3446,9 +3404,7 @@ checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" dependencies = [ "deranged", "itoa", - "libc", "num-conv", - "num_threads", "powerfmt", "serde", "time-core", @@ -3942,12 +3898,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" -[[package]] -name = "value-bag" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74797339c3b98616c009c7c3eb53a0ce41e85c8ec66bd3db96ed132d20cfdee8" - [[package]] name = "vcpkg" version = "0.2.15" @@ -4073,15 +4023,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/rust/agama-server/Cargo.toml b/rust/agama-server/Cargo.toml index 76ab2b682c..ab26dcff19 100644 --- a/rust/agama-server/Cargo.toml +++ b/rust/agama-server/Cargo.toml @@ -11,8 +11,6 @@ anyhow = "1.0" agama-locale-data = { path = "../agama-locale-data" } agama-lib = { path = "../agama-lib" } log = "0.4" -simplelog = "0.12.1" -systemd-journal-logger = "1.0" zbus = { version = "3", default-features = false, features = ["tokio"] } zbus_macros = "3" uuid = { version = "1.3.4", features = ["v4"] } @@ -53,7 +51,10 @@ openssl = "0.10.64" hyper = "1.2.0" hyper-util = "0.1.3" tokio-openssl = "0.6.4" -futures-util = { version = "0.3.30", default-features = false, features = ["alloc"] } +futures-util = { version = "0.3.30", default-features = false, features = [ + "alloc", +] } +libsystemd = "0.7.0" [[bin]] name = "agama-dbus-server" diff --git a/rust/agama-server/src/agama-web-server.rs b/rust/agama-server/src/agama-web-server.rs index 3a434166c4..c6e41b2a39 100644 --- a/rust/agama-server/src/agama-web-server.rs +++ b/rust/agama-server/src/agama-web-server.rs @@ -28,7 +28,6 @@ use openssl::ssl::{Ssl, SslAcceptor, SslFiletype, SslMethod}; use tokio::sync::broadcast::channel; use tokio_openssl::SslStream; use tower::Service; -use tracing_subscriber::prelude::*; use utoipa::OpenApi; const DEFAULT_WEB_UI_DIR: &str = "/usr/share/agama/web_ui"; @@ -294,8 +293,6 @@ async fn start_server(address: String, service: Router, ssl_acceptor: SslAccepto /// `options`: command-line arguments. async fn serve_command(args: ServeArgs) -> anyhow::Result<()> { start_logging().context("Could not initialize the logger")?; - let journald = tracing_journald::layer().context("could not connect to journald")?; - tracing_subscriber::registry().with(journald).init(); let (tx, _) = channel(16); run_monitor(tx.clone()).await?; diff --git a/rust/agama-server/src/logs.rs b/rust/agama-server/src/logs.rs index 4242097849..cb791ea9f6 100644 --- a/rust/agama-server/src/logs.rs +++ b/rust/agama-server/src/logs.rs @@ -1,17 +1,18 @@ -use log::{self, LevelFilter, SetLoggerError}; +//! Functions to work with logs. -pub fn start_logging() -> Result<(), SetLoggerError> { - if systemd_journal_logger::connected_to_journal() { - // unwrap here is intentional as we are sure no other logger is active yet - systemd_journal_logger::JournalLog::default().install()?; - log::set_max_level(LevelFilter::Info); // log only info for journal logger +use anyhow::Context; +use libsystemd::logging; +use tracing_subscriber::prelude::*; + +/// Initializes the logging mechanism. +/// +/// It is based on [Tracing](https://github.com/tokio-rs/tracing), part of the Tokio ecosystem. +pub fn start_logging() -> anyhow::Result<()> { + if logging::connected_to_journal() { + let journald = tracing_journald::layer().context("could not connect to journald")?; + tracing_subscriber::registry().with(journald).init(); } else { - simplelog::TermLogger::init( - LevelFilter::Info, // lets use info, trace provides too much output from libraries - simplelog::Config::default(), - simplelog::TerminalMode::Stderr, // only stderr output for easier filtering - simplelog::ColorChoice::Auto, - )?; + tracing_subscriber::fmt::init(); } Ok(()) } From ad879566ab60005c2f3229f3aa2e4fceb28c79b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 15 May 2024 16:17:09 +0100 Subject: [PATCH 3/5] rust: include file/line when logging to stdout --- rust/agama-server/src/logs.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rust/agama-server/src/logs.rs b/rust/agama-server/src/logs.rs index cb791ea9f6..de9b546ed3 100644 --- a/rust/agama-server/src/logs.rs +++ b/rust/agama-server/src/logs.rs @@ -12,7 +12,12 @@ pub fn start_logging() -> anyhow::Result<()> { let journald = tracing_journald::layer().context("could not connect to journald")?; tracing_subscriber::registry().with(journald).init(); } else { - tracing_subscriber::fmt::init(); + let subscriber = tracing_subscriber::fmt() + .with_file(true) + .with_line_number(true) + .compact() + .finish(); + tracing::subscriber::set_global_default(subscriber)?; } Ok(()) } From db56ea5f6ddc2e101d045767e4beeb97d6c821da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 15 May 2024 16:20:22 +0100 Subject: [PATCH 4/5] rust: rename start_logging to init_logging --- rust/agama-server/src/agama-dbus-server.rs | 4 ++-- rust/agama-server/src/agama-web-server.rs | 4 ++-- rust/agama-server/src/logs.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/agama-server/src/agama-dbus-server.rs b/rust/agama-server/src/agama-dbus-server.rs index fb44523038..ca90d3db53 100644 --- a/rust/agama-server/src/agama-dbus-server.rs +++ b/rust/agama-server/src/agama-dbus-server.rs @@ -1,6 +1,6 @@ use agama_server::{ l10n::{self, helpers}, - logs::start_logging, + logs::init_logging, questions, }; @@ -14,7 +14,7 @@ const SERVICE_NAME: &str = "org.opensuse.Agama1"; #[tokio::main] async fn main() -> Result<(), Box> { let locale = helpers::init_locale()?; - start_logging().context("Could not initialize the logger")?; + init_logging().context("Could not initialize the logger")?; let connection = connection_to(ADDRESS) .await diff --git a/rust/agama-server/src/agama-web-server.rs b/rust/agama-server/src/agama-web-server.rs index c6e41b2a39..ab5d701306 100644 --- a/rust/agama-server/src/agama-web-server.rs +++ b/rust/agama-server/src/agama-web-server.rs @@ -10,7 +10,7 @@ use std::{ use agama_lib::connection_to; use agama_server::{ l10n::helpers, - logs::start_logging, + logs::init_logging, web::{self, generate_token, run_monitor}, }; use anyhow::Context; @@ -292,7 +292,7 @@ async fn start_server(address: String, service: Router, ssl_acceptor: SslAccepto /// Start serving the API. /// `options`: command-line arguments. async fn serve_command(args: ServeArgs) -> anyhow::Result<()> { - start_logging().context("Could not initialize the logger")?; + init_logging().context("Could not initialize the logger")?; let (tx, _) = channel(16); run_monitor(tx.clone()).await?; diff --git a/rust/agama-server/src/logs.rs b/rust/agama-server/src/logs.rs index de9b546ed3..1432b1157e 100644 --- a/rust/agama-server/src/logs.rs +++ b/rust/agama-server/src/logs.rs @@ -7,7 +7,7 @@ use tracing_subscriber::prelude::*; /// Initializes the logging mechanism. /// /// It is based on [Tracing](https://github.com/tokio-rs/tracing), part of the Tokio ecosystem. -pub fn start_logging() -> anyhow::Result<()> { +pub fn init_logging() -> anyhow::Result<()> { if logging::connected_to_journal() { let journald = tracing_journald::layer().context("could not connect to journald")?; tracing_subscriber::registry().with(journald).init(); From c521683dc95636c572c585e77f2516d76c912d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 15 May 2024 16:35:16 +0100 Subject: [PATCH 5/5] rust: update changes file --- rust/package/agama.changes | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/package/agama.changes b/rust/package/agama.changes index 9857943e3d..eb06387606 100644 --- a/rust/package/agama.changes +++ b/rust/package/agama.changes @@ -1,3 +1,13 @@ +------------------------------------------------------------------- +Wed May 15 15:21:30 UTC 2024 - Imobach Gonzalez Sosa + +- Improve logging in the D-Bus and web servers + (gh#openSUSE/agama#1215): + - Write to the stdout if they are not connected to + systemd-journald. + - The stdout logger includes the file/line (it was already + included when logging to systemd-journald). + ------------------------------------------------------------------- Wed May 15 14:08:26 UTC 2024 - Imobach Gonzalez Sosa