#logging #systemd #journal

systemd-journal-logger

Systemd journal logger for the log facade

6 releases (3 breaking)

0.5.0 Jan 28, 2022
0.4.1 Nov 20, 2021
0.4.0 Oct 28, 2021
0.3.1 Oct 6, 2021
0.1.0 May 28, 2021

#73 in Debugging

Download history 179/week @ 2022-01-29 408/week @ 2022-02-05 373/week @ 2022-02-12 313/week @ 2022-02-19 213/week @ 2022-02-26 153/week @ 2022-03-05 332/week @ 2022-03-12 204/week @ 2022-03-19 315/week @ 2022-03-26 388/week @ 2022-04-02 228/week @ 2022-04-09 212/week @ 2022-04-16 377/week @ 2022-04-23 468/week @ 2022-04-30 146/week @ 2022-05-07 156/week @ 2022-05-14

1,181 downloads per month
Used in 2 crates

MIT/Apache

21KB
222 lines

systemd-journal-logger

A pure Rust log logger for the systemd journal.

This logger used libsystemd and has no dependency on the libsystemd C library.

Usage

[dependencies]
log = "^0.4"
systemd-journal-logger = "0.5.0"

Then initialize the logger at the start of main:

use log::{info, LevelFilter};

fn main() {
    systemd_journal_logger::init();
    log::set_max_level(LevelFilter::Info);

    info!("Hello systemd journal");
}

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

use log::{info, LevelFilter};

fn main() {
    let version = env!("CARGO_PKG_VERSION");
    systemd_journal_logger::init_with_extra_fields(vec![("VERSION", version)]).unwrap();
    log::set_max_level(LevelFilter::Info);

    info!("Hello systemd 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.

Related projects

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

License

Either MIT or Apache 2.0, at your option.

Dependencies

~4MB
~85K SLoC