Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-30522: Implemented a method to allow setting a logging.StreamHand… #2921

Merged
merged 3 commits into from
Jul 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Doc/library/logging.handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,26 @@ and :meth:`flush` methods).
:meth:`close` method is inherited from :class:`~logging.Handler` and so
does no output, so an explicit :meth:`flush` call may be needed at times.

.. method:: setStream(stream)
Copy link
Member

Choose a reason for hiding this comment

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

This method needs a versionadded marker.


Sets the instance's stream to the specified value, if it is different.
The old stream is flushed before the new stream is set.

:param stream: The stream that the handler should use.

:return: the old stream, if the stream was changed, or *None* if it wasn't.

.. versionadded:: 3.7


.. versionchanged:: 3.2
The ``StreamHandler`` class now has a ``terminator`` attribute, default
value ``'\n'``, which is used as the terminator when writing a formatted
record to a stream. If you don't want this newline termination, you can
set the handler instance's ``terminator`` attribute to the empty string.
In earlier versions, the terminator was hardcoded as ``'\n'``.


.. _file-handler:

FileHandler
Expand Down
20 changes: 20 additions & 0 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,26 @@ def emit(self, record):
except Exception:
self.handleError(record)

def setStream(self, stream):
"""
Sets the StreamHandler's stream to the specified value,
if it is different.

Returns the old stream, if the stream was changed, or None
if it wasn't.
"""
if stream is self.stream:
result = None
else:
result = self.stream
self.acquire()
try:
self.flush()
Copy link

@qingyunha qingyunha Aug 3, 2017

Choose a reason for hiding this comment

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

Is the flush need here? The only place to write steam is emit(),that already has a flush. If the flush is not needed, the lock is also not needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

This may seem a bit defensive but is unlikely to be in performance-critical code, I would have thought.

self.stream = stream
finally:
self.release()
return result

def __repr__(self):
level = getLevelName(self.level)
name = getattr(self.stream, 'name', '')
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,20 @@ def test_error_handling(self):
finally:
logging.raiseExceptions = old_raise

def test_stream_setting(self):
"""
Test setting the handler's stream
"""
h = logging.StreamHandler()
stream = io.StringIO()
old = h.setStream(stream)
self.assertIs(old, sys.stderr)
actual = h.setStream(old)
self.assertIs(actual, stream)
# test that setting to existing value returns None
actual = h.setStream(old)
self.assertIsNone(actual)

# -- The following section could be moved into a server_helper.py module
# -- if it proves to be of wider utility than just test_logging

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added a ``setStream`` method to ``logging.StreamHandler`` to allow the
stream to be set after creation.