#systemd #journal #logging #log-messages

systemd-journal-logger

Systemd journal logger for the log facade

13 releases (4 stable)

2.1.1 Nov 15, 2023
1.0.0 Apr 29, 2023
0.7.0 Dec 23, 2022
0.5.1 Oct 15, 2022
0.3.0 Jun 7, 2021

#73 in Debugging

Download history 1381/week @ 2023-12-17 912/week @ 2023-12-24 856/week @ 2023-12-31 1360/week @ 2024-01-07 1441/week @ 2024-01-14 1329/week @ 2024-01-21 1316/week @ 2024-01-28 1960/week @ 2024-02-04 1546/week @ 2024-02-11 1439/week @ 2024-02-18 1722/week @ 2024-02-25 1764/week @ 2024-03-03 1786/week @ 2024-03-10 1809/week @ 2024-03-17 1469/week @ 2024-03-24 1714/week @ 2024-03-31

6,861 downloads per month
Used in 6 crates

MIT/Apache

29KB
324 lines

systemd-journal-logger

A pure Rust log logger for the systemd journal.

Usage

$ cargo add systemd-journal-logger

Then initialize the logger at the start of main:

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");

You can also add additional fields to every log message, such as the version of your executable:

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");

These extra fields appear in the output of journalctl --output=verbose or in any of the JSON output formats of journalctl.

See systemd_service.rs for a simple example of logging in a systemd service which automatically falls back to a different logger if not started through systemd.

Both loggers use mostly the same fields and priorities as this implementation.

Minimum Supported Rust Version

The MSRV used in this repo is best effort only and may be bumped at any time, even in patch releases.

License

Either MIT or Apache 2.0, at your option.

Dependencies

~2–12MB
~117K SLoC