Search code examples
linuxautomated-testssyslogrsyslog

How to test that a message has been sent to syslog


A linux program is sending logs to syslog.

To test this program automatically, I need to check that after some action has been done a message has been sent to syslog.

Unfortunately, the location of the syslog file is different depending on syslog configuration (/var/log/messages on one computer, /var/log/syslog on another computer and possibly other paths).

How can I know the current path of the syslog file programmatically ?

If I know the log path, how to detect the log message ? I could grep the log file and check the return value of the grep command but:

  • how to deal with log rotation possibly happening at around the same time ?
  • how to deal with the fact that the test may need to be repeated and work again (delete the log file before the test ?)

This test should also work with the busybox version of syslog.


Solution

  • Probably the best method is to interpose a shared library (using LD_PRELOAD) in which you provide your own verifiable implementations of the syslog functions for the purpose of your unit tests. You don't have to let the messages go to the installed syslog daemon at all (unless you want to).