cihub / seelog

Seelog is a native Go logging library that provides flexible asynchronous dispatching, filtering, and formatting.
BSD 3-Clause "New" or "Revised" License
1.64k stars 244 forks source link

Add direct syslog support #142

Closed lwithers closed 6 years ago

lwithers commented 7 years ago

This pull request adds support for writing to syslog directly.

The motivation of this pull request is to provide an easy, consistent way to write to syslog. It's certainly possible to do this already, but it requires additional code (a custom formatter for the priority field) and additional configuration (a format for the syslog message, and the socket address for the syslog daemon). As this code and config needs to be copied+pasted between various projects, it is a tedious and error-prone process, and it would be better for seelog to provide the capability in a much simpler manner.

The minimal usage of the syslog support is a config as follows:

<seelog>
  <outputs>
    <syslog/>
  </outputs>
</seelog>

This will tell seelog to connect to the local syslog daemon (using the same logic as Go's syslog package, where it tries a few different Unix sockets). The <syslog> element's behaviour differs in one respect from the norm: it doesn't derive the format from its parent element, but rather uses the new format std:syslog-bsd-local (unless overridden with a formatid= attribute). Although it is a departure from the behaviour of the other elements, it does minimise the amount of configuration required to get going, which is my primary motivation here.

If writing to a remote syslog daemon is desired, then the existing <conn> receiver can be used along with a new predefined format:

<seelog>
  <outputs>
    <conn net="tcp" addr="sysloghost:5514" formatid="std:syslog-rfc5424"/>
    <!-- std:syslog-bsd is also provided -->
  </outputs>
</seelog>

The new formats are defined as follows:

A few new formatters were added to support this:

An example configuration where we want to override the facility and log level mapping:

<seelog>
  <outputs>
    <syslog formatid="my-syslog"/>
  </outputs>
  <formats>
    <format id="my-syslog" format="%SyslogPriority(auth,debug,info,notice,warning,err,alert)%Date(Jan _2 15:04:05) importantd[%PID]: %Msg%n" />
  </formats>
</seelog>
lwithers commented 7 years ago

So… any thoughts, anyone?