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-dbus-server.rs b/rust/agama-server/src/agama-dbus-server.rs index 0637a90a2d..ca90d3db53 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::init_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 - } + 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 cd749f4ee8..ab5d701306 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::init_logging, web::{self, generate_token, run_monitor}, }; use anyhow::Context; @@ -27,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"; @@ -292,8 +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<()> { - let journald = tracing_journald::layer().context("could not connect to journald")?; - tracing_subscriber::registry().with(journald).init(); + init_logging().context("Could not initialize the logger")?; let (tx, _) = channel(16); run_monitor(tx.clone()).await?; 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..1432b1157e --- /dev/null +++ b/rust/agama-server/src/logs.rs @@ -0,0 +1,23 @@ +//! Functions to work with logs. + +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 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(); + } else { + let subscriber = tracing_subscriber::fmt() + .with_file(true) + .with_line_number(true) + .compact() + .finish(); + tracing::subscriber::set_global_default(subscriber)?; + } + Ok(()) +} 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