Logging: Formatter: name of the function

G

Gregor Horvath

Hi,

Is there a possibility to format a log message to give the function name
where the log appears?

Example

import logging

def aTestFunction():
logger.debug("This is a message")

The log should read:

aTestFunction This is a message.

There is a possibilty to format the module namewith %(module)s, but I
did not find one for the function name.

Is there a possibilty or can I create it myself?
How can I determine the function name within a function?
Introspection?
 
B

Bengt Richter

Hi,

Is there a possibility to format a log message to give the function name
where the log appears?

Example

import logging

def aTestFunction():
logger.debug("This is a message")

The log should read:

aTestFunction This is a message.

There is a possibilty to format the module namewith %(module)s, but I
did not find one for the function name.

Is there a possibilty or can I create it myself?
How can I determine the function name within a function?
Introspection?
There's not a nice way that I know of, but you can do something like
... print 'logger string containing function name "%s"'%sys._getframe().f_code.co_name
... logger string containing function name "foo"

However, you might want to consider using a decorator that could
wrap a function if debugging and leave it alone otherwise. Also you
can log before and after calling the function. E.g.,
... if not __debug__: return f
... def wrap(*args, **kw):
... print 'before entering function "%s" ...'%f.func_name
... result = f(*args, **kw)
... print 'after returning from function "%s" ...'%f.func_name
... return result
... wrap.func_name = f.func_name # make it look the same if desired
... return wrap
... ... def foo(something): print something; return 'whatever'
... before entering function "foo" ...
hello
after returning from function "foo" ...
'whatever' File "<stdin>", line 1
SyntaxError: can not assign to __debug__

Oops, can't experiment that way ;-)
We'll just start it with -O to set __debug__ False:

[ 8:45] E:\Info\Politics>py24 -O
Python 2.4b1 (#56, Nov 3 2004, 01:47:27)
[GCC 3.2.3 (mingw special 20030504-1)] on win32
Type "help", "copyright", "credits" or "license" for more information. ... if not __debug__: return f
... def wrap(*args, **kw):
... print 'before entering function "%s" ...'%f.func_name
... result = f(*args, **kw)
... print 'after returning from function "%s" ...'%f.func_name
... return result
... wrap.func_name = f.func_name # make it look the same if desired
... return wrap
... ... def foo(something): print something; return 'whatever'
... hello
'whatever'

You could still do stuff unconditionally of course, and also inside foo.

Regards,
Bengt Richter
 
S

Sylvain Defresne

Le vendredi 23 décembre 2005 à 16:23 +0100, Gregor Horvath a écrit :
Hi,

Is there a possibility to format a log message to give the function name
where the log appears?

Example

import logging

def aTestFunction():
logger.debug("This is a message")

The log should read:

aTestFunction This is a message.

You can use the currentframe of the inspect module. For example :
... frame = inspect.currentframe(1)
... print frame.f_code.co_name, string a_test_function This is a message

But please take note that this is not recommended. As stated in the
documentation of inspect.currentframe : "[t]his function should be used
for internal and specialized purposes only."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top