3

I am trying to configure my logging using the dictConfig() method. I think I understood the Dictionary Schema Details, but my code still throws an error.

Code:

import logging
import logging.config

verbose_formatter_conf = {
    "format": "%(asctime)-15s %(name)s %(levelname)-9s %(message)s",
}

formatters_dict = {
    "verbose_formatter": verbose_formatter_conf,
}

logfile_handler_conf = {
    "class": logging.FileHandler,
    "level": logging.DEBUG,
    "formatter": "verbose_formatter",
    "filename": "logfile.txt",
    "mode": "w",
    "encoding": "utf-8",
}

handlers_dict = {
    "logfile_handler": logfile_handler_conf,
}

verbose_file_logger_conf = {
    "propagate": True,
    "handlers": ["logfile_handler"]
}

loggers_dict = {
    "verbose_file_logger": verbose_file_logger_conf
}

final_conf = {
    "version": 1,
    "formatters": formatters_dict,
    "handlers": handlers_dict,
    "loggers": loggers_dict,
    "incremental": False,
}

logging.config.dictConfig(final_conf)
logger = logging.getLogger(__name__)

logger.debug("hello")

The error it throws is:

Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\logging\config.py", line 555, in configure
    handler = self.configure_handler(handlers[name])
  File "C:\Program Files\Python37\lib\logging\config.py", line 705, in configure_handler
    klass = self.resolve(cname)
  File "C:\Program Files\Python37\lib\logging\config.py", line 372, in resolve
    name = s.split('.')
AttributeError: type object 'FileHandler' has no attribute 'split'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\Tejul\.vscode\extensions\ms-python.python-2019.6.22090\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\Tejul\.vscode\extensions\ms-python.python-2019.6.22090\pythonFiles\lib\python\ptvsd\__main__.py", line 434, in main
    run()
  File "c:\Users\Tejul\.vscode\extensions\ms-python.python-2019.6.22090\pythonFiles\lib\python\ptvsd\__main__.py", line 312, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Program Files\Python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files\Python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "g:\Sven\Python\pygtris\logger_conf.py", line 42, in <module>
    logging.config.dictConfig(final_conf)
  File "C:\Program Files\Python37\lib\logging\config.py", line 792, in dictConfig
    dictConfigClass(config).configure()
  File "C:\Program Files\Python37\lib\logging\config.py", line 563, in configure
    '%r' % name) from e
ValueError: Unable to configure handler 'logfile_handler'

But I don't understand it. My handler is configured just as the documentation says it should be, I think. Did I oversee something?

3
  • 1
    Try to surround logging.FileHandler with quotation marks. Commented Jun 29, 2019 at 7:25
  • Ok, that helped, and I also corrected "mode": "w", because I want the logfile to be overwritten each time. Commented Jun 29, 2019 at 7:37
  • So now it no longer throws any error, but it also does not write any output into the logfile - I wonder why. Commented Jun 29, 2019 at 7:39

1 Answer 1

3

The error seems to be that the "class" value in the logfile_handler_conf dictionary is supposed to be a string, "logging.FileHandler", rather than the FileHandler class itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.