Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, logging: use custom handler with dictionary configuration?

this is about the logging module on Python 3.2 (GNU/Linux x86_64): is it possible to set a custom handler with dictionary configuration? This is the code I'm trying:

import logging
import logging.config

class CustomHandler(logging.StreamHandler):
    pass

logconfig = {
    'version': 1,
    'handlers': {
        'console': {
            'class': 'CustomHandler',
        }
    },
    'loggers': {
        'custom': {
            'handlers': ['console'],
        }
    }
}
logging.config.dictConfig(logconfig)
logger = logging.getLogger('custom')
logger.error('Error message')

Which of course does not work. This is the output:

Traceback (most recent call last):
  File "/usr/lib/python3.2/logging/config.py", line 390, in resolve
    found = self.importer(used)
ImportError: No module named CustomHandler

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

Traceback (most recent call last):
  File "/usr/lib/python3.2/logging/config.py", line 569, in configure
    handler = self.configure_handler(handlers[name])
  File "/usr/lib/python3.2/logging/config.py", line 698, in configure_handler
    klass = self.resolve(config.pop('class'))
  File "/usr/lib/python3.2/logging/config.py", line 403, in resolve
    raise v
  File "/usr/lib/python3.2/logging/config.py", line 390, in resolve
    found = self.importer(used)
ValueError: Cannot resolve 'CustomHandler': No module named CustomHandler

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./prova.py", line 91, in <module>
    logging.config.dictConfig(logconfig)
  File "/usr/lib/python3.2/logging/config.py", line 777, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python3.2/logging/config.py", line 574, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'console': Cannot resolve 'CustomHandler': No module named CustomHandler

There is that importer method in the source which I really don't understand... Any idea?

Thank you!

like image 385
kynikos Avatar asked Mar 17 '11 23:03

kynikos


People also ask

What is Handler in logging Python?

Python Logging Handler The log handler is the component that effectively writes/displays a log: Display it in the console (via StreamHandler), in a file (via FileHandler), or even by sending you an email via SMTPHandler, etc. Each log handler has 2 important fields: A formatter which adds context information to a log.

What is dictConfig?

dictConfig (config) Takes the logging configuration from a dictionary. The contents of this dictionary are described in Configuration dictionary schema below.


1 Answers

You need to tell it where to find the CustomHandler class. The string should contain the module (and possibly package) where it can be found. If you are running this script directly, you can use __main__.CustomHandler. Otherwise, use your_module.CustomHandler, Where you replace your_module with the name of the module containing the class.

like image 134
dappawit Avatar answered Sep 22 '22 00:09

dappawit