I might be enough to use RotatingFileHandler
without maxBytes
, then call doRollover()
on application start.
Yup, seems to work fine. The code below will create a new log file on each application run, with added timestamps for log start and close times. Running it will print the list of available log files. You can inspect them to check correct behavior. Adapted from the Python docs example:
import os
import glob
import logging
import logging.handlers
import time
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)
# Check if log exists and should therefore be rolled
needRoll = os.path.isfile(LOG_FILENAME)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, backupCount=50)
my_logger.addHandler(handler)
# This is a stale log, so roll it
if needRoll:
# Add timestamp
my_logger.debug('
---------
Log closed on %s.
---------
' % time.asctime())
# Roll over on application start
my_logger.handlers[0].doRollover()
# Add timestamp
my_logger.debug('
---------
Log started on %s.
---------
' % time.asctime())
# Log some messages
for i in xrange(20):
my_logger.debug('i = %d' % i)
# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)
print '
'.join(logfiles)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…