Skip to content

Instantly share code, notes, and snippets.

@ento
Created August 29, 2013 08:04
Show Gist options
  • Save ento/6375397 to your computer and use it in GitHub Desktop.
Save ento/6375397 to your computer and use it in GitHub Desktop.
Iterate through logs captured by nosetest's logcapture plugin
import logging
from nose.plugins import logcapture
def iter_captured_logs(pluck=None):
handler = get_nose_log_handler()
# need to call `format` in order to let it compose the `message` attr for us
format = handler.format
for r in handler.buffer:
format(r)
kv = r.__dict__
yield kv.get(pluck) if pluck else kv
def get_nose_log_handler():
root_logger = logging.getLogger()
try:
return next(handler for handler in root_logger.handlers if isinstance(handler, logcapture.MyMemoryHandler))
except StopIteration:
raise Exception("nose's logcapture plugin must be enabled.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment