#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

#72 in Debugging

Download history 1762/week @ 2023-11-29 1967/week @ 2023-12-06 1262/week @ 2023-12-13 1108/week @ 2023-12-20 808/week @ 2023-12-27 1230/week @ 2024-01-03 1298/week @ 2024-01-10 1519/week @ 2024-01-17 1194/week @ 2024-01-24 1791/week @ 2024-01-31 1683/week @ 2024-02-07 1455/week @ 2024-02-14 1476/week @ 2024-02-21 1777/week @ 2024-02-28 1778/week @ 2024-03-06 1423/week @ 2024-03-13

6,768 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
~123K SLoC