python logging – KeyError: ‘fileHandler’ [solved]

I tried to use python native logging mechanism, with logging config loaded from external .conf file via logging.config module.

I got hard time finding out what was causing below error:

File “/usr/lib/python3.4/logging/config.py”, line 253, in _install_loggers
logger.addHandler(handlers[hand])
KeyError: ‘fileHandler’

Top of my configuration file was as per below, declaring 2 loggers, 1 handler and 1 formatter:

[loggers]
keys=root,defaultLogger

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

Body of my configuration file contained the following:

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('log/NPID[NPID].spo.log', 'a', 'UTF-8', False)

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

In other words, I am defining 2 handlers, but only declared 1 in the top part.
That was the source of the error.

Remediation is pretty simple, declare all your handlers in the declaration part. I replaced content with below:

[handlers]
keys=consoleHandler,fileHandler

Issue solved.

sources

Post a Comment

Your email is never published nor shared. You're allow to say what you want...