2

Based on the accepted answer to this question: storing logger messages in a string

I would like to accomplish the same thing, but send each each log message to a list.

Many thanks

GlaceCelery
  • 921
  • 1
  • 13
  • 30

1 Answers1

2

This is already possible in stdlib by using a special-cased BufferingHandler instance. If you use an infinite buffer, it will never flush.

>>> myhandler = logging.handlers.BufferingHandler(capacity=float('inf'))
>>> logging.basicConfig(handlers=[myhandler]) 
>>> logging.warning("uh-oh")
>>> logging.error("boom") 
>>> myhandler.buffer
[<LogRecord: root, 30, <ipython-input-11-feb6b3c2bc6f>, 1, "uh-oh">,
 <LogRecord: root, 40, <ipython-input-12-fd4144525168>, 1, "boom">]
wim
  • 338,267
  • 99
  • 616
  • 750