Traceback (most recent call last):
File "/usr/lib64/python2.6/logging/handlers.py", line 76, in emit
if self.shouldRollover(record):
File "/usr/lib64/python2.6/logging/handlers.py", line 150, in shouldRollover
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
ValueError: I/O operation on closed file
我的剧本中有一行:
handler = logging.handlers.RotatingFileHandler(cfg_obj.log_file,maxBytes = maxlog_size, backupCount = 10)
没有错误消息时运行正常。但是当出现错误日志时,除非重新启动进程,否则错误后的日志不会写入文件。每次出现错误时,我们都不希望重新启动进程。 感谢您的帮助!
答案 0 :(得分:1)
我强烈建议您使用配置文件。下面的配置代码" logging.conf"有不同的处理程序和格式化程序,例如:
[loggers]
keys=root
[handlers]
keys=consoleHandler, rotatingFileHandler
[formatters]
keys=simpleFormatter, extendedFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler, rotatingFileHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[handler_rotatingFileHandler]
class=handlers.RotatingFileHandler
level=INFO
formatter=extendedFormatter
args=('path/logs_file.log', 'a', 2000000, 1000)
[formatter_simpleFormatter]
format=%(asctime)s - %(levelname)s - %(message)s
datefmt=
[formatter_extendedFormatter]
format= %(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(funcName)s() %(message)s
datefmt=
现在如何使用它" main.py":
import logging.config
# LOGGER
logging.config.fileConfig('path_to_conf_file/logging.conf')
LOGGER = logging.getLogger('root')
try:
LOGGER.debug("Debug message...")
LOGGER.info("Info message...")
except Exception as e:
LOGGER.exception(e)
如果您需要更多帮助,请与我联系。