Repository navigation
fix(nodeenv): stop patching logging.StreamHandler.emit globally - #472
tridev-tej wants to merge 1 commit into
Conversation
importing nodeenv replaced emit on every StreamHandler in the process, so other handlers lost their terminator. move the continued-line emit into a nodeenv-only handler subclass.
ekalinin
left a comment
There was a problem hiding this comment.
Thanks for the patch! Two things before merge.
-
nodeenv's own handler still lacks the error handling that the description mentions for the other handlers.
_StreamHandler.emitwrites to the stream itself, so whensys.stderris None (pythonw, or stderr closed with2>&-) the firstlogger.inforaisesAttributeError: 'NoneType' object has no attribute 'write', and a write error such asBrokenPipeErrorcomes out oflogger.infotoo. A plainStreamHandlerpasses both tohandleErrorand goes on. Setting the terminator and leaving the write to the stdlib keeps it:def emit(self, record): continued = getattr(record, 'continued', False) self.terminator = '' if continued else '\n' super().emit(record)
Handler.handle()holds the handler lock aroundemit, so settingterminatorthere is safe with threads. I checked it on a copy of the class: a continued record and thendone.still give* Install ... done.\n, and withsys.stderr = Noneor a stream raisingBrokenPipeErrorit returns normally, where the currentemitraises. -
#185 asks to split the logging setup from the module so nodeenv can be used as a library, and importing nodeenv still adds a stderr handler to the
nodeenvlogger and sets its level to INFO. So withlogging.basicConfig()in the importing code every nodeenv message is still printed twice, ashelloandINFO:nodeenv:hello. As the description says, this PR leaves that part out, soRefs #185instead ofFixes #185would keep the issue open for it.
importing nodeenv replaced
logging.StreamHandler.emitfor the whole process, so every other stream handler lost itsterminatorand its error handling. this moves the continued-line emit into a small handler subclass used only by nodeenv's own logger, so nodeenv output is unchanged. it does not split the module, it only removes the process-wide side effect from the issue.Fixes #185