Created
August 29, 2013 08:04
-
-
Save ento/6375397 to your computer and use it in GitHub Desktop.
Iterate through logs captured by nosetest's logcapture plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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