如果
RotatingFileHandler没有
maxBytes,我可能足够使用,然后调用
doRollover()应用程序启动。
是的,似乎工作正常。下面的代码将在每次运行应用程序时创建一个新的日志文件,并为日志的开始和关闭时间添加时间戳。运行它将打印可用日志文件的列表。您可以检查它们以检查正确的行为。改编自Python
docs示例:
import osimport globimport loggingimport logging.handlersimport timeLOG_FILENAME = 'logging_rotatingfile_example.out'# Set up a specific logger with our desired output levelmy_logger = logging.getLogger('MyLogger')my_logger.setLevel(logging.DEBUG)# Check if log exists and should therefore be rolledneedRoll = os.path.isfile(LOG_FILENAME)# Add the log message handler to the loggerhandler = logging.handlers.RotatingFileHandler(LOG_FILENAME, backupCount=50)my_logger.addHandler(handler)# This is a stale log, so roll itif 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 timestampmy_logger.debug('n---------nLog started on %s.n---------n' % time.asctime())# Log some messagesfor i in xrange(20): my_logger.debug('i = %d' % i)# See what files are createdlogfiles = glob.glob('%s*' % LOG_FILENAME)print 'n'.join(logfiles)


