看起来答案是调用 logging.disable(logging.NOTSET)
import logging
logger = logging.getLogger('testlog')
handler = logging.FileHandler('\\path\\to\\testlog.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.error("some error occurred")
logger.info('some info msg')
logging.disable(logging.ERROR)
logger.info('another info msg')
logging.disable(logging.NOTSET)
logger.info('last info msg')
生成带有此输出的日志文件:
2012-05-17 11:09:24,441 ERROR some error occurred
2012-05-17 11:09:24,443 INFO some info msg
2012-05-17 11:09:24,443 INFO last info msg