We can code out some sort of logging decorator to echo function/method calls like the following:
def log(fn):
...
@log
def foo():
...
class Foo(object):
@log
def foo(self):
...
@log
def bar(self, a, b):
...
@log
def foobar(self, x, y, z):
...
But what if we are like to log method calls without putting that many @log in front of each meth definition? Is there some way to just put one decorator above a class definition to make all its method calls decorated/logged? Or are there some other better and interesting ways to do that instead of decorator?
I'm not sure what your use case is for this, but generally, I would think more about what exactly is the problem that you're trying to solve.
That said, here's an example that might do what you want but without a decorator:
Output:
See Attaching a decorator to all functions within a class
However, as the accepted answer to that question points out, it generally isn't a good idea.
If you decide to go the aspect oriented programming route, I suggest starting here: Any AOP support library for Python?
Well, If you do not want to explicitly decorate all your functions, you can get all the functions/methods of a given module and apply your decorator automatically. not the easiest thing but not infeasible in python :)
You can also try an aspect oriented programming framework.
my2c
This might be overkill, but there is a trace function facility that will inform you of a great deal of activity within your program: