Open
Description
Bug report
Bug description:
The following code:
# test.py
import sys
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.debug('foo')
logger.debug('bar')
when piped to head -n1
, produces:
# python test.py | head -n1
foo
--- Logging error ---
Traceback (most recent call last):
File "/src/cpython/Lib/logging/__init__.py", line 1155, in emit
self.flush()
~~~~~~~~~~^^
File "/src/cpython/Lib/logging/__init__.py", line 1137, in flush
self.stream.flush()
~~~~~~~~~~~~~~~~~^^
BrokenPipeError: [Errno 32] Broken pipe
Call stack:
File "test.py", line 8, in <module>
logger.debug('bar')
Message: 'bar'
Arguments: ()
Exception ignored while flushing sys.stdout:
BrokenPipeError: [Errno 32] Broken pipe
This is because logging.StreamHandler.emit
does not handle BrokenPipeError
when flushing the stream.
I will fix this with the approach recommended by Note on SIGPIPE.
CPython versions tested on:
CPython main branch, 3.13
Operating systems tested on:
Linux