From: Peng Yu on
I want to print filename and line number for debugging purpose. So far
I only find how to print the line number but not how to print
filename.

import inspect
print inspect.currentframe().f_lineno

I found inspect.getsourcefile(), but I have to supply a class name to
it. I have searched online, but I don't find how to print the source
filename. Would you please let me know?

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).

--
Regards,
Peng
From: Stephen Hansen on
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/

From: Stephen Hansen on
On 6/22/10 10:26 AM, Shashwat Anand wrote:
> ---- 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 -----
>
> You can probably use string default concatenation feature of python.
> Anyway, am just nitpicking. Loved your example, as I was unaware of
> logging module.

As a sort of personal policy, I never use string concatenation. I do not
advocate this position or advise that others should follow it, but in my
experience-- for me-- it does not scale. I find I often want to go back
and modify or enhance an output with new data, and string concatenation
ends up becoming cumbersome after awhile, and so I always use format
strings so I don't have to rewrite the line to convert from + to % later.

(And in a couple years when I can migrate to Py3, I'll start using
.format())

--

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

From: Peng Yu on
On Tue, Jun 22, 2010 at 12:13 PM, Stephen Hansen
<me+list/python(a)ixokai.io> wrote:
> 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).

I tried to put the above code in a module. Say in a.b.__init__.py

%(module)s only print to "__init__". However, I need the fullname
a.b.__init__. I looked at the manual, but I don't see what format
string I should supply. Would you please let me know?

--
Regards,
Peng
From: Vinay Sajip on
On Jun 24, 4:07 am, Peng Yu <pengyu...(a)gmail.com> wrote:
> %(module)s only print to "__init__". However, I need the fullname
> a.b.__init__. I looked at the manual, but I don't see what format
> string I should supply. Would you please let me know?
>

Did you look at this part of the documentation?

http://docs.python.org/library/logging.html#formatter-objects

You should use in each of your modules

logger = logging.getLogger(__name__)

and in the format string, use %(name)s. This will give "a.b" for a
logger defined in <pythonpath>/a/b/__init__.py, "a" for a logger
defined in <pythonpath>/a/__init__.py, "a.b.c" for a logger defined in
<pythonpath>/a/b/c.py, and so on.

Regards,

Vinay Sajip