Crate systemd_journal_logger

Source
Expand description

A pure Rust log logger for the systemd journal.

§Usage

Create a JournalLog with JournalLog::new and then use JournalLog::install to setup journal logging. Then configure the logging level and now you can use the standard macros from the log crate to send log messages to the systemd journal:

use log::{info, warn, error, LevelFilter};
use systemd_journal_logger::JournalLog;

JournalLog::new().unwrap().install().unwrap();
log::set_max_level(LevelFilter::Info);

info!("hello log");
warn!("warning");
error!("oops");

See JournalLog for details about the logging format.

§Journal connections

In a service you can use connected_to_journal to check whether the standard output or error stream of the current process is directly connected to the systemd journal (the default for services started by systemd) and fall back to logging to standard error if that’s not the case. Take a look at the systemd_service.rs example for details.

use log::{info, warn, error, LevelFilter};
use systemd_journal_logger::JournalLog;

JournalLog::new()
    .unwrap()
    .with_extra_fields(vec![("VERSION", env!("CARGO_PKG_VERSION"))])
    .with_syslog_identifier("foo".to_string())
    .install().unwrap();
log::set_max_level(LevelFilter::Info);

info!("this message has an extra VERSION field in the journal");

You can display these extra fields with journalctl --output=verbose and extract them with any of the structured output formats of journalctl, e.g. journalctl --output=json.

Structs§

JournalLog
A systemd journal logger.

Functions§

connected_to_journal
Whether the current process is directly connected to the systemd journal.
current_exe_identifier
Create a syslog identifier from the current executable.