[Django]-No handlers could be found for logger

23👍

The docs are a little unclear about this, but when you use the built-in functionality for specifying logging settings, you don’t need to get an instance of the logger.

You would simply do the following:

import logging

def empdel(request,id):
    e = get_object_or_404(emp, pk=id)
    e.delete()
    logging.info('A row is deleted successfully !!!')
    return HttpResponseRedirect('/empthanks/')
👤xeeton

13👍

I think you have misunderstood what a Handler is in the context of the logging package.

A Handler is an object which you attach to a Logger.
Whenever the logger has a message to process, it sends the message to all of its handlers.
Additionally, Loggers exist in a tree structure, with the aptly named “root” Logger at its root.
After sending a message to its Handlers, a Logger may pass it on to its parent in the tree (determined by the Logger instance’s propagate attribute).

As a slightly humorous aside, I once found an application bug in which someone started using the Logger named “root”, which is different from the Root Logger.

Don’t use the root logger (accessed by logging.info and company, or logging.getLogger()).
Any Handlers you attach or settings you alter will also affect well behaved libraries that propagate their errors up to the root logger.
Case in point: I once wrote a simple script using the root logger out of laziness.
I then incorporated a few calls to boto and got an explosion of debug messages that were being propagated to the root logger.
It is recommended that you always create a logger whose name matches your package’s name in the namespace (this also leads to nice inheritance structure with getLogger‘s interpretation of .) by creating loggers with logging.getLogger(__name__)

I strongly recommend giving a thorough reading to the section on Logger Objects in the logging docs: https://docs.python.org/2/library/logging.html

You may notice that your config also specifies a Formatter.
Formatters handle the presentation of a message when it is processed by a Handler, so that a single message may be formatted differently for terminal output than for, for example, rsyslog.


All of that said, to fix your code you can use the logger you have created, and just attach a handler to it.
I would recommend creating a StreamHandler (the base Handler class is not meant to be instantiated, but for horrible reasons is not an ABC), since that covers a lot of typical use cases.

myhandler = logging.StreamHandler()  # writes to stderr
myformatter = logging.Formatter(fmt='%(levelname)s: %(message)s')
myhandler.setFormatter(myformatter)
logger.addHandler(myhandler)

However, it seems that Django config already includes a lot of this information, not configured the way that I do things above, of course.
Not having actually written Django apps, I don’t want to give you bad information on this, but Django provides nice docs here: https://docs.djangoproject.com/en/dev/topics/logging/

Leave a comment