Skip to content

allow setting a custom process name #12

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 24 additions & 11 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ fn log_with_level(level: slog::Level, mut io: std::sync::MutexGuard<Box<SysLogge
///
/// This follows ``get_process_info()`` in the syslog crate to some extent
/// which is private.
fn syslog_format3164(facility: syslog::Facility, hostname: Option<String>) -> syslog::Formatter3164 {
let path = std::env::current_exe()
.unwrap_or_else(|_| PathBuf::new());
let process = path.file_name()
.map(|file| file.to_string_lossy().into_owned())
.unwrap_or_else(|| String::new());
fn syslog_format3164(facility: syslog::Facility, hostname: Option<String>, process_name: Option<String>) -> syslog::Formatter3164 {
let process = process_name.unwrap_or_else(|| {
let path = std::env::current_exe()
.unwrap_or_else(|_| PathBuf::new());
let filename = path.file_name()
.map(|file| file.to_string_lossy().into_owned())
.unwrap_or_else(|| String::new());
filename
});


syslog::Formatter3164 {
facility,
Expand Down Expand Up @@ -233,13 +237,15 @@ enum SyslogKind {
pub struct SyslogBuilder {
facility: Option<syslog::Facility>,
level: Level,
process: Option<String>,
logkind: Option<SyslogKind>,
}
impl Default for SyslogBuilder {
fn default() -> Self {
Self {
facility: None,
level: Level::Trace,
process: None,
logkind: None,
}
}
Expand All @@ -266,6 +272,13 @@ impl SyslogBuilder {
s
}

/// Process name inside Syslog
pub fn process(self, name: String) -> Self {
let mut s = self;
s.process = Some(name);
s
}

/// Remote UDP syslogging
pub fn udp<S: AsRef<str>>(self, local: SocketAddr, host: SocketAddr, hostname: S) -> Self {
let mut s = self;
Expand Down Expand Up @@ -316,19 +329,19 @@ impl SyslogBuilder {
};
let log = match logkind {
SyslogKind::Unix { path } => {
let format = syslog_format3164(facility, None);
let format = syslog_format3164(facility, None, self.process);
syslog::unix_custom(format, path).map_err(handle_syslog_error)?
}
SyslogKind::Udp {
local,
host,
hostname,
} => {
let format = syslog_format3164(facility, Some(hostname));
let format = syslog_format3164(facility, Some(hostname), self.process);
syslog::udp(format, local, host).map_err(handle_syslog_error)?
},
SyslogKind::Tcp { server, hostname } => {
let format = syslog_format3164(facility, Some(hostname));
let format = syslog_format3164(facility, Some(hostname), self.process);
syslog::tcp(format, server).map_err(handle_syslog_error)?
},
};
Expand All @@ -338,7 +351,7 @@ impl SyslogBuilder {

/// `Streamer` to Unix syslog using RFC 3164 format
pub fn unix_3164_with_level(facility: syslog::Facility, level: Level) -> io::Result<Streamer3164> {
let format = syslog_format3164(facility, None);
let format = syslog_format3164(facility, None, None);
syslog::unix(format)
.map(Box::new)
.map(|logger| Streamer3164::new_with_level(logger, level))
Expand All @@ -347,7 +360,7 @@ pub fn unix_3164_with_level(facility: syslog::Facility, level: Level) -> io::Res

/// `Streamer` to Unix syslog using RFC 3164 format
pub fn unix_3164(facility: syslog::Facility) -> io::Result<Streamer3164> {
let format = syslog_format3164(facility, None);
let format = syslog_format3164(facility, None, None);
syslog::unix(format)
.map(Box::new)
.map(Streamer3164::new)
Expand Down