1

I am trying to create my own log handler, which extends logging.Handler

import logging
from logging import Handler

class DBHandler(Handler,object):
    model = None
    def __init__(self, model):
        super(DBHandler, self).__init__(self)
        mod = __import__(model)
        components = name.split('.')
        for comp in components[1:]:
            mod = getattr(mod, comp)
        self.model = mod

    def emit(self,record):
        log_entry = self.model(level=record.levelname, message=record.msg)
        log_entry.save()

and this is the log config:

'search_log':{
    'level': 'INFO',
    'class': 'logger.handlers.DBHandler',
    'model': 'logger.models.SearchLog',
    'formatter': 'verbose',
     }

however I am getting the follow error, see stacktrace:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 252, in fetch_command
    app_name = get_commands()[subcommand]
  File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 101, in get_commands
    apps = settings.INSTALLED_APPS
  File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/conf/__init__.py", line 135, in __init__
    logging_config_func(self.LOGGING)
  File "/usr/lib/python2.7/logging/config.py", line 777, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python2.7/logging/config.py", line 575, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'search_log': Level not an integer or a valid string: <logger.handlers.DBHandler object at 0x2df1710>

I have been looking at the python logging source code and cannot figure out what I have done wrong

2 Answers 2

3

The problem is in this line:

super(DBHandler, self).__init__(self)

You should not be passing self in if you use super this way. You're passing in self twice, effectively, and the logging module is trying to interpret the 2nd self as a logging level.

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

5 Comments

NP: easy question, easy answer. But would you mind accepting it as the correct answer?
another quick question, I now keep getting "'module' object has no attribute 'handlers'" any ideas?
Is there a traceback, and does it say logging.handlers somewhere in it? It might be easier to post as a separate question so you can post the traceback and details.
Where is logger.handlers.DBHandler defined?
0

i solved the same problem with changing the level='Debug' to level=30 python 3.9.0

"Level not an integer or a valid string:'%r%' level

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.