I use the logging module to warn about issues with some routines. These routines may be run several times before getting back to normal operations (e.g. repeated queries to an API which fail, but eventually go through). Each failed call triggers a log entry.

Is there a way to limit the number of identical log messages?

I would like this limit to fire off after n identical messages being output, then possibly inform that n more were generated (so not to clutter the log file) and reset once a recovery log is received. This is the ideal scenario - I am looking at how to approach the problem to start with.