[BangPypers] FW: Exception file, lineno, method inforamtion needed

Anand Chitipothu anandology at gmail.com
Mon Oct 26 13:38:55 CET 2009


On Mon, Oct 26, 2009 at 5:41 PM, Anand Balachandran Pillai
<abpillai at gmail.com> wrote:
> Forwarding a question from a friend, since he could not reach the list
> due to some reason...
>
> ---------------------------------------------------------------------------------------------------
> From: edukulla Santhosh kumar <k2_santhosh at yahoo.com>
> Subject: Exception file,lineno,method inforamtion needed
> To: bangpypers at python.org
> Date: Tuesday, October 13, 2009, 12:52 PM
>
>    Hi Team,
>
>    Iam using the logging module of python to log messages to a file.
>
>    I have the following code:
>
>    ###Filename : Method.py
>    class _Method:
>
>    def _init_(self):
>        self.a = None
>
>    def g(self):
>         a = 1/0
>         print(a)
>
>    ###Filename: Test.py
>    from Method import _Method
>    from log import log
>
>    k = _Method()
>
>    def f():
>       try:
>         k.g()
>      except Exception,e:
>         log(e)
>
>    Now, the log()  method under Test above is under log.py and uses the
> logging module of python with the following format specifications. When we
> print the e under log, what i wanted is the information trace pertaining to
> Method.py like where actually the line no,filename and module name
> information under method.py to be printed. Currently,iam not getting what i
> wanted.
>
>    What need to be done to achieve this?
>    %(levelno)s
>    %(levelname)s
>    %(pathname)s
>    %(filename)s
>    %(module)s
>    %(funcName)s
>    %(lineno)d
>    %(created)f
>    %(relativeCreated)d
>    %(asctime)s
>    %(msecs)d
>    %(thread)d
>    %(threadName)s
>    %(process)d
>    %(message)s

You can get filename, function, lineno information like this:

    exception_type, exception_value, tback = sys.exc_info()
    frames = []
    while tback is not None:
        filename = tback.tb_frame.f_code.co_filename
        function = tback.tb_frame.f_code.co_name
        lineno = tback.tb_lineno

thread name can be found from:
    threading.currentThread().getName()

Can you explain what mean by level, created, relativeCreated, asctime,
process etc.

Anand


More information about the BangPypers mailing list