Created
February 9, 2012 19:59
-
-
Save sweenzor/1782457 to your computer and use it in GitHub Desktop.
Writing to syslog with Python's logging library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import logging.handlers | |
log = logging.getLogger(__name__) | |
log.setLevel(logging.DEBUG) | |
handler = logging.handlers.SysLogHandler(address = '/dev/log') | |
formatter = logging.Formatter('%(module)s.%(funcName)s: %(message)s') | |
handler.setFormatter(formatter) | |
log.addHandler(handler) | |
def hello(): | |
log.debug('this is debug') | |
log.critical('this is critical') | |
if __name__ == '__main__': | |
hello() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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: http://urbanautomaton.com/blog/2014/09/09/redirecting-bash-script-output-to-syslog/ (or some variant of that for your use case...)