Created
June 6, 2013 22:08
-
-
Save bobhancockgist/5725397 to your computer and use it in GitHub Desktop.
Get log handle for log that rotates on size.
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 | |
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