I can't get my head around Python's logging
module. My needs are very simple: I just want to log everything to syslog. After reading documentation I came up with this simple test script:
import logging
import logging.handlers
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler()
my_logger.addHandler(handler)
my_logger.debug('this is debug')
my_logger.critical('this is critical')
But this script does not produce any log records in syslog. What's wrong?
Is your syslog.conf set up to handle facility=user?
You can set the facility used by the python logger with the facility argument, something like this:
Piecing things together from here and other places, this is what I came up with that works on unbuntu 12.04 and centOS6
Create an file in
/etc/rsyslog.d/
that ends in .conf and add the following textRestart
rsyslog
, reloading did NOT seem to work for the new log files. Maybe it only reloads existing conf files?Then you can use this test program to make sure it actually works.
I found the syslog module to make it quite easy to get the basic logging behavior you describe:
There are other things you could do, too, but even just the first two lines of that will get you what you've asked for as I understand it.
You can also add a file handler or rotating file handler to send your logs to a local file: http://docs.python.org/2/library/logging.handlers.html