Skip to content

Commit

Permalink
Improve logging in the Rust part (#1215)
Browse files Browse the repository at this point in the history
* `agama-web-server` writes now to the stdout if it is not connected to
journald.
* `agama-dbus-server` uses
[tracing](https://github.com/tokio-rs/tracing).
* The stdout logger now includes the file/line. They were already
included in the journal logs.

```
2024-05-15T15:30:42.816878Z  WARN agama_server::network::nm::client: agama-server/src/network/nm/client.rs:155: Ignoring network device on path /org/freedesktop/NetworkManager/Devices/1
2024-05-15T15:30:42.845629Z  INFO agama_server::web::service: agama-server/src/web/service.rs:91: Serving static files from /usr/share/agama/web_ui
2024-05-15T15:30:42.901972Z  INFO agama_web_server: agama-server/src/agama-web-server.rs:253: Starting Agama web server at 0.0.0.0:443
2024-05-15T15:30:42.901993Z  INFO agama_web_server: agama-server/src/agama-web-server.rs:253: Starting Agama web server at 0.0.0.0:80
```
  • Loading branch information
imobachgs authored May 15, 2024
2 parents 3ef5965 + c521683 commit 066d480
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 88 deletions.
69 changes: 5 additions & 64 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions rust/agama-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down Expand Up @@ -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"
Expand Down
20 changes: 2 additions & 18 deletions rust/agama-server/src/agama-dbus-server.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -14,23 +14,7 @@ const SERVICE_NAME: &str = "org.opensuse.Agama1";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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
Expand Down
5 changes: 2 additions & 3 deletions rust/agama-server/src/agama-web-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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?;
Expand Down
1 change: 1 addition & 0 deletions rust/agama-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions rust/agama-server/src/logs.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
10 changes: 10 additions & 0 deletions rust/package/agama.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
-------------------------------------------------------------------
Wed May 15 15:21:30 UTC 2024 - Imobach Gonzalez Sosa <[email protected]>

- 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 <[email protected]>

Expand Down

0 comments on commit 066d480

Please sign in to comment.