1

I am trying to use decorator for printing my logs. And to do this, I have defined decorator in one file named custom_logger.py :

import logging

class Logger(object):
   def __init__(self,decoratee_enclosing_class):
        self.decoratee_enclosing_class = decoratee_enclosing_class
   def __call__(self, aFunc):
      """Trace entry, exit and exceptions."""
      def loggedFunc( *args, **kw ):
         print "enter", aFunc.__name__
         try:
            result= aFunc( *args, **kw )
         except Exception, e:
            print "exception", aFunc.__name__, e
            raise
         print "exit", aFunc.__name__
         return result
         loggedFunc.__name__= aFunc.__name__
         loggedFunc.__doc__= aFunc.__doc__
         return loggedFunc

And here is my sample test code :

from custom_logger import Logger

class Test(object):
   @Logger('Test')
   def testP(self):
      print "hello"
a = Test()
a.testP()

I am getting following error : Traceback (most recent call last): File "test.py", line 13, in a.testP() TypeError: 'NoneType' object is not callable

So can any one point out what am I missing ?

I have followed this link for reference.

Community
  • 1
  • 1
Rahul
  • 1,607
  • 3
  • 23
  • 41
  • The last three lines in the first block of code should be outdented by one level. – UltraInstinct Mar 14 '17 at 11:09
  • What's the point of `decoratee_enclosing_class`? Why take the class name as the argument to a method decorator? Also `except Exception, e:` is at this point a very old fashioned syntax that won't work on Python 3.x. – jonrsharpe Mar 14 '17 at 11:10
  • @jonrsharpe Thanks for pointing out ... but this is just sample code which I was trying. – Rahul Mar 14 '17 at 16:17

1 Answers1

3

You have an indentation error in your decorator. The last three lines of the __call__ method should be at the same indentation as the def loggedFunc line.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895