Skip to content

Instantly share code, notes, and snippets.

@wmw
Created September 11, 2013 17:52
Show Gist options
  • Save wmw/6527244 to your computer and use it in GitHub Desktop.
Save wmw/6527244 to your computer and use it in GitHub Desktop.
logging python stuff
#!/usr/bin/python
import logging
import logging.handlers
import json
#
# http://docs.python.org/2/howto/logging-cookbook.html#implementing-structured-logging
#
class StructuredMessage(object):
def __init__(self, message, **kwargs):
self.message = message
self.kwargs = kwargs
def __str__(self):
return '{} >>> {}'.format(self.message, json.dumps(self.kwargs))
#
# http://stackoverflow.com/questions/3968669/how-to-configure-logging-to-syslog-in-python
#
logging.basicConfig(level=logging.INFO, format='%(message)s')
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address='/dev/log')
my_logger.addHandler(handler)
my_logger.critical('Unstructured Log')
my_logger.debug( StructuredMessage('testItem', foo='bar', bar='baz', num=123, f=123.456) )
my_logger.debug( StructuredMessage('testItem2', foo='bar', bar='baz', num=123, f=123.456) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment