pythonlogging

Show most recent python logging messages in case of an error


I'm using python logging to log messages. One module (let's call it checker) produces a lot of tracing information, which is usually omitted (by setting the log level to ERROR) as it otherwise produces too many messages.

However, in some cases, an error occurs in another module runner that calls checker. In case of an error, I would like to see the most recent log messages from checker.

How can I do this with python's logging?

spdlog (a C++ logging library), has backtrace support that uses a ring buffer to accomplish this. Does something similar exist in python?


Solution

  • You can use instances of a subclass of the BufferingHandler class to buffer up messages, and only output them under particular circumstances. See this recipe in the logging docs for more information.