Python记录到在配置文件中初始化的MemoryHandler

时间:2017-06-07 09:02:15

标签: python logging configuration-files

我正在尝试将python MemoryHandler添加到日志记录配置文件中。我使用类似于以前工作的文件处理程序的结构。我对我收到的错误感到有点困惑,并且无法弄清楚我所缺少的内容以便将日志记录转到内存中。下面列出了必要的信息。非常感谢协助。

log_settings.conf

[loggers]
keys=root

[handlers]
keys=memHandler, consoleHandler

[formatters]
keys=fileFormatter, consoleFormatter

[logger_root]
level=DEBUG
handlers=memHandler, consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=consoleFormatter
args=(sys.stdout,)

[handler_memHandler]
class=handlers.MemoryHandler
formatter=fileFormatter
args=(10, DEBUG, '%(logfilename)s')

[formatter_fileFormatter]
format=[%(asctime)s] [%(levelname)8s] --- (%(filename)15s:%(lineno)4s) %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_consoleFormatter]
format=%(message)s

错误追溯

Traceback (most recent call last):
  File "E:\BuildScript\proj_tcl\blr_sbz\blr_sbz_build.py", line 22, in <module>
    main(args.jenkins, args.revision, args.jenkins_num)
  File "E:\BuildScript\proj_tcl\blr_sbz\blr_sbz_build.py", line 10, in main
    build(jenkins, revision, jenkins_num)
  File "../scripts\build.py", line 17, in build
    logging.info('Starting the FGPA build script')
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1870, in info
    root.info(msg, *args, **kwargs)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1301, in info
    self._log(INFO, msg, args, **kwargs)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1437, in _log
    self.handle(record)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1447, in handle
    self.callHandlers(record)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1509, in callHandlers
    hdlr.handle(record)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 858, in handle
    self.emit(record)
  File "C:\Program Files\Python36\lib\logging\handlers.py", line 1213, in emit
    self.flush()
  File "C:\Program Files\Python36\lib\logging\handlers.py", line 1289, in flush
    self.target.handle(record)
AttributeError: 'str' object has no attribute 'handle'

build.py

logging.config.fileConfig('../scripts/logging/log_settings.conf', defaults={'logfilename': '../scripts/logging/log.log'})
logging.info('Starting the FGPA build script')

1 个答案:

答案 0 :(得分:1)

请改用此配置文件:

[loggers]
keys=root

[handlers]
keys=memHandler, consoleHandler

[formatters]
keys=fileFormatter, consoleFormatter

[logger_root]
level=DEBUG
handlers=memHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=consoleFormatter
args=(sys.stdout,)

[handler_memHandler]
class=handlers.MemoryHandler
formatter=fileFormatter
args=(10, DEBUG)
target=consoleHandler

[formatter_fileFormatter]
format=[%(asctime)s] [%(levelname)8s] --- (%(filename)15s:%(lineno)4s) %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_consoleFormatter]
format=%(message)s

请注意,您不应在根记录器的处理程序列表中包含consoleHandler。此外,您需要使用target=行设置目标,并且需要引用目标处理程序的键。使用以下配置:

logging.config.fileConfig('logging.ini')