Summary count for Python logging

Posted by Craig McQueen on Stack Overflow See other posts from Stack Overflow or by Craig McQueen
Published on 2010-05-17T07:29:43Z Indexed on 2010/05/17 7:30 UTC
Read the original article Hit count: 197

Filed under:
|

At the end of my Python program, I'd like to be able to get a summary of the number of items logged through the standard logging module. I'd specifically like to be able to get a count for each specified name (and possibly its children). E.g. if I have:

input_logger = getLogger('input')
input_logger.debug("got input1")
input_logger.debug("got input2")
input_logger.debug("got input3")

network_input_logger = getLogger('input.network')
network_input_logger.debug("got network input1")
network_input_logger.debug("got network input2")

getLogger('output')
output_logger.debug("sent output1")

Then at the end I'd like to get a summary such as:

input: 5
input.network: 2
output: 1

Perhaps by calling a getcount() method for a logger or a handler.

What would be a good way to achieve this? I imagine it would involve a sub-class of one of the logging classes, but I'm not sure which one would be best.

© Stack Overflow or respective owner

Related posts about python

Related posts about logging