12

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:
3
  • Doesn't that already do just that? Commented Jan 14, 2011 at 13:45
  • @marcog Sorry for my inability to explain my problem more clearly. TryPyPy and unutbu understood what I was looking for. Hopefully their answers explain what I was after. Commented Jan 15, 2011 at 9:41
  • I also made the same assumption as marcog and couldn't understand what the problem was. Perhaps edit your question to read "programmatically include arbitrary source code lines, not just the line that logs" Commented Jun 12, 2013 at 15:24

2 Answers 2

21
import inspect
import logging
import linecache

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:
        logging.debug(linecache.getline(
            __file__,
            inspect.getlineno(inspect.currentframe())-1))

if __name__=='__main__':
    main()

yields

test.py: DEBUG: main(): 18:     if something_is_not_right == True:
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, this is crying out for simple ____file____ and ____line____ (without any of the inspect/currentframe stuff).
5

Just because I saw unutbu try something similar, here's the code I came up with (too late to post otherwise):

import logging, sys

# From logging.py
def currentframe():
    """Return the frame object for the caller's stack frame."""
    try:
        raise Exception
    except:
        return sys.exc_traceback

f = open(__file__.rstrip('c'))
owncode = f.readlines()
f.close()

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:
        prev = owncode[currentframe().tb_frame.f_back.f_lineno - 2]
        logging.debug('previous line of source code here:\n%s' % prev)

if __name__ == '__main__':
    main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.