In C, I can put a log printf inside a function like this:
void xx_lock (int xx_flag)
{
printf ("%s: START with %d\n", __FUNCTION__, xx_flag);
}
so I can copy the same line where I need in any function and it displays the function name in the log. I want something similar in Python. But if I use
__name__
the same way, it displays the module name, not the function name.
def xxx_lock(xx_flag=0)
sys.stdout.write("%s: START with %d\n" % (__name__, xx_flag))
Is there some simple construct that I can use in that example to show the Python function name? To produce output like this:
xxx_lock: START with 1
Edited: added the parameter to the example.
katrielalex above is right that you should not attempt to do this the “C way”. Python has batteries included, so why not use them?
import logging, sys logging.basicConfig(format="%(filename)s:%(funcName)s:%(message)s",level=logging.DEBUG,stream=sys.stderr) def testFunc(): logging.debug("entering") testFunc()