Search code examples
loggingcherrypysyslog

CherryPy: Log access and error events to syslog


I have created a REST interface using CherryPy. I prefer using syslog, instead of individual files to log messages for each of my applications. So, currently, the application uses the Python logging facility, logging to syslog via a /dev/log handler.

I would like to be able to log all of the access attempts at the debug level, and all of the errors at the error level, which you can do by specifying the cherrypy.log.access_file and cherrypy.log.error_file file-paths.

Is there a way to direct the cherrypy.log.error_file to syslog? I would like to avoid logging directly to /var/log/syslog and changing the permissions on that file, if possible.

Thanks,

Zach


Solution

  • I see how to do this:

    I have my logging defined in my configuration file using Python's logging facility. I have initialized a logger for general purposes based on that.

    Using the documentation here: http://docs.cherrypy.org/stable/refman/_cplogging.html, I added my syslog handler to the cherrypy.log handlers.

    Here is my code:

    from cherrypy import log
    
    h = logger.handlers[0]
    h.setLevel(logging.DEBUG)
    log.access_log.addHandler(h)
    log.error_log.addHandler(h)
    

    It's super simple.