I found similar questions here, but my question is bit different…
I have log() function as staticmethod in MyUtil class:
class MyUtil(object):
@staticmethod
def log(message, level):
...
but now I have to know from which class/object this staticmethod was called?
For example:
class Deployer(object):
def deploy(self):
...
MyUtil.log ("Starting deployment", "info")
...
Is it possible to get and log this “Deployer” class name from MyUtil.log function?
(I’m aware that this is very un-OO and probably never useful in a well written program, but anyway…)
It’s not the nicest design, logging is a “singleton” with all of its drawbacks, but if you are willing to accept that, there’s some language support for your problem. You can get the caller by inspecting the stack: