Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

I have a logging function as follows.

logging.basicConfig(

    filename = fileName,

    format = "%(levelname) -10s %(asctime)s %(message)s",

    level = logging.DEBUG

)

def printinfo(string):

    if DEBUG:

        logging.info(string)

def printerror(string):

    if DEBUG:

        logging.error(string)

    print string

I need to log in the line number, stack information. For example:

1: def hello():

2:    goodbye()

3:

4: def goodbye():

5:    printinfo()

---> Line 5: goodbye()/hello()

1 Answer

0 votes
by (16.8k points)

def printinfo(string):

    if DEBUG:

        frame = inspect.currentframe()

        stack_trace = traceback.format_stack(frame)

        logging.debug(stack_trace[:-1])

    if LOG:

        logging.info(string)

gives me this info which is exactly what I need.

DEBUG      2011-02-23 10:09:13,500 [

  '  File "/abc.py", line 553, in <module>\n    runUnitTest(COVERAGE, PROFILE)\n', 

  '  File "/abc.py", line 411, in runUnitTest\n    printinfo(string)\n']

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 19, 2020 in SQL by Appu (6.1k points)

Browse Categories

...