microsoft / picologging

An optimized logging library for Python
https://microsoft.github.io/picologging
MIT License
659 stars 22 forks source link

handle error path (see handleError(record)) #4

Open tonybaloney opened 2 years ago

tonybaloney commented 2 years ago
    // TODO: handle error path (see handleError(record))

https://github.com/microsoft/picologging/blob/f3eedeb49c627248b3861a7f0d21c2b2dc764cda/src/picologging/streamhandler.cxx#L65

pamelafox commented 2 years ago

I just ran into this! I didn't fix it myself since I found your TODO, but here's a test for it (based off the CPython test):

def test_stream_error_handling():
    class BadStream(object):
        def write(self, data):
            raise RuntimeError('deliberate mistake')

    class TestStreamHandler(picologging.StreamHandler):
        def handleError(self, record):
            self.error_record = record

    handler = TestStreamHandler(BadStream())
    record = picologging.makeLogRecord({})
    handler.handle(record)
    assert handler.error_record is record

    handler = picologging.StreamHandler(BadStream())
    with pytest.raises(RuntimeError, match="RuntimeError: deliberate mistake"):
        handler.handle(record)

This test currently fails since TestStreamHandler handleError isn't being called.