emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
315 stars 55 forks source link

SyslogWriter needs to append LF to buffer when try_tcp is used for connection #121

Closed griffdp closed 2 years ago

griffdp commented 2 years ago

It appears that rsyslog by default uses the LF character as the delimiter for messages received via plaintext TCP.

With flexilogger v0.23.3, log messages sent via UDP result in correctly formatted (1 log message per line) log files. Sending the same log messages via TCP results in no line separation between log messages in the log file:

    Logger::try_with_str("debug")?
        .log_to_writer(SyslogWriter::try_new(
            UserLevel,
            None,
            LevelFilter::Off,
            String::from("my-app"),
            Syslog::try_tcp("127.0.0.1:1514")?,
        )?)
        .start()?;

    info!("my-app started");
    info!("my-app doing something");
    info!("my-app idle");

Temporarily amending the Rfc5424 format string for the write function of LogWriter for SyslogWriter in src/writers/syslog_writer.rs to append a LF character produces correctly separated log lines in the log file:

"<{pri}>{version} {timestamp} {hostname} {appname} {procid} {msgid} - {msg}", to "<{pri}>{version} {timestamp} {hostname} {appname} {procid} {msgid} - {msg}\n",

This would need to be done only when the syslog connection is TCP.