pythonloggingsyslogpython-logging

How to configure logging to syslog in Python?


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?


Solution

  • Change the line to this:

    handler = SysLogHandler(address='/dev/log')
    

    This works for me

    import logging
    import logging.handlers
    
    my_logger = logging.getLogger('MyLogger')
    my_logger.setLevel(logging.DEBUG)
    
    handler = logging.handlers.SysLogHandler(address = '/dev/log')
    
    my_logger.addHandler(handler)
    
    my_logger.debug('this is debug')
    my_logger.critical('this is critical')