Skip to content

Commit 37ac919

Browse files
committed
refactor: addapt syslog example to auto-detection
1 parent 0d92265 commit 37ac919

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

examples/syslog_friendly_format.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1-
use std::io::Write;
2-
31
fn main() {
4-
match std::env::var("RUST_LOG_STYLE") {
5-
Ok(s) if s == "SYSTEMD" => env_logger::builder()
6-
.build_with_format_fn(|buf, record| {
7-
writeln!(
8-
buf,
9-
"<{}>{}: {}",
10-
match record.level() {
11-
log::Level::Error => 3,
12-
log::Level::Warn => 4,
13-
log::Level::Info => 6,
14-
log::Level::Debug => 7,
15-
log::Level::Trace => 7,
16-
},
17-
record.target(),
18-
record.args()
19-
)
20-
})
21-
.try_init()
22-
.unwrap(),
23-
_ => env_logger::init(),
24-
};
2+
env_logger::builder()
3+
.parse_default_env()
4+
// While journald-logging is auto-detected, but you can manually override it.
5+
// Especially useful if you are using a different logging system.
6+
.format_syslog(true)
7+
.init();
8+
9+
// Prints in a human readable way if run interactively,
10+
// and in a syslog-compatible way if run as a systemd service.
11+
log::info!("we are logging");
2512
}

0 commit comments

Comments
 (0)