Skip to content

Commit

Permalink
Allow for custom process names when using the syslog logger
Browse files Browse the repository at this point in the history
  • Loading branch information
julienjpk-withings committed Oct 31, 2024
1 parent 0bb276b commit 40a0a5b
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/writers/syslog/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct SyslogWriterBuilder {
syslog_connection: SyslogConnection,
syslog_line_header: SyslogLineHeader,
syslog_facility: SyslogFacility,
process: Option<String>,
determine_severity: LevelToSyslogSeverity,
max_log_level: log::LevelFilter,
format: FormatFunction,
Expand All @@ -28,12 +29,20 @@ impl SyslogWriterBuilder {
syslog_connection: syslog,
syslog_line_header,
syslog_facility,
process: None,
determine_severity: default_mapping,
max_log_level: log::LevelFilter::Warn,
format: syslog_default_format,
}
}

/// Specify a custom process name, or unsets it to revert back to name inference
#[must_use]
pub fn process(mut self, name: Option<&str>) -> Self {
self.process = name.map(|s| s.into());
self
}

/// Use the given function to map the rust log levels to the syslog severities.
/// By default a trivial mapping is used, which should be good enough in most cases.
#[must_use]
Expand Down Expand Up @@ -73,7 +82,7 @@ impl SyslogWriterBuilder {
pub fn build(self) -> IoResult<Box<SyslogWriter>> {
Ok(Box::new(SyslogWriter::new(
std::process::id(),
std::env::args().next().ok_or_else(|| {
self.process.or(std::env::args().next()).ok_or_else(|| {
IoError::new(
ErrorKind::Other,
"Can't infer app name as no env args are present".to_owned(),
Expand Down

0 comments on commit 40a0a5b

Please sign in to comment.