Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No handlers could be found for logger

Tags:

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" "]..here is my code,

(In my settings.py)

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

(In my views.py)

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()     logger.info('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 !!!

like image 885
Mani Avatar asked Jul 19 '11 10:07

Mani


2 Answers

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/') 
like image 168
xeeton Avatar answered Sep 22 '22 03:09

xeeton


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/

like image 36
sirosen Avatar answered Sep 25 '22 03:09

sirosen