Skip to content

fix(nodeenv): stop patching logging.StreamHandler.emit globally - #472

Open
tridev-tej wants to merge 1 commit into
ekalinin:masterfrom
tridev-tej:fix-issue-185
Open

tridev-tej wants to merge 1 commit into
ekalinin:masterfrom
tridev-tej:fix-issue-185

Conversation

@tridev-tej

Copy link
Copy Markdown
Contributor

importing nodeenv replaced logging.StreamHandler.emit for the whole process, so every other stream handler lost its terminator and 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

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 ekalinin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.emit writes to the stream itself, so when sys.stderr is None (pythonw, or stderr closed with 2>&-) the first logger.info raises AttributeError: 'NoneType' object has no attribute 'write', and a write error such as BrokenPipeError comes out of logger.info too. A plain StreamHandler passes both to handleError and 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 around emit, so setting terminator there is safe with threads. I checked it on a copy of the class: a continued record and then done. still give * Install ... done.\n, and with sys.stderr = None or a stream raising BrokenPipeError it returns normally, where the current emit raises.

  • #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 nodeenv logger and sets its level to INFO. So with logging.basicConfig() in the importing code every nodeenv message is still printed twice, as hello and INFO:nodeenv:hello. As the description says, this PR leaves that part out, so Refs #185 instead of Fixes #185 would keep the issue open for it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make nodeenv usable as library - split main and logging config into sep. file

2 participants