Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging.warn() add stacktrace

There are several logging.warn('....') calls in the legacy code base I am working on today.

I want to understand the log output better. Up to now logging.warn() does emit one line. But this single line is not enough to understand the context.

I would like to see the stacktrace of the interpreter.

Since there are a lot of logging.warn('....') lines in my code, I would like to leave them like they are and only modify the configuration of the logging.

How can I add the interpreter stacktrace to every warn() or error() call automatically?

I know that logging.exception("message") shows the stacktrace, but I would like to leave the logging.warn() lines untouched.

like image 967
guettli Avatar asked Jun 08 '26 23:06

guettli


1 Answers

The answer I was looking for was given by @Martijn Pieters♦ in the comments

In python 3.x

logger.warning(f'{error_message}', stack_info=True)

does exactly what you need.

Thanks @Martijn Pieters♦

like image 101
Prakash Avatar answered Jun 11 '26 12:06

Prakash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!