Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Writing to syslog with Python's logging library
import logging
import logging.handlers
log = logging.getLogger(__name__)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
formatter = logging.Formatter('%(module)s.%(funcName)s: %(message)s')
def hello():
log.debug('this is debug')
log.critical('this is critical')
if __name__ == '__main__':
matt@Jenkins:~$ tail -n 2 /var/log/syslog
Feb 9 11:56:19 Jenkins logtest.hello: this is debug
Feb 9 11:56:19 Jenkins logtest.hello: this is critical
Copy link

khurshid-alam commented May 19, 2017

@sweenzor How to redirect stderr to syslog with above script?

Copy link

sprive commented Sep 29, 2017

@khurshid-alam I think you misunderstand the intent of the code. This demonstrates the origination of NEW messages, written to syslog.

I think what you are really asking is "how can take stderr from an existing script, and redirect that to syslog?" If so: (or some variant of that for your use case...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment