Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > smart logging and the "inspect" module ...

Reply
Thread Tools

smart logging and the "inspect" module ...

 
 
Darran Edmundson
Guest
Posts: n/a
 
      05-28-2005

I was playing around with the inspect module the other day
trying to write a quick and dirty "smart" logger. By this I mean
that writing a message to the global log would also yield some
info about the calling scope. If you look at my function "test"
below, I'd ideally like log messages:

foo.py:test, "message 1"
foo.py:Foo.__init__, "message 2"
foo.py:Foo.bar, "message 3"

For the life of me I can't figure out how to get this info without
resorting to ridiculous things like parsing the source code (available
via inspect.stack.

Any help would be much appreciated - my unit tests are failing
on this at the moment

Cheers,
Darran.

#------------ foo.py ---------------------------------

import inspect

class Log:

def __init__(self):
pass

def write(self, msg):
print
"------------------------------------------------------------"
print msg
print
"------------------------------------------------------------"
cframe, cmodule, cline, cfunction, csrc, tmp = inspect.stack()[1]
print inspect.stack()

# I want to determine if cfunction is a regular function or a
class method.
# If the latter, what is the name of the class?

del cframe


def test():

log = Log()
log.write('message 1)

class Foo:
def __init__(self):
log.write('message 2')

def bar(self):
log.write('message 3')

f = Foo()
f.bar()


test()

#--------------------- end of foo.py --------------
 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      05-28-2005
"Darran Edmundson" wrote:
> I was playing around with the inspect module the other day
> trying to write a quick and dirty "smart" logger. By this I mean
> that writing a message to the global log would also yield some
> info about the calling scope. If you look at my function "test"
> below, I'd ideally like log messages:
>
> foo.py:test, "message 1"
> foo.py:Foo.__init__, "message 2"
> foo.py:Foo.bar, "message 3"
>
> For the life of me I can't figure out how to get this info without
> resorting to ridiculous things like parsing the source code (available
> via inspect.stack.
>
> Any help would be much appreciated - my unit tests are failing
> on this at the moment
>
> Cheers,
> Darran.


Unfortunately introspection gives access to frame and code objects, but
not to the callable objects (functions, methods, builtins); I think the
reason is that code objects can be shared for different callables.
Also, even the function name and the line number are misleading if
there is an alias (e.g. baz = bar). By the way, here's an alternative
that doesn't use inspect.

import sys

class Log:
def write(self,message):
frame = sys._getframe(1)
code = frame.f_code
print "%s:%d:%s, %s" % (code.co_filename, frame.f_lineno,
code.co_name, message)


There's a logging module in the standard library btw; make sure you
check it out before going on and extending the quick and dirty logger.

Regards,
George

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Logging to a file and closing it again properly (logging module) Christoph Haas Python 1 06-14-2006 08:47 AM
Logging to a file and closing it again properly (logging module) Christoph Haas Python 0 06-12-2006 09:58 PM
Passing a log handle to a module? Help needed with logging module and rh0dium Python 2 08-04-2005 02:27 PM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM
trade 64mb smart media for 16mb smart media cards wjva Digital Photography 1 08-20-2003 08:30 PM



Advertisments