Logging - have logger use caller's function name and line number

David bouncingcats at gmail.com
Sat Dec 29 18:36:16 EST 2018


On Sun, 30 Dec 2018 at 05:58, Malcolm Greene <python at bdurham.com> wrote:
>
> I have a class method that adds additional context to many of the
> class's log messages. Rather than calling, for example,  logger.info(
> 'message ...' ) I would like to call my_object.log( 'message ...' ) so
> that this additional context is managed in a single place. I'm actually
> doing that and its working great except that ... as expected ... all my
> log records have the my_object.log method's name and line numbers vs the
> callers' names and line numbers.
> Is there a technique I can use to have my custom log method inject the
> caller's name and line numbers in the log output?

You can use this:
https://docs.python.org/3/library/inspect.html#the-interpreter-stack


More information about the Python-list mailing list