mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
logging: Added QueueHandler.prepare and updated documentation.
This commit is contained in:
parent
b5d23b4dfc
commit
0258ce8f0a
2 changed files with 42 additions and 13 deletions
|
|
@ -944,6 +944,7 @@ Loggers have the following attributes and methods. Note that Loggers are never
|
||||||
instantiated directly, but always through the module-level function
|
instantiated directly, but always through the module-level function
|
||||||
``logging.getLogger(name)``.
|
``logging.getLogger(name)``.
|
||||||
|
|
||||||
|
.. class:: Logger
|
||||||
|
|
||||||
.. attribute:: Logger.propagate
|
.. attribute:: Logger.propagate
|
||||||
|
|
||||||
|
|
@ -2661,7 +2662,20 @@ supports sending logging messages to a queue, such as those implemented in the
|
||||||
|
|
||||||
.. method:: emit(record)
|
.. method:: emit(record)
|
||||||
|
|
||||||
Sends the record to the handler's queue.
|
Enqueues the result of preparing the LogRecord.
|
||||||
|
|
||||||
|
.. method:: prepare(record)
|
||||||
|
|
||||||
|
Prepares a record for queuing. The object returned by this
|
||||||
|
method is enqueued.
|
||||||
|
|
||||||
|
The base implementation formats the record to merge the message
|
||||||
|
and arguments, and removes unpickleable items from the record
|
||||||
|
in-place.
|
||||||
|
|
||||||
|
You might want to override this method if you want to convert
|
||||||
|
the record to a dict or JSON string, or send a modified copy
|
||||||
|
of the record while leaving the original intact.
|
||||||
|
|
||||||
.. method:: enqueue(record)
|
.. method:: enqueue(record)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1176,13 +1176,19 @@ class QueueHandler(logging.Handler):
|
||||||
"""
|
"""
|
||||||
self.queue.put_nowait(record)
|
self.queue.put_nowait(record)
|
||||||
|
|
||||||
def emit(self, record):
|
def prepare(self, record):
|
||||||
"""
|
"""
|
||||||
Emit a record.
|
Prepares a record for queuing. The object returned by this
|
||||||
|
method is enqueued.
|
||||||
|
|
||||||
Writes the LogRecord to the queue, preparing it for pickling first.
|
The base implementation formats the record to merge the message
|
||||||
|
and arguments, and removes unpickleable items from the record
|
||||||
|
in-place.
|
||||||
|
|
||||||
|
You might want to override this method if you want to convert
|
||||||
|
the record to a dict or JSON string, or send a modified copy
|
||||||
|
of the record while leaving the original intact.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
# The format operation gets traceback text into record.exc_text
|
# The format operation gets traceback text into record.exc_text
|
||||||
# (if there's exception data), and also puts the message into
|
# (if there's exception data), and also puts the message into
|
||||||
# record.message. We can then use this to replace the original
|
# record.message. We can then use this to replace the original
|
||||||
|
|
@ -1193,7 +1199,16 @@ class QueueHandler(logging.Handler):
|
||||||
record.msg = record.message
|
record.msg = record.message
|
||||||
record.args = None
|
record.args = None
|
||||||
record.exc_info = None
|
record.exc_info = None
|
||||||
self.enqueue(record)
|
return record
|
||||||
|
|
||||||
|
def emit(self, record):
|
||||||
|
"""
|
||||||
|
Emit a record.
|
||||||
|
|
||||||
|
Writes the LogRecord to the queue, preparing it first.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
self.enqueue(self.prepare(record))
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
raise
|
raise
|
||||||
except:
|
except:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue