Jaymon / testdata

Python module to make testing easier, it can generate random data like names and text, run commands, fetch urls, create files and directories, and more
MIT License
10 stars 0 forks source link

Capture with StreamHandler #67

Open Jaymon opened 4 years ago

Jaymon commented 4 years ago

In the new Captain code I have stderr and stdout logging streams that are wrapped by my custom Stream class, something like this:

class InlineStream(io.IOBase):
    def __init__(self, stream):
        self.stream = stream

errh = logging.StreamHandler(stream=InlineStream("stderr"))

But this didn't work as expected:

with testdata.capture() as c:
    logger.info("this wasn't captured")

And the reason why is because testdata's logging capture logic is only messing with StreamHandlers that have stdout and stderr streams, I think this can be modified to capture all StreamHandlers and just assume stream handlers will always go to a print stream.

Or it can be expanded to just capture anything that is logged including being logged to a file or whatever, which might make more sense.