Skip to content

Commit

Permalink
Revert r42936: Revert twisted#6750 and related tickets
Browse files Browse the repository at this point in the history
Please see http://twistedmatrix.com/pipermail/twisted-python/2014-November/028912.html for more details.

Reopens: twisted#7548


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@43428 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
hawkowl committed Nov 4, 2014
1 parent a5ff276 commit 1eb09b8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 78 deletions.
41 changes: 2 additions & 39 deletions docs/core/howto/logger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,6 @@ For example:
This example will show the string "object with value 7 doing a task" because the ``log_source`` key is automatically set to the ``MyObject`` instance that the ``Logger`` is retrieved from.


Capturing Failures
~~~~~~~~~~~~~~~~~~

:api:`twisted.python.logger.Logger <Logger>` provides a :api:`twisted.python.logger.Logger.failure <failure>` method, which allows one to capture a :api:`twisted.python.failure.Failure <Failure>` object conveniently:

.. code-block:: python
from twisted.python.logger import Logger
log = Logger()
try:
1 / 0
except:
log.failure("Math is hard!")
The emitted event will have the ``"log_failure"`` key set, which is a :api:`twisted.python.failure.Failure <Failure>` than captures the exception.
This can be used my observers to obtain a traceback.
For example, :api:`twisted.python.logger.FileLogObserver <FileLogObserver>` will append the traceback to it's output::

Math is hard!

Traceback (most recent call last):
--- <exception caught here> ---
File "/tmp/test.py", line 8, in <module>
1/0
exceptions.ZeroDivisionError: integer division or modulo by zero

Note that this API is meant to capture unexpected and unhandled errors (that is: bugs, which is why tracebacks are preserved).
As such, it defaults to logging that the :api:`twisted.python.logger.LogLevel.critical <critical>` level.
It is generally more appropriate to instead use `log.error()` when logging an expected error condition that was appropriately handled by the software.


Namespaces
~~~~~~~~~~

Expand Down Expand Up @@ -237,9 +205,8 @@ Event keys added by the system

The logging system will add keys to emitted events.
All event keys that are inserted by the logging system with have a ``log_`` prefix, to avoid namespace collisions with application-provided event keys.
Applications should therefore not insert event keys using the ``log_`` prefix, as that prefix is reserved for the logging system.
System-provided event keys include:

``log_logger``

:api:`twisted.python.logger.Logger <Logger>` object that the event was emitted to.
Expand Down Expand Up @@ -268,15 +235,11 @@ System-provided event keys include:

The time that the event was emitted, as returned by :api:`time.time <time>` .

``log_failure``

A :api:`twisted.python.failure.Failure <Failure>` object captured when the event was emitted.


Avoid mutable event keys
~~~~~~~~~~~~~~~~~~~~~~~~

Emitting applications should be cautious about inserting objects into event which may be mutated later.
Emitting applications should be cautious about inserting objects into event which will be mutated later.
While observers are called synchronously, it is possible that an observer will do something like queue up the event for later serialization, in which case the serialized object may be different than intended.


Expand Down
14 changes: 4 additions & 10 deletions twisted/python/logger/_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,13 @@ def __call__(self, event):
@type event: L{dict}
"""
text = self.formatEvent(event)

if text is None:
text = u""

if "log_failure" in event:
text = u"\n".join((text, event["log_failure"].getTraceback()))

if not text:
return
if self._encoding is not None:
text = text.encode(self._encoding)

if text:
self._outFile.write(text)
self._outFile.flush()
self._outFile.write(text)
self._outFile.flush()



Expand Down
27 changes: 0 additions & 27 deletions twisted/python/logger/test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from twisted.trial.unittest import TestCase

from twisted.python.failure import Failure
from twisted.python.compat import unicode
from .._observer import ILogObserver
from .._file import FileLogObserver
Expand Down Expand Up @@ -109,32 +108,6 @@ def test_observeFlushes(self):
fileHandle.close()


def test_observeFailure(self):
"""
If the C{"log_failure"} key exists in an event, the observer should
append the failure's traceback to the output.
"""
try:
fileHandle = StringIO()
observer = FileLogObserver(fileHandle, lambda e: unicode(e))

try:
1 / 0
except ZeroDivisionError:
failure = Failure()

event = dict(log_failure=failure)
observer(event)
output = fileHandle.getvalue()
self.assertTrue(
output.startswith("{0}\nTraceback ".format(unicode(event))),
"Incorrect output:\n{0}".format(output)
)

finally:
fileHandle.close()



class TextFileLogObserverTests(TestCase):
"""
Expand Down
2 changes: 0 additions & 2 deletions twisted/topfiles/7548.bugfix

This file was deleted.

0 comments on commit 1eb09b8

Please sign in to comment.