不知道它是否已经存在,但是我找到了解决方案:
http://bytes.com/topic/python/answer...iguration-file
# logging.conf
[loggers]
keys=root
[handlers]
keys=TimedRotatingFileHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=TimedRotatingFileHandler
[handler_TimedRotatingFileHandler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('C:\\log\\'+os.path.basename(sys.argv[0]).split('.')[0]+'.log', 'D', 15, 'backupCount=30')
[formatter_simpleFormatter]
format=%(asctime)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
# main.py
import logging, os
logging.config.fileConfig('C:\\log\\logging.conf')
log = logging.getLogger()
log.info('Started')
#do stuff
log.info('Finished')
屏幕上将不会打印任何内容。
一份文件
C:\ log \ main.log将每15天创建和轮换一次
日志如下所示:
2011-07-04 12:18:16开始
2011-07-04 12:18:19完成
我不确定日志记录级别以及调用“ log.info()”时如何在屏幕上打印,因此我也不必打印。
干杯
From: https://bytes.com/topic/python/insights/914546-choosing-log-file-destination-logging-configuration-file