Skip to content

Instantly share code, notes, and snippets.

@TheWaWaR
Created June 17, 2014 01:30
Show Gist options
  • Save TheWaWaR/5dd7f1aeb46acd6b172b to your computer and use it in GitHub Desktop.
Save TheWaWaR/5dd7f1aeb46acd6b172b to your computer and use it in GitHub Desktop.
Log Rotate
import glob
import logging
import logging.handlers
LOG_FILENAME = 'logging_rotatingfile_example.out'
# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
LOG_FILENAME, maxBytes=20, backupCount=5)
my_logger.addHandler(handler)
# Log some messages
for i in range(40):
my_logger.debug('i = %d' % i)
# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)
for filename in logfiles:
print(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment