Skip to content

Instantly share code, notes, and snippets.

@vanzaj
Last active December 18, 2015 19:08
Show Gist options
  • Save vanzaj/5830385 to your computer and use it in GitHub Desktop.
Save vanzaj/5830385 to your computer and use it in GitHub Desktop.
logging example with syslog like formatting
import logging
import logging.handlers
LOG_FILE = 'test.log'
#logging.basicConfig(filename=LOG_FILE,
#level=logging.DEBUG,
#)
#logging.debug('this should be in the log file')
log = logging.getLogger(__file__)
log.setLevel(logging.DEBUG)
fmt = '%(asctime)s %(name)s: [%(levelname)s] %(message)s'
formatter = logging.Formatter(fmt, datefmt='%b %d %H:%M:%S')
handler = logging.handlers.RotatingFileHandler(
LOG_FILE, maxBytes=300, backupCount=3)
handler.setFormatter(formatter)
log.addHandler(handler)
for i in range(50):
msg = 'i = %d' % (i)
if i % 2:
log.debug(msg)
else:
log.warning(msg)
# log.debug('debug message')
# log.info('info message')
# log.warn('warn message')
# log.error('error message')
# log.critical('critical message')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment