Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syslog facility #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions tracing-journald/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct Subscriber {
socket: UnixDatagram,
field_prefix: Option<String>,
syslog_identifier: String,
syslog_facility: Option<libc::c_int>,
syslog_facility: Option<u8>,
}

#[cfg(unix)]
Expand Down Expand Up @@ -169,20 +169,21 @@ impl Subscriber {
/// and allows filtering log messages by syslog facility with `journalctl
/// --facility`.
///
/// See [Journal Fields](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html)
/// See [Syslog Message Facilities](https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1),
/// [Journal Fields](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html)
/// and [journalctl](https://www.freedesktop.org/software/systemd/man/journalctl.html)
/// for more information.
///
/// If not set, this field is left out.
pub fn with_syslog_facility(mut self, facility: libc::c_int) -> Self {
pub fn with_syslog_facility(mut self, facility: u8) -> Self {
self.syslog_facility = Some(facility);
self
}

/// Returns the syslog facility in use.
///
/// A syslog facility can be set using [`Subscriber::with_syslog_facility`].
pub fn syslog_facility(&self) -> Option<libc::c_int> {
pub fn syslog_facility(&self) -> Option<u8> {
self.syslog_facility
}

Expand Down Expand Up @@ -289,9 +290,8 @@ where
write!(buf, "{}", self.syslog_identifier).unwrap()
});
if let Some(facility) = self.syslog_facility {
// libc definitions are bitshifted left by 3, but journald uses
// values without bitshift.
writeln!(buf, "SYSLOG_FACILITY={}", facility >> 3).unwrap();
// Text format is safe as a u8 can't possibly contain anything funny
writeln!(buf, "SYSLOG_FACILITY={}", facility).unwrap();
}

event.record(&mut EventVisitor::new(
Expand Down
4 changes: 3 additions & 1 deletion tracing-journald/tests/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ fn simple_metadata() {
let sub = Subscriber::new()
.unwrap()
.with_field_prefix(None)
.with_syslog_identifier("test_ident".to_string());
.with_syslog_identifier("test_ident".to_string())
.with_syslog_facility(18);
with_journald_subscriber(sub, || {
info!(test.name = "simple_metadata", "Hello World");

Expand All @@ -232,6 +233,7 @@ fn simple_metadata() {
assert_eq!(message["PRIORITY"], "5");
assert_eq!(message["TARGET"], "journal");
assert_eq!(message["SYSLOG_IDENTIFIER"], "test_ident");
assert_eq!(message["SYSLOG_FACILITY"], "18");
assert!(message["CODE_FILE"].as_text().is_some());
assert!(message["CODE_LINE"].as_text().is_some());
});
Expand Down