Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging in the Rust part #1215

Merged
merged 5 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we return here error if logging failed to be initialized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should return an error (see the ? sign).


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 <igonzalezsosa@suse.com>

- 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 <igonzalezsosa@suse.com>

Expand Down