print line number and source filename

Stephen Hansen me+list/python at ixokai.io
Tue Jun 22 13:13:28 EDT 2010


On 6/22/10 9:44 AM, Peng Yu wrote:
> Also, always importing the inspect module and getting the frame and
> accessing the lineno from the frame is not very convenient to type. Is
> there a shorter way to access the line number (for example, in C++ and
> perl, __LINE__ can be used to access line number, which is much more
> convenient than the way that I found in python).

This all seems to be reinventing the wheel. Have you seen the logging
module?

Given this toy file:

---- begin -----
import logging
logging.basicConfig(level=logging.DEBUG,format="%(asctime)s
%(levelname)-5.5s [%(name)s %(module)s:%(funcName)s:%(lineno)d]
%(message)s")

def run():
    x = 5
    logging.debug("X = %d" % x)

run()
----- end -----

2010-06-22 10:12:07,907 DEBUG [root test:run:6] X = 5

It outputs exact time, type of log message, the name of the logger
(That's the 'root' -- you can skip if you only use one), the exact
module (test.py), function (run) and line (6).


-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100622/af0d5c87/attachment-0001.sig>


More information about the Python-list mailing list