A pure Rust [log] logger for the systemd journal.
This logger used libsystemd and has no dependency on the libsystemd C library.
toml
[dependencies]
log = "^0.4"
systemd-journal-logger = "1.0.0"
Then initialize the logger at the start of main
:
```rust use log::{info, LevelFilter};
fn main() { systemdjournallogger::init(); log::setmaxlevel(LevelFilter::Info);
info!("Hello systemd journal");
} ```
You can also add additional fields to every log message, such as the version of your executable:
```rust use log::{info, LevelFilter};
fn main() { let version = env!("CARGOPKGVERSION"); systemdjournallogger::initwithextrafields(vec![("VERSION", version)]).unwrap(); log::setmax_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.
libsystemd
C library.libsystemd
C library.Both loggers use mostly the same fields and priorities as this implementation.
Either MIT or Apache 2.0, at your option.