Skip to content

Instantly share code, notes, and snippets.

@bobhancockgist
Created June 6, 2013 22:08
Show Gist options
  • Save bobhancockgist/5725397 to your computer and use it in GitHub Desktop.
Save bobhancockgist/5725397 to your computer and use it in GitHub Desktop.
Get log handle for log that rotates on size.
import logging
def setup_log_size_rotating(log_filename,
logname='defaultlog',
max_size_in_bytes=(1024 * 1000000),
debug=False,
backups = 32):
""" The file will rollover when it approaches maxBytes.
Our default size is 1MB
"""
log = logging.getLogger(logname)
lh = logging.handlers.RotatingFileHandler(log_filename,
maxBytes=max_size_in_bytes,
backupCount=backups,
encoding='utf-8')
if debug:
log.setLevel(logging.DEBUG)
lh.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
lh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s|%(name)s|%(levelname)s|%(message)s')
lh.setFormatter(formatter)
log.addHandler(lh)
return log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment