21

I'm looking at how to log to syslog from within my Python app, and I found there are two ways of doing it:

  1. Using syslog.syslog() routines
  2. Using the logger module SysLogHandler

Which is the best option to use, advantages/disadvantages of each one, etc, because I really don't know which one should I use.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Juancho
  • 629
  • 7
  • 17
  • 1
    Please consider this blog post... I believe it provides many valuable insight and POVs. http://www.aminus.org/blogs/index.php/2008/07/03/writing-high-efficiency-large-python-sys-1?blog=2 –  Apr 03 '14 at 17:35
  • @David that is a very old post; is it still relevant? – tshepang Jul 02 '14 at 11:52

3 Answers3

11

syslog.syslog() can only be used to send messages to the local syslogd. SysLogHandler can be used as part of a comprehensive, configurable logging subsystem, and can log to remote machines.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 4
    Well, that's not completely true, you can configure your syslog to send what it receives to a remote syslog server too. The advantage of this is that not only the messages of the python application are sent to the remote server but every message of the system. – Juancho Dec 27 '12 at 17:22
  • 1
    But that configuration is done independent of the Python program. My answer still stands. – Ignacio Vazquez-Abrams Dec 27 '12 at 17:26
  • 2
    Yees, but what I meant is that with syslog.syslog() you can also send messages to a remote syslog server through the local syslog server, so it isn't nessearely an advantage of SysLogHandler over syslog.syslog() – Juancho Dec 27 '12 at 20:26
  • I'm with Juancho: I'm setting up an RPi to do real-time sensing, and I want *all* system messages (not just Python's messages) to be sent to a remote logging service. So in this case using `syslog.syslog()` and configuring the syslog service is ultimately more comprehensive. – fearless_fool Sep 05 '14 at 22:20
5

The logging module is a more comprehensive solution that can potentially handle all of your log messages, and is very flexible. For instance, you can setup multiple handers for your logger and each can be set to log at a different level. You can have a SysLogHandler for sending errors to syslog, and a FileHandler for debugging logs, and an SMTPHandler to email the really critical messages to ops. You can also define a hierarchy of loggers within your modules, and each one has its own level so you can enable/disable messages from specific modules, such as:

import logging
logger = logging.getLogger('package.stable_module')
logger.setLevel(logging.WARNING)

And in another module:

import logging
logger = logging.getLogger('package.buggy_module')
logger.setLevel(logging.DEBUG)

The log messages in both of the these modules will be sent, depending on the level, to the 'package' logger and ultimately to the handlers you've defined. You can also add handlers directly to the module loggers, and so on. If you've followed along this far and are still interested, then I recommend jumping to the logging tutorial for more details.

tshepang
  • 12,111
  • 21
  • 91
  • 136
rread
  • 149
  • 3
0

So far, there is a disadvantage in logging.handlers.SysLogHander which is not mentioned yet. That is I can't set options like LOG_ODELAY or LOG_NOWAIT or LOG_PID. On the other hands, LOG_CONS and LOG_PERROR can be achieved with adding more handlers, and LOG_NDELAY is already set by default, because the connection opens when the handler is instantiated.

soundlake
  • 321
  • 4
  • 10