1

我在根目录和子文件夹中创建了一个包含各种脚本的模块。我之前正在创建一个定义中央记录器实例的日志记录脚本,并且我正在使用RotatedFileHandler来创建日志文件。问题是我无法找到可以决定模块执行已结束并且记录器实例需要doRollOver的条件。该怎么办?

import os
import glob
import logging
import logging.handlers
import time

LOG_FILENAME = str(os.getcwd()+'/logs/log-.out')

# Set up a specific logger with our desired output level
logger = logging.getLogger(__name__)

needRoll = <CONDITION?>

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, backupCount=50)
formatter = logging.Formatter(LOG_FORMAT)
handler.setFormatter(formatter)

logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

# This is a stale log, so roll it
if needRoll:
# Add timestamp
    logger.debug('---------Log closed on %s.---------' % time.asctime())

    # Roll over on application start
    logger.handlers[0].doRollover()

# Add timestamp
logger.debug('---------Log started on %s.---------' % time.asctime())
4

0 回答 0