Does anyone have any experience combining the tracing crate with the syslog crate? I am in a situation where I would like to print to the system log, and I have successfully been using the syslog crate like so:
use syslog::{Facility, Formatter3164};
let formatter = Formatter3164 {
facility: Facility::LOG_USER,
hostname: None,
process: "appname".into(),
pid: std::process::id() as i32,
};
let mut writer = syslog::unix(formatter).expect("Should get syslog");
writer.info("Hello, world!").expect("Success");
I think maybe I should use the with_writer method on the FmtSubscriber builder, but can't quite figure out what to put in there
use tracing::{info, Level};
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_max_level(Level::TRACE)
.with_writer(/* What to do here? */)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("setting default subscriber failed");
Hello! I'm a tracing contributor, but I'll say that I've never used syslog before, so I apologize if I make incorrect assumptions around syslog. The bad news is that I'm unaware of any out-of-the-box crates that work with tracing and syslog. The less bad news is that it's not too painful to implement your own subscriber for syslog, if you've got the time.
But first, a detour about fmt::SubscriberBuilder: fmt::SubscriberBuilder::with_writer takes a type implementing the MakeWriter trait, which tells how the fmt::Subscriberwhere to write the formatted events it has processed.
If I understand syslog correctly, there are two components: a logger based on UDP, TCP, or Unix daemon sockets and a format that complies with either RFC 3164 or RFC 5424. tracing_subscriber::fmt::FmtSubscriber expects to the former to be provided through a MakeWriter, while for the syslog-specifc formats, FmtSubscriber expects to receive an RFC 3164/5424-compliant formatter defined through the FormatFields and FormatEvent traits.
However, since we're looking at changing the formatter traits in fmt::Subscriber in the next breaking release of tracing-subscriber, I suggest creating your own subscriber/Layer (which is a composable version of the Subscriber trait) that can target syslog directly. Looking over the implementation of the syslog crate, I that you'd be able to build your syslog Subscriber/Layer atop of tracing by borrowing some of the code from the syslog crate and this layer that I wrote.