How to print source code lines in python logger

Posted by anon on Stack Overflow See other posts from Stack Overflow or by anon
Published on 2011-01-14T13:36:37Z Indexed on 2011/01/14 14:53 UTC
Read the original article Hit count: 174

Filed under:
|
|

Is there some relatively simple way to programmatically include source code lines to python logger report. For example...

import logging

def main():
    something_is_not_right = True
    logging.basicConfig(level=logging.DEBUG,
                        format=('%(filename)s: '    
                                '%(levelname)s: '
                                '%(funcName)s(): '
                                '%(lineno)d:\t'
                                '%(message)s')
                        )

    if something_is_not_right == True:
        logging.debug('some way to get previous line of source code here?')

So that output would look like this.

example.py: DEBUG: main(): 14:       if something_is_not_right == True:

© Stack Overflow or respective owner

Related posts about python

Related posts about debugging