systemd: Add module for logging to the journal

This commit is contained in:
Lorenz Leutgeb 2025-07-29 16:50:25 +02:00 committed by Fintan Halpenny
parent 2776e003dc
commit c7f3c47f9a
4 changed files with 46 additions and 1 deletions

14
Cargo.lock generated
View File

@ -2782,6 +2782,10 @@ dependencies = [
[[package]]
name = "radicle-systemd"
version = "0.9.0"
dependencies = [
"log",
"systemd-journal-logger",
]
[[package]]
name = "radicle-term"
@ -3432,6 +3436,16 @@ dependencies = [
"syn 2.0.89",
]
[[package]]
name = "systemd-journal-logger"
version = "2.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7266304d24ca5a4b230545fc558c80e18bd3e1d2eb1be149b6bcd04398d3e79c"
dependencies = [
"log",
"rustix 1.0.7",
]
[[package]]
name = "tar"
version = "0.4.40"

View File

@ -8,6 +8,11 @@ edition.workspace = true
version = "0.9.0"
rust-version.workspace = true
[dependencies]
log = { workspace = true, optional = true }
systemd-journal-logger = { version = "2.2.2", optional = true }
[features]
default = ["listen"]
default = ["journal", "listen"]
journal = ["dep:log", "dep:systemd-journal-logger"]
listen = []

View File

@ -0,0 +1,23 @@
use systemd_journal_logger::{connected_to_journal, current_exe_identifier, JournalLog};
/// If the current process is directly connected to the systemd journal,
/// return a logger that will write to it.
pub fn logger<K, V, I>(
default_identifier: String,
extra_fields: I,
) -> std::io::Result<Option<Box<dyn log::Log>>>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: AsRef<[u8]>,
{
if !connected_to_journal() {
return Ok(None);
}
Ok(Some(Box::new(
JournalLog::new()?
.with_syslog_identifier(current_exe_identifier().unwrap_or(default_identifier))
.with_extra_fields(extra_fields),
)))
}

View File

@ -1,4 +1,7 @@
//! Library for interaction with systemd, specialized for Radicle.
#[cfg(feature = "journal")]
pub mod journal;
#[cfg(feature = "listen")]
pub mod listen;