1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#[cfg(not(windows))]
// This is necessary because `fern` depends on both version 3, 4 and 6
use syslog7 as syslog;
#[cfg(not(windows))]
use log::{debug, info, warn};
#[cfg(not(windows))]
fn setup_logging() -> Result<(), Box<dyn std::error::Error>> {
let syslog_fmt = syslog::Formatter3164 {
facility: syslog::Facility::LOG_USER,
hostname: None,
process: "fern-syslog-example".into(),
pid: 0,
};
fern::Dispatch::new()
// by default only accept warning messages so as not to spam
.level(log::LevelFilter::Warn)
// but accept Info if we explicitly mention it
.level_for("explicit-syslog", log::LevelFilter::Info)
.chain(syslog::unix(syslog_fmt)?)
.apply()?;
Ok(())
}
#[cfg(not(windows))]
fn main() {
setup_logging().expect("failed to initialize logging.");
// None of this will be shown in the syslog:
for i in 0..5 {
info!("executing section: {}", i);
debug!("section {} 1/4 complete.", i);
debug!("section {} 1/2 complete.", i);
debug!("section {} 3/4 complete.", i);
info!("section {} completed!", i);
}
// these two *will* show.
info!(target: "explicit-syslog", "hello to the syslog! this is rust.");
warn!("AHHH something's on fire.");
}
#[cfg(windows)]
fn main() {
panic!("this example does not work on Windows.");
}
|