#logging #systemd #journal #log-messages

systemd-journal-logger

Systemd journal logger for the log facade

14 releases (5 stable)

2.2.0 Oct 17, 2024
2.1.1 Nov 15, 2023
2.1.0 Oct 19, 2023
1.0.0 Apr 29, 2023
0.3.0 Jun 7, 2021

#61 in Debugging

Download history 2371/week @ 2024-08-21 2837/week @ 2024-08-28 3641/week @ 2024-09-04 3569/week @ 2024-09-11 3119/week @ 2024-09-18 3435/week @ 2024-09-25 3662/week @ 2024-10-02 3916/week @ 2024-10-09 5162/week @ 2024-10-16 4331/week @ 2024-10-23 4908/week @ 2024-10-30 4379/week @ 2024-11-06 5083/week @ 2024-11-13 5487/week @ 2024-11-20 4524/week @ 2024-11-27 3624/week @ 2024-12-04

19,509 downloads per month
Used in 11 crates (10 directly)

MIT/Apache

30KB
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

~1.5–9.5MB
~110K SLoC