Search code examples
pythonlinuxpython-2.7loggingsyslog

logging.handlers.SysLogHandler omits leading whitespace in log message


I am writing an application that logs both to STDOUT and to syslog using the logging module and a logging.StreamHandler() and logging.handlers.SysLogHandler(address='/dev/log') handler respectively.

The formatter is set as

    handler.setFormatter(logging.Formatter('%(message)s'))

in both cases.

It now appears that leading whitespace (which I use to indent certain log messages for improved readability) is shown in STDOUT but not in syslog. Is there a way for the SysLogHandler to preserve the whitespace like the StreamHandler seems to be doing?


Solution

  • syslog daemons modify the data that's sent to them before display. For example, the data actually sent to the syslog daemon has a header as mandated by RFC 5424 which you don't see in the log you see in a log viewer. So the formatting of the message at SysLogHandler level won't guarantee a completely faithful representation (at least when it comes to leading spaces).