Skip to content

Commit 0258ce8

Browse files
committed
logging: Added QueueHandler.prepare and updated documentation.
1 parent b5d23b4 commit 0258ce8

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

Doc/library/logging.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,7 @@ Loggers have the following attributes and methods. Note that Loggers are never
944944
instantiated directly, but always through the module-level function
945945
``logging.getLogger(name)``.
946946

947+
.. class:: Logger
947948

948949
.. attribute:: Logger.propagate
949950

@@ -2661,7 +2662,20 @@ supports sending logging messages to a queue, such as those implemented in the
26612662

26622663
.. method:: emit(record)
26632664

2664-
Sends the record to the handler's queue.
2665+
Enqueues the result of preparing the LogRecord.
2666+
2667+
.. method:: prepare(record)
2668+
2669+
Prepares a record for queuing. The object returned by this
2670+
method is enqueued.
2671+
2672+
The base implementation formats the record to merge the message
2673+
and arguments, and removes unpickleable items from the record
2674+
in-place.
2675+
2676+
You might want to override this method if you want to convert
2677+
the record to a dict or JSON string, or send a modified copy
2678+
of the record while leaving the original intact.
26652679

26662680
.. method:: enqueue(record)
26672681

Lib/logging/handlers.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,24 +1176,39 @@ def enqueue(self, record):
11761176
"""
11771177
self.queue.put_nowait(record)
11781178

1179+
def prepare(self, record):
1180+
"""
1181+
Prepares a record for queuing. The object returned by this
1182+
method is enqueued.
1183+
1184+
The base implementation formats the record to merge the message
1185+
and arguments, and removes unpickleable items from the record
1186+
in-place.
1187+
1188+
You might want to override this method if you want to convert
1189+
the record to a dict or JSON string, or send a modified copy
1190+
of the record while leaving the original intact.
1191+
"""
1192+
# The format operation gets traceback text into record.exc_text
1193+
# (if there's exception data), and also puts the message into
1194+
# record.message. We can then use this to replace the original
1195+
# msg + args, as these might be unpickleable. We also zap the
1196+
# exc_info attribute, as it's no longer needed and, if not None,
1197+
# will typically not be pickleable.
1198+
self.format(record)
1199+
record.msg = record.message
1200+
record.args = None
1201+
record.exc_info = None
1202+
return record
1203+
11791204
def emit(self, record):
11801205
"""
11811206
Emit a record.
11821207
1183-
Writes the LogRecord to the queue, preparing it for pickling first.
1208+
Writes the LogRecord to the queue, preparing it first.
11841209
"""
11851210
try:
1186-
# The format operation gets traceback text into record.exc_text
1187-
# (if there's exception data), and also puts the message into
1188-
# record.message. We can then use this to replace the original
1189-
# msg + args, as these might be unpickleable. We also zap the
1190-
# exc_info attribute, as it's no longer needed and, if not None,
1191-
# will typically not be pickleable.
1192-
self.format(record)
1193-
record.msg = record.message
1194-
record.args = None
1195-
record.exc_info = None
1196-
self.enqueue(record)
1211+
self.enqueue(self.prepare(record))
11971212
except (KeyboardInterrupt, SystemExit):
11981213
raise
11991214
except:

0 commit comments

Comments
 (0)