[Solved] No handlers could be found for logger

I am newbie to Django.I am trying for Django logging now. While trying,I am getting this error [“No handlers could be found for logger “sample” “] is my code,

(In my

    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
    'handlers': {
        'default': {
            'filename': '/home/linuxuser/mani/f/logs/msg.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
    'loggers': {
        'sample': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True,

(In my

import logging
import logging.handlers
from django.conf import settings
logger = logging.getLogger('sample')

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

While running this code, i got this error ie [“No handlers could be found for logger “sample” “].. Whats wrong with my code? Why i am getting such a error even i am using handler in LOGGING?

and also i am trying to save log message into the file that i have used in LOGGING…any idea?? Thanks in advance !!!

Enquirer: Mani

Solution #1:

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()'A row is deleted successfully !!!')
    return HttpResponseRedirect('/empthanks/')
Respondent: xeeton
Solution #2:

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 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:

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')

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:

Respondent: sirosen
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy