Skip to content

Instantly share code, notes, and snippets.

@biggers
Last active May 31, 2023 20:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save biggers/7da64042c86f7405f488bcc9efe040f8 to your computer and use it in GitHub Desktop.
Save biggers/7da64042c86f7405f488bcc9efe040f8 to your computer and use it in GitHub Desktop.
Python dictConfig logging example using SyslogHandler - modified from Stackoverflow answer
import logging
import sys
from logging import config
# REF, modified from:
# https://stackoverflow.com/a/19367225
if sys.platform == "darwin":
address = '/var/run/syslog'
facility = 'local1'
elif sys.platform == 'linux':
address = '/dev/log'
else:
address = ('localhost', 514)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format':
'%(asctime)s %(name)s %(levelname)-10s %(message)s'
},
},
'handlers': {
'stderr': {
'class': 'logging.StreamHandler',
'stream': sys.stderr,
'formatter': 'verbose',
},
'sys-logger0': {
'class': 'logging.handlers.SysLogHandler',
'address': address,
'facility': "local0",
'formatter': 'verbose',
},
},
'loggers': {
'sre_mon': {
'handlers': ['sys-logger0', 'stderr'],
'level': logging.DEBUG,
'propagate': True,
},
}
}
def main():
config.dictConfig(LOGGING)
logger = logging.getLogger('sre_mon')
logger.debug("debugging...")
logger.info("getting some info")
logger.warning("Warning, Will Robinson!")
logger.error("Error!")
logger.critical("It's Critical")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment