I'm using the logging module in Python and I would like it to create a new logfile each time my application is started. The older logfiles shoud be rotated (eg: logfile.txt -> logfile1.txt, etc).
I already found this:
http://docs.python.org/library/logging.html
BaseRotatingHandler is the base class for handlers that rotate log files at a certain point. It is not meant to be instantiated directly. Instead, use RotatingFileHandler or TimedRotatingFileHandler.
The RotatingFileHandler does a rollover at a predetermined size and the TimedRotatingFileHandler does a rollover based on the product of when and interval. Both are not what I want, I want the rotation to happen immediately when my application starts.
The TimedRotatingFileHandler class, located in the logging. handlers module, supports rotation of disk log files at certain timed intervals. class logging.handlers. TimedRotatingFileHandler (filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None, errors=None)
Each file should be rotated weekly. The log rotation job runs nightly, though, so this can be changed to daily for a specific log file if desired. The three commands that specify how often rotation should take place are daily, weekly and monthly. Keep four sets of log files.
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('\n---------\nLog closed on %s.\n---------\n' % time.asctime())
# Roll over on application start
my_logger.handlers[0].doRollover()
# Add timestamp
my_logger.debug('\n---------\nLog started on %s.\n---------\n' % 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 '\n'.join(logfiles)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With