Skip to content

Commit

Permalink
Don't set hostname with the unix backend
Browse files Browse the repository at this point in the history
The UNIX socket specific initialization functions `init_unix()` and
`init_unix_custom()` take care of not setting the hostname in the
header. This is based on the assumption that a syslog relay will
set the appropriate hostname (see PR #7).

The generic initialization function `init()` that supports UNIX, UDP
and TCP sockets does set the hostname unconditionally though. Users
of this API end up with an extra hostname being added to their logs,
as seen with virtiofsd for example :

Jan 12 17:03:04 myhostname virtiofsd[70727]: myhostname virtiofsd[70725]: Waiting for vhost-user socket connection...

Only set the hostname for UDP and TCP.

Signed-off-by: Greg Kurz <groug@kaod.org>
  • Loading branch information
gkurz committed Jan 15, 2024
1 parent ee861eb commit eaa3940
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,21 +483,23 @@ pub fn init(
) -> Result<()> {
let (process_name, pid) = get_process_info()?;
let process = application_name.map(From::from).unwrap_or(process_name);
let formatter = Formatter3164 {
let mut formatter = Formatter3164 {
facility,
hostname: get_hostname().ok(),
hostname: None,
process,
pid,
};

let backend = unix(formatter.clone())
.map(|logger: Logger<LoggerBackend, Formatter3164>| logger.backend)
.or_else(|_| {
TcpStream::connect(("127.0.0.1", 601)).map(|s| LoggerBackend::Tcp(BufWriter::new(s)))
})
.or_else(|_| {
let udp_addr = "127.0.0.1:514".parse().unwrap();
UdpSocket::bind(("127.0.0.1", 0)).map(|s| LoggerBackend::Udp(s, udp_addr))
formatter.hostname = get_hostname().ok();
TcpStream::connect(("127.0.0.1", 601))
.map(|s| LoggerBackend::Tcp(BufWriter::new(s)))
.or_else(|_| {
let udp_addr = "127.0.0.1:514".parse().unwrap();
UdpSocket::bind(("127.0.0.1", 0)).map(|s| LoggerBackend::Udp(s, udp_addr))
})
})?;
log::set_boxed_logger(Box::new(BasicLogger::new(Logger { formatter, backend })))
.chain_err(|| ErrorKind::Initialization)?;
Expand Down

0 comments on commit eaa3940

Please sign in to comment.