Skip to content

Instantly share code, notes, and snippets.

@codyaray
Last active January 17, 2017 16:29
Show Gist options
  • Save codyaray/fdd14e419bf5622045920d7382739d0e to your computer and use it in GitHub Desktop.
Save codyaray/fdd14e419bf5622045920d7382739d0e to your computer and use it in GitHub Desktop.
from myhandlers import EnhancedRotatingFileHandler
import time
import logging
def demo():
log_filename = 'log_rotate.log'
logger = logging.getLogger('MyLogger')
logger.setLevel(logging.DEBUG)
handler = EnhancedRotatingFileHandler(
log_filename, maxBytes=10,
when='m',interval=1,
# encoding='bz2', # uncomment for bz2 compression
)
logger.addHandler(handler)
for i in range(1000):
time.sleep(0.1)
logger.debug('i=%d' % i)
demo()
# Output:
# log_rotate.log i=999
# log_rotate.log.2017_01_17_09-33 i=175
# log_rotate.log.2017_01_17_09-34 i=749
# log_rotate.log.2017_01_17_09-35 i=998
#
# Expected:
# Multiple files containing all i=0 to 999 (since backupCount=0 by default)
# Not missing any of the logging data.
# Even with backupCount=5, I'd expect the files to be the _most recent_ 5 data points (i=995...999, not i=175,749,998,999)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment