Created
September 24, 2018 17:07
-
-
Save tazjin/720fd1479f6ab7d0a8b02c8dac556ec7 to your computer and use it in GitHub Desktop.
journald priority decorator for log4rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Implementation of a log-encoder decorator that can prepend | |
/// journald log priorities to any given `log4rs`-encoder. | |
#[derive(Debug)] | |
struct JournaldDecorator<E: Encode>(E); | |
/// Helper function to map from `log`-crate log levels to journald log | |
/// priorites. Please see `sd-daemon(3)` for detailed information on | |
/// these levels. | |
fn journald_priority(level: &Level) -> &'static str { | |
match level { | |
Level::Error => "<3>", | |
Level::Warn => "<4>", | |
Level::Info => "<6>", | |
// journald & stackdriver do not differentiate between 'Debug' | |
// and higher levels, so debug and trace are both mapped to | |
// `SD_DEBUG`. | |
Level::Debug => "<7>", | |
Level::Trace => "<7>", | |
} | |
} | |
impl <E: Encode> Encode for JournaldDecorator<E> { | |
fn encode(&self, w: &mut Write, record: &Record) | |
-> Result<(), Box<Error + Sync + Send>> { | |
// First write the journald priority, then delegate to the | |
// decorated encoder. | |
write!(w, "{}", journald_priority(&record.level()))?; | |
self.0.encode(w, record) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment