blob: a664efdc625261f4e5559909d48af992dea6547b [file] [log] [blame]
Vinay Sajipc63619b2010-12-19 12:56:57 +00001:mod:`logging.handlers` --- Logging handlers
2============================================
3
4.. module:: logging.handlers
5 :synopsis: Handlers for the logging module.
6
Vinay Sajipc63619b2010-12-19 12:56:57 +00007.. moduleauthor:: Vinay Sajip <vinay_sajip@red-dove.com>
8.. sectionauthor:: Vinay Sajip <vinay_sajip@red-dove.com>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/logging/handlers.py`
11
Vinay Sajip01094e12010-12-19 13:41:26 +000012.. sidebar:: Important
13
14 This page contains only reference information. For tutorials,
15 please see
16
17 * :ref:`Basic Tutorial <logging-basic-tutorial>`
18 * :ref:`Advanced Tutorial <logging-advanced-tutorial>`
19 * :ref:`Logging Cookbook <logging-cookbook>`
Vinay Sajipc63619b2010-12-19 12:56:57 +000020
Vinay Sajip31b862d2013-09-05 23:01:07 +010021--------------
22
Vinay Sajipc63619b2010-12-19 12:56:57 +000023.. currentmodule:: logging
24
Vinay Sajip01094e12010-12-19 13:41:26 +000025The following useful handlers are provided in the package. Note that three of
26the handlers (:class:`StreamHandler`, :class:`FileHandler` and
27:class:`NullHandler`) are actually defined in the :mod:`logging` module itself,
28but have been documented here along with the other handlers.
29
Vinay Sajipc63619b2010-12-19 12:56:57 +000030.. _stream-handler:
31
32StreamHandler
33^^^^^^^^^^^^^
34
35The :class:`StreamHandler` class, located in the core :mod:`logging` package,
36sends logging output to streams such as *sys.stdout*, *sys.stderr* or any
37file-like object (or, more precisely, any object which supports :meth:`write`
38and :meth:`flush` methods).
39
40
41.. class:: StreamHandler(stream=None)
42
43 Returns a new instance of the :class:`StreamHandler` class. If *stream* is
44 specified, the instance will use it for logging output; otherwise, *sys.stderr*
45 will be used.
46
47
48 .. method:: emit(record)
49
50 If a formatter is specified, it is used to format the record. The record
Andre Delfino3e658302019-06-29 18:59:49 -030051 is then written to the stream followed by :attr:`terminator`. If exception information
Vinay Sajip689b68a2010-12-22 15:04:15 +000052 is present, it is formatted using :func:`traceback.print_exception` and
53 appended to the stream.
Vinay Sajipc63619b2010-12-19 12:56:57 +000054
55
56 .. method:: flush()
57
58 Flushes the stream by calling its :meth:`flush` method. Note that the
Vinay Sajip67f39772013-08-17 00:39:42 +010059 :meth:`close` method is inherited from :class:`~logging.Handler` and so
60 does no output, so an explicit :meth:`flush` call may be needed at times.
Vinay Sajipc63619b2010-12-19 12:56:57 +000061
Vinay Sajip2543f502017-07-30 10:41:45 +010062 .. method:: setStream(stream)
63
64 Sets the instance's stream to the specified value, if it is different.
65 The old stream is flushed before the new stream is set.
66
67 :param stream: The stream that the handler should use.
68
69 :return: the old stream, if the stream was changed, or *None* if it wasn't.
70
Andre Delfino18a2fc62019-06-29 18:57:39 -030071 .. versionadded:: 3.7
Vinay Sajip2543f502017-07-30 10:41:45 +010072
Andre Delfino3e658302019-06-29 18:59:49 -030073 .. attribute:: terminator
Vinay Sajip2543f502017-07-30 10:41:45 +010074
Andre Delfino3e658302019-06-29 18:59:49 -030075 String used as the terminator when writing a formatted record to a stream.
76 Default value is ``'\n'``.
77
78 If you don't want a newline termination, you can set the handler instance's
79 ``terminator`` attribute to the empty string.
80
81 In earlier versions, the terminator was hardcoded as ``'\n'``.
82
83 .. versionadded:: 3.2
Vinay Sajipc63619b2010-12-19 12:56:57 +000084
Vinay Sajip2543f502017-07-30 10:41:45 +010085
Vinay Sajipc63619b2010-12-19 12:56:57 +000086.. _file-handler:
87
88FileHandler
89^^^^^^^^^^^
90
91The :class:`FileHandler` class, located in the core :mod:`logging` package,
92sends logging output to a disk file. It inherits the output functionality from
93:class:`StreamHandler`.
94
95
Vinay Sajipca7b5042019-06-17 17:40:52 +010096.. class:: FileHandler(filename, mode='a', encoding=None, delay=False, errors=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +000097
98 Returns a new instance of the :class:`FileHandler` class. The specified file is
99 opened and used as the stream for logging. If *mode* is not specified,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300100 :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file
Vinay Sajipc63619b2010-12-19 12:56:57 +0000101 with that encoding. If *delay* is true, then file opening is deferred until the
Vinay Sajipca7b5042019-06-17 17:40:52 +0100102 first call to :meth:`emit`. By default, the file grows indefinitely. If
103 *errors* is specified, it's used to determine how encoding errors are handled.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000104
Vinay Sajip638e6222016-07-22 18:23:04 +0100105 .. versionchanged:: 3.6
106 As well as string values, :class:`~pathlib.Path` objects are also accepted
107 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000108
Vinay Sajipca7b5042019-06-17 17:40:52 +0100109 .. versionchanged:: 3.9
110 The *errors* parameter was added.
111
Vinay Sajipc63619b2010-12-19 12:56:57 +0000112 .. method:: close()
113
114 Closes the file.
115
Vinay Sajipc63619b2010-12-19 12:56:57 +0000116 .. method:: emit(record)
117
118 Outputs the record to the file.
119
120
121.. _null-handler:
122
123NullHandler
124^^^^^^^^^^^
125
126.. versionadded:: 3.1
127
128The :class:`NullHandler` class, located in the core :mod:`logging` package,
129does not do any formatting or output. It is essentially a 'no-op' handler
130for use by library developers.
131
132.. class:: NullHandler()
133
134 Returns a new instance of the :class:`NullHandler` class.
135
136 .. method:: emit(record)
137
138 This method does nothing.
139
140 .. method:: handle(record)
141
142 This method does nothing.
143
144 .. method:: createLock()
145
146 This method returns ``None`` for the lock, since there is no
147 underlying I/O to which access needs to be serialized.
148
149
150See :ref:`library-config` for more information on how to use
151:class:`NullHandler`.
152
153.. _watched-file-handler:
154
155WatchedFileHandler
156^^^^^^^^^^^^^^^^^^
157
158.. currentmodule:: logging.handlers
159
160The :class:`WatchedFileHandler` class, located in the :mod:`logging.handlers`
161module, is a :class:`FileHandler` which watches the file it is logging to. If
162the file changes, it is closed and reopened using the file name.
163
164A file change can happen because of usage of programs such as *newsyslog* and
165*logrotate* which perform log file rotation. This handler, intended for use
166under Unix/Linux, watches the file to see if it has changed since the last emit.
167(A file is deemed to have changed if its device or inode have changed.) If the
168file has changed, the old file stream is closed, and the file opened to get a
169new stream.
170
171This handler is not appropriate for use under Windows, because under Windows
172open log files cannot be moved or renamed - logging opens the files with
173exclusive locks - and so there is no need for such a handler. Furthermore,
Vinay Sajip67f39772013-08-17 00:39:42 +0100174*ST_INO* is not supported under Windows; :func:`~os.stat` always returns zero
175for this value.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000176
177
Vinay Sajipca7b5042019-06-17 17:40:52 +0100178.. class:: WatchedFileHandler(filename, mode='a', encoding=None, delay=False, errors=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000179
180 Returns a new instance of the :class:`WatchedFileHandler` class. The specified
181 file is opened and used as the stream for logging. If *mode* is not specified,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300182 :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file
Vinay Sajipc63619b2010-12-19 12:56:57 +0000183 with that encoding. If *delay* is true, then file opening is deferred until the
Vinay Sajipca7b5042019-06-17 17:40:52 +0100184 first call to :meth:`emit`. By default, the file grows indefinitely. If
185 *errors* is provided, it determines how encoding errors are handled.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000186
Vinay Sajip638e6222016-07-22 18:23:04 +0100187 .. versionchanged:: 3.6
188 As well as string values, :class:`~pathlib.Path` objects are also accepted
189 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000190
Vinay Sajipca7b5042019-06-17 17:40:52 +0100191 .. versionchanged:: 3.9
192 The *errors* parameter was added.
193
Vinay Sajip29a14452015-10-01 20:54:41 +0100194 .. method:: reopenIfNeeded()
195
196 Checks to see if the file has changed. If it has, the existing stream is
197 flushed and closed and the file opened again, typically as a precursor to
198 outputting the record to the file.
199
Berker Peksag6f038ad2015-10-07 07:54:23 +0300200 .. versionadded:: 3.6
201
Vinay Sajip29a14452015-10-01 20:54:41 +0100202
Vinay Sajipc63619b2010-12-19 12:56:57 +0000203 .. method:: emit(record)
204
Vinay Sajip29a14452015-10-01 20:54:41 +0100205 Outputs the record to the file, but first calls :meth:`reopenIfNeeded` to
206 reopen the file if it has changed.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000207
Vinay Sajip23b94d02012-01-04 12:02:26 +0000208.. _base-rotating-handler:
209
210BaseRotatingHandler
211^^^^^^^^^^^^^^^^^^^
212
213The :class:`BaseRotatingHandler` class, located in the :mod:`logging.handlers`
214module, is the base class for the rotating file handlers,
215:class:`RotatingFileHandler` and :class:`TimedRotatingFileHandler`. You should
216not need to instantiate this class, but it has attributes and methods you may
217need to override.
218
Vinay Sajipca7b5042019-06-17 17:40:52 +0100219.. class:: BaseRotatingHandler(filename, mode, encoding=None, delay=False, errors=None)
Vinay Sajip23b94d02012-01-04 12:02:26 +0000220
221 The parameters are as for :class:`FileHandler`. The attributes are:
222
223 .. attribute:: namer
224
225 If this attribute is set to a callable, the :meth:`rotation_filename`
226 method delegates to this callable. The parameters passed to the callable
227 are those passed to :meth:`rotation_filename`.
228
229 .. note:: The namer function is called quite a few times during rollover,
230 so it should be as simple and as fast as possible. It should also
231 return the same output every time for a given input, otherwise the
232 rollover behaviour may not work as expected.
233
Miss Islington (bot)f84e2f62021-12-13 17:17:56 -0800234 It's also worth noting that care should be taken when using a namer to
235 preserve certain attributes in the filename which are used during rotation.
236 For example, :class:`RotatingFileHandler` expects to have a set of log files
237 whose names contain successive integers, so that rotation works as expected,
238 and :class:`TimedRotatingFileHandler` deletes old log files (based on the
239 ``backupCount`` parameter passed to the handler's initializer) by determining
240 the oldest files to delete. For this to happen, the filenames should be
241 sortable using the date/time portion of the filename, and a namer needs to
242 respect this. (If a namer is wanted that doesn't respect this scheme, it will
243 need to be used in a subclass of :class:`TimedRotatingFileHandler` which
244 overrides the :meth:`~TimedRotatingFileHandler.getFilesToDelete` method to
245 fit in with the custom naming scheme.)
246
Vinay Sajip23b94d02012-01-04 12:02:26 +0000247 .. versionadded:: 3.3
248
249
250 .. attribute:: BaseRotatingHandler.rotator
251
252 If this attribute is set to a callable, the :meth:`rotate` method
253 delegates to this callable. The parameters passed to the callable are
254 those passed to :meth:`rotate`.
255
256 .. versionadded:: 3.3
257
258 .. method:: BaseRotatingHandler.rotation_filename(default_name)
259
260 Modify the filename of a log file when rotating.
261
262 This is provided so that a custom filename can be provided.
263
264 The default implementation calls the 'namer' attribute of the handler,
265 if it's callable, passing the default name to it. If the attribute isn't
Ezio Melotti226231c2012-01-18 05:40:00 +0200266 callable (the default is ``None``), the name is returned unchanged.
Vinay Sajip23b94d02012-01-04 12:02:26 +0000267
268 :param default_name: The default name for the log file.
269
270 .. versionadded:: 3.3
271
272
273 .. method:: BaseRotatingHandler.rotate(source, dest)
274
275 When rotating, rotate the current log.
276
277 The default implementation calls the 'rotator' attribute of the handler,
278 if it's callable, passing the source and dest arguments to it. If the
Ezio Melotti226231c2012-01-18 05:40:00 +0200279 attribute isn't callable (the default is ``None``), the source is simply
Vinay Sajip23b94d02012-01-04 12:02:26 +0000280 renamed to the destination.
281
282 :param source: The source filename. This is normally the base
Martin Panterd21e0b52015-10-10 10:36:22 +0000283 filename, e.g. 'test.log'.
Vinay Sajip23b94d02012-01-04 12:02:26 +0000284 :param dest: The destination filename. This is normally
285 what the source is rotated to, e.g. 'test.log.1'.
286
287 .. versionadded:: 3.3
288
289The reason the attributes exist is to save you having to subclass - you can use
290the same callables for instances of :class:`RotatingFileHandler` and
291:class:`TimedRotatingFileHandler`. If either the namer or rotator callable
292raises an exception, this will be handled in the same way as any other
293exception during an :meth:`emit` call, i.e. via the :meth:`handleError` method
294of the handler.
295
296If you need to make more significant changes to rotation processing, you can
297override the methods.
298
299For an example, see :ref:`cookbook-rotator-namer`.
300
301
Vinay Sajipc63619b2010-12-19 12:56:57 +0000302.. _rotating-file-handler:
303
304RotatingFileHandler
305^^^^^^^^^^^^^^^^^^^
306
307The :class:`RotatingFileHandler` class, located in the :mod:`logging.handlers`
308module, supports rotation of disk log files.
309
310
Vinay Sajipca7b5042019-06-17 17:40:52 +0100311.. class:: RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False, errors=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000312
313 Returns a new instance of the :class:`RotatingFileHandler` class. The specified
314 file is opened and used as the stream for logging. If *mode* is not specified,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300315 ``'a'`` is used. If *encoding* is not ``None``, it is used to open the file
Vinay Sajipc63619b2010-12-19 12:56:57 +0000316 with that encoding. If *delay* is true, then file opening is deferred until the
Vinay Sajipca7b5042019-06-17 17:40:52 +0100317 first call to :meth:`emit`. By default, the file grows indefinitely. If
318 *errors* is provided, it determines how encoding errors are handled.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000319
320 You can use the *maxBytes* and *backupCount* values to allow the file to
321 :dfn:`rollover` at a predetermined size. When the size is about to be exceeded,
322 the file is closed and a new file is silently opened for output. Rollover occurs
Vinay Sajip53a21eb2016-12-31 11:06:57 +0000323 whenever the current log file is nearly *maxBytes* in length; but if either of
324 *maxBytes* or *backupCount* is zero, rollover never occurs, so you generally want
325 to set *backupCount* to at least 1, and have a non-zero *maxBytes*.
326 When *backupCount* is non-zero, the system will save old log files by appending
327 the extensions '.1', '.2' etc., to the filename. For example, with a *backupCount*
328 of 5 and a base file name of :file:`app.log`, you would get :file:`app.log`,
Vinay Sajipff37cfe2015-01-23 21:19:04 +0000329 :file:`app.log.1`, :file:`app.log.2`, up to :file:`app.log.5`. The file being
330 written to is always :file:`app.log`. When this file is filled, it is closed
331 and renamed to :file:`app.log.1`, and if files :file:`app.log.1`,
Vinay Sajip53a21eb2016-12-31 11:06:57 +0000332 :file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`,
333 :file:`app.log.3` etc. respectively.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000334
Vinay Sajip638e6222016-07-22 18:23:04 +0100335 .. versionchanged:: 3.6
336 As well as string values, :class:`~pathlib.Path` objects are also accepted
337 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000338
Vinay Sajipca7b5042019-06-17 17:40:52 +0100339 .. versionchanged:: 3.9
340 The *errors* parameter was added.
341
Vinay Sajipc63619b2010-12-19 12:56:57 +0000342 .. method:: doRollover()
343
344 Does a rollover, as described above.
345
346
347 .. method:: emit(record)
348
349 Outputs the record to the file, catering for rollover as described
350 previously.
351
352.. _timed-rotating-file-handler:
353
354TimedRotatingFileHandler
355^^^^^^^^^^^^^^^^^^^^^^^^
356
357The :class:`TimedRotatingFileHandler` class, located in the
358:mod:`logging.handlers` module, supports rotation of disk log files at certain
359timed intervals.
360
361
Vinay Sajipca7b5042019-06-17 17:40:52 +0100362.. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None, errors=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000363
364 Returns a new instance of the :class:`TimedRotatingFileHandler` class. The
365 specified file is opened and used as the stream for logging. On rotating it also
366 sets the filename suffix. Rotating happens based on the product of *when* and
367 *interval*.
368
369 You can use the *when* to specify the type of *interval*. The list of possible
370 values is below. Note that they are not case sensitive.
371
Vinay Sajipdd308302016-08-24 17:49:15 +0100372 +----------------+----------------------------+-------------------------+
373 | Value | Type of interval | If/how *atTime* is used |
374 +================+============================+=========================+
375 | ``'S'`` | Seconds | Ignored |
376 +----------------+----------------------------+-------------------------+
377 | ``'M'`` | Minutes | Ignored |
378 +----------------+----------------------------+-------------------------+
379 | ``'H'`` | Hours | Ignored |
380 +----------------+----------------------------+-------------------------+
381 | ``'D'`` | Days | Ignored |
382 +----------------+----------------------------+-------------------------+
383 | ``'W0'-'W6'`` | Weekday (0=Monday) | Used to compute initial |
384 | | | rollover time |
385 +----------------+----------------------------+-------------------------+
386 | ``'midnight'`` | Roll over at midnight, if | Used to compute initial |
387 | | *atTime* not specified, | rollover time |
388 | | else at time *atTime* | |
389 +----------------+----------------------------+-------------------------+
Vinay Sajipc63619b2010-12-19 12:56:57 +0000390
Vinay Sajip832d99b2013-03-08 23:24:30 +0000391 When using weekday-based rotation, specify 'W0' for Monday, 'W1' for
392 Tuesday, and so on up to 'W6' for Sunday. In this case, the value passed for
393 *interval* isn't used.
394
Vinay Sajipc63619b2010-12-19 12:56:57 +0000395 The system will save old log files by appending extensions to the filename.
396 The extensions are date-and-time based, using the strftime format
397 ``%Y-%m-%d_%H-%M-%S`` or a leading portion thereof, depending on the
398 rollover interval.
399
400 When computing the next rollover time for the first time (when the handler
401 is created), the last modification time of an existing log file, or else
402 the current time, is used to compute when the next rotation will occur.
403
404 If the *utc* argument is true, times in UTC will be used; otherwise
405 local time is used.
406
407 If *backupCount* is nonzero, at most *backupCount* files
408 will be kept, and if more would be created when rollover occurs, the oldest
409 one is deleted. The deletion logic uses the interval to determine which
410 files to delete, so changing the interval may leave old files lying around.
411
412 If *delay* is true, then file opening is deferred until the first call to
413 :meth:`emit`.
414
Vinay Sajipa7130792013-04-12 17:04:23 +0100415 If *atTime* is not ``None``, it must be a ``datetime.time`` instance which
416 specifies the time of day when rollover occurs, for the cases where rollover
Vinay Sajipdd308302016-08-24 17:49:15 +0100417 is set to happen "at midnight" or "on a particular weekday". Note that in
418 these cases, the *atTime* value is effectively used to compute the *initial*
419 rollover, and subsequent rollovers would be calculated via the normal
420 interval calculation.
421
Vinay Sajipca7b5042019-06-17 17:40:52 +0100422 If *errors* is specified, it's used to determine how encoding errors are
423 handled.
424
Vinay Sajipdd308302016-08-24 17:49:15 +0100425 .. note:: Calculation of the initial rollover time is done when the handler
426 is initialised. Calculation of subsequent rollover times is done only
427 when rollover occurs, and rollover occurs only when emitting output. If
428 this is not kept in mind, it might lead to some confusion. For example,
429 if an interval of "every minute" is set, that does not mean you will
430 always see log files with times (in the filename) separated by a minute;
431 if, during application execution, logging output is generated more
432 frequently than once a minute, *then* you can expect to see log files
433 with times separated by a minute. If, on the other hand, logging messages
434 are only output once every five minutes (say), then there will be gaps in
435 the file times corresponding to the minutes where no output (and hence no
436 rollover) occurred.
Vinay Sajipa7130792013-04-12 17:04:23 +0100437
438 .. versionchanged:: 3.4
439 *atTime* parameter was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000440
Vinay Sajip638e6222016-07-22 18:23:04 +0100441 .. versionchanged:: 3.6
442 As well as string values, :class:`~pathlib.Path` objects are also accepted
443 for the *filename* argument.
444
Vinay Sajipca7b5042019-06-17 17:40:52 +0100445 .. versionchanged:: 3.9
446 The *errors* parameter was added.
447
Vinay Sajipc63619b2010-12-19 12:56:57 +0000448 .. method:: doRollover()
449
450 Does a rollover, as described above.
451
Vinay Sajipc63619b2010-12-19 12:56:57 +0000452 .. method:: emit(record)
453
454 Outputs the record to the file, catering for rollover as described above.
455
Miss Islington (bot)f84e2f62021-12-13 17:17:56 -0800456 .. method:: getFilesToDelete()
457
458 Returns a list of filenames which should be deleted as part of rollover. These
459 are the absolute paths of the oldest backup log files written by the handler.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000460
461.. _socket-handler:
462
463SocketHandler
464^^^^^^^^^^^^^
465
466The :class:`SocketHandler` class, located in the :mod:`logging.handlers` module,
467sends logging output to a network socket. The base class uses a TCP socket.
468
469
470.. class:: SocketHandler(host, port)
471
472 Returns a new instance of the :class:`SocketHandler` class intended to
473 communicate with a remote machine whose address is given by *host* and *port*.
474
Vinay Sajip5421f352013-09-27 18:18:28 +0100475 .. versionchanged:: 3.4
476 If ``port`` is specified as ``None``, a Unix domain socket is created
477 using the value in ``host`` - otherwise, a TCP socket is created.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000478
479 .. method:: close()
480
481 Closes the socket.
482
483
484 .. method:: emit()
485
486 Pickles the record's attribute dictionary and writes it to the socket in
487 binary format. If there is an error with the socket, silently drops the
488 packet. If the connection was previously lost, re-establishes the
489 connection. To unpickle the record at the receiving end into a
Vinay Sajip67f39772013-08-17 00:39:42 +0100490 :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
491 function.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000492
493
494 .. method:: handleError()
495
496 Handles an error which has occurred during :meth:`emit`. The most likely
497 cause is a lost connection. Closes the socket so that we can retry on the
498 next event.
499
500
501 .. method:: makeSocket()
502
503 This is a factory method which allows subclasses to define the precise
504 type of socket they want. The default implementation creates a TCP socket
505 (:const:`socket.SOCK_STREAM`).
506
507
508 .. method:: makePickle(record)
509
510 Pickles the record's attribute dictionary in binary format with a length
Vinay Sajipf06b5692019-06-19 15:29:57 +0100511 prefix, and returns it ready for transmission across the socket. The
512 details of this operation are equivalent to::
513
514 data = pickle.dumps(record_attr_dict, 1)
515 datalen = struct.pack('>L', len(data))
516 return datalen + data
Vinay Sajipc63619b2010-12-19 12:56:57 +0000517
518 Note that pickles aren't completely secure. If you are concerned about
519 security, you may want to override this method to implement a more secure
520 mechanism. For example, you can sign pickles using HMAC and then verify
521 them on the receiving end, or alternatively you can disable unpickling of
522 global objects on the receiving end.
523
Georg Brandl08e278a2011-02-15 12:44:43 +0000524
Vinay Sajipc63619b2010-12-19 12:56:57 +0000525 .. method:: send(packet)
526
Vinay Sajipf06b5692019-06-19 15:29:57 +0100527 Send a pickled byte-string *packet* to the socket. The format of the sent
528 byte-string is as described in the documentation for
529 :meth:`~SocketHandler.makePickle`.
530
531 This function allows for partial sends, which can happen when the network
532 is busy.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000533
Georg Brandl08e278a2011-02-15 12:44:43 +0000534
Georg Brandldbb95852011-02-15 12:41:17 +0000535 .. method:: createSocket()
536
537 Tries to create a socket; on failure, uses an exponential back-off
Donald Stufft8b852f12014-05-20 12:58:38 -0400538 algorithm. On initial failure, the handler will drop the message it was
Georg Brandldbb95852011-02-15 12:41:17 +0000539 trying to send. When subsequent messages are handled by the same
540 instance, it will not try connecting until some time has passed. The
541 default parameters are such that the initial delay is one second, and if
542 after that delay the connection still can't be made, the handler will
543 double the delay each time up to a maximum of 30 seconds.
544
545 This behaviour is controlled by the following handler attributes:
546
547 * ``retryStart`` (initial delay, defaulting to 1.0 seconds).
548 * ``retryFactor`` (multiplier, defaulting to 2.0).
549 * ``retryMax`` (maximum delay, defaulting to 30.0 seconds).
550
551 This means that if the remote listener starts up *after* the handler has
552 been used, you could lose messages (since the handler won't even attempt
553 a connection until the delay has elapsed, but just silently drop messages
554 during the delay period).
Georg Brandl08e278a2011-02-15 12:44:43 +0000555
Vinay Sajipc63619b2010-12-19 12:56:57 +0000556
557.. _datagram-handler:
558
559DatagramHandler
560^^^^^^^^^^^^^^^
561
562The :class:`DatagramHandler` class, located in the :mod:`logging.handlers`
563module, inherits from :class:`SocketHandler` to support sending logging messages
564over UDP sockets.
565
566
567.. class:: DatagramHandler(host, port)
568
569 Returns a new instance of the :class:`DatagramHandler` class intended to
570 communicate with a remote machine whose address is given by *host* and *port*.
571
Vinay Sajip5421f352013-09-27 18:18:28 +0100572 .. versionchanged:: 3.4
573 If ``port`` is specified as ``None``, a Unix domain socket is created
Mike DePalatis233de022018-03-30 03:36:06 -0400574 using the value in ``host`` - otherwise, a UDP socket is created.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000575
576 .. method:: emit()
577
578 Pickles the record's attribute dictionary and writes it to the socket in
579 binary format. If there is an error with the socket, silently drops the
580 packet. To unpickle the record at the receiving end into a
Vinay Sajip67f39772013-08-17 00:39:42 +0100581 :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
582 function.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000583
584
585 .. method:: makeSocket()
586
587 The factory method of :class:`SocketHandler` is here overridden to create
588 a UDP socket (:const:`socket.SOCK_DGRAM`).
589
590
591 .. method:: send(s)
592
Vinay Sajipf06b5692019-06-19 15:29:57 +0100593 Send a pickled byte-string to a socket. The format of the sent byte-string
594 is as described in the documentation for :meth:`SocketHandler.makePickle`.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000595
596
597.. _syslog-handler:
598
599SysLogHandler
600^^^^^^^^^^^^^
601
602The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module,
603supports sending logging messages to a remote or local Unix syslog.
604
605
606.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
607
608 Returns a new instance of the :class:`SysLogHandler` class intended to
609 communicate with a remote Unix machine whose address is given by *address* in
610 the form of a ``(host, port)`` tuple. If *address* is not specified,
611 ``('localhost', 514)`` is used. The address is used to open a socket. An
612 alternative to providing a ``(host, port)`` tuple is providing an address as a
613 string, for example '/dev/log'. In this case, a Unix domain socket is used to
614 send the message to the syslog. If *facility* is not specified,
615 :const:`LOG_USER` is used. The type of socket opened depends on the
616 *socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus
617 opens a UDP socket. To open a TCP socket (for use with the newer syslog
618 daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`.
619
620 Note that if your server is not listening on UDP port 514,
621 :class:`SysLogHandler` may appear not to work. In that case, check what
622 address you should be using for a domain socket - it's system dependent.
623 For example, on Linux it's usually '/dev/log' but on OS/X it's
624 '/var/run/syslog'. You'll need to check your platform and use the
625 appropriate address (you may need to do this check at runtime if your
626 application needs to run on several platforms). On Windows, you pretty
627 much have to use the UDP option.
628
629 .. versionchanged:: 3.2
630 *socktype* was added.
631
632
633 .. method:: close()
634
635 Closes the socket to the remote host.
636
637
638 .. method:: emit(record)
639
640 The record is formatted, and then sent to the syslog server. If exception
641 information is present, it is *not* sent to the server.
642
Vinay Sajip645e4582011-06-10 18:52:50 +0100643 .. versionchanged:: 3.2.1
644 (See: :issue:`12168`.) In earlier versions, the message sent to the
645 syslog daemons was always terminated with a NUL byte, because early
646 versions of these daemons expected a NUL terminated message - even
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300647 though it's not in the relevant specification (:rfc:`5424`). More recent
Vinay Sajip645e4582011-06-10 18:52:50 +0100648 versions of these daemons don't expect the NUL byte but strip it off
649 if it's there, and even more recent daemons (which adhere more closely
650 to RFC 5424) pass the NUL byte on as part of the message.
651
652 To enable easier handling of syslog messages in the face of all these
653 differing daemon behaviours, the appending of the NUL byte has been
654 made configurable, through the use of a class-level attribute,
655 ``append_nul``. This defaults to ``True`` (preserving the existing
656 behaviour) but can be set to ``False`` on a ``SysLogHandler`` instance
657 in order for that instance to *not* append the NUL terminator.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000658
Vinay Sajip2353e352011-06-27 15:40:06 +0100659 .. versionchanged:: 3.3
660 (See: :issue:`12419`.) In earlier versions, there was no facility for
661 an "ident" or "tag" prefix to identify the source of the message. This
662 can now be specified using a class-level attribute, defaulting to
663 ``""`` to preserve existing behaviour, but which can be overridden on
664 a ``SysLogHandler`` instance in order for that instance to prepend
665 the ident to every message handled. Note that the provided ident must
666 be text, not bytes, and is prepended to the message exactly as is.
667
Vinay Sajipc63619b2010-12-19 12:56:57 +0000668 .. method:: encodePriority(facility, priority)
669
670 Encodes the facility and priority into an integer. You can pass in strings
671 or integers - if strings are passed, internal mapping dictionaries are
672 used to convert them to integers.
673
674 The symbolic ``LOG_`` values are defined in :class:`SysLogHandler` and
675 mirror the values defined in the ``sys/syslog.h`` header file.
676
677 **Priorities**
678
679 +--------------------------+---------------+
680 | Name (string) | Symbolic value|
681 +==========================+===============+
682 | ``alert`` | LOG_ALERT |
683 +--------------------------+---------------+
684 | ``crit`` or ``critical`` | LOG_CRIT |
685 +--------------------------+---------------+
686 | ``debug`` | LOG_DEBUG |
687 +--------------------------+---------------+
688 | ``emerg`` or ``panic`` | LOG_EMERG |
689 +--------------------------+---------------+
690 | ``err`` or ``error`` | LOG_ERR |
691 +--------------------------+---------------+
692 | ``info`` | LOG_INFO |
693 +--------------------------+---------------+
694 | ``notice`` | LOG_NOTICE |
695 +--------------------------+---------------+
696 | ``warn`` or ``warning`` | LOG_WARNING |
697 +--------------------------+---------------+
698
699 **Facilities**
700
701 +---------------+---------------+
702 | Name (string) | Symbolic value|
703 +===============+===============+
704 | ``auth`` | LOG_AUTH |
705 +---------------+---------------+
706 | ``authpriv`` | LOG_AUTHPRIV |
707 +---------------+---------------+
708 | ``cron`` | LOG_CRON |
709 +---------------+---------------+
710 | ``daemon`` | LOG_DAEMON |
711 +---------------+---------------+
712 | ``ftp`` | LOG_FTP |
713 +---------------+---------------+
714 | ``kern`` | LOG_KERN |
715 +---------------+---------------+
716 | ``lpr`` | LOG_LPR |
717 +---------------+---------------+
718 | ``mail`` | LOG_MAIL |
719 +---------------+---------------+
720 | ``news`` | LOG_NEWS |
721 +---------------+---------------+
722 | ``syslog`` | LOG_SYSLOG |
723 +---------------+---------------+
724 | ``user`` | LOG_USER |
725 +---------------+---------------+
726 | ``uucp`` | LOG_UUCP |
727 +---------------+---------------+
728 | ``local0`` | LOG_LOCAL0 |
729 +---------------+---------------+
730 | ``local1`` | LOG_LOCAL1 |
731 +---------------+---------------+
732 | ``local2`` | LOG_LOCAL2 |
733 +---------------+---------------+
734 | ``local3`` | LOG_LOCAL3 |
735 +---------------+---------------+
736 | ``local4`` | LOG_LOCAL4 |
737 +---------------+---------------+
738 | ``local5`` | LOG_LOCAL5 |
739 +---------------+---------------+
740 | ``local6`` | LOG_LOCAL6 |
741 +---------------+---------------+
742 | ``local7`` | LOG_LOCAL7 |
743 +---------------+---------------+
744
745 .. method:: mapPriority(levelname)
746
747 Maps a logging level name to a syslog priority name.
748 You may need to override this if you are using custom levels, or
749 if the default algorithm is not suitable for your needs. The
750 default algorithm maps ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR`` and
751 ``CRITICAL`` to the equivalent syslog names, and all other level
752 names to 'warning'.
753
754.. _nt-eventlog-handler:
755
756NTEventLogHandler
757^^^^^^^^^^^^^^^^^
758
759The :class:`NTEventLogHandler` class, located in the :mod:`logging.handlers`
760module, supports sending logging messages to a local Windows NT, Windows 2000 or
761Windows XP event log. Before you can use it, you need Mark Hammond's Win32
762extensions for Python installed.
763
764
765.. class:: NTEventLogHandler(appname, dllname=None, logtype='Application')
766
767 Returns a new instance of the :class:`NTEventLogHandler` class. The *appname* is
768 used to define the application name as it appears in the event log. An
769 appropriate registry entry is created using this name. The *dllname* should give
770 the fully qualified pathname of a .dll or .exe which contains message
771 definitions to hold in the log (if not specified, ``'win32service.pyd'`` is used
772 - this is installed with the Win32 extensions and contains some basic
773 placeholder message definitions. Note that use of these placeholders will make
774 your event logs big, as the entire message source is held in the log. If you
775 want slimmer logs, you have to pass in the name of your own .dll or .exe which
776 contains the message definitions you want to use in the event log). The
777 *logtype* is one of ``'Application'``, ``'System'`` or ``'Security'``, and
778 defaults to ``'Application'``.
779
780
781 .. method:: close()
782
783 At this point, you can remove the application name from the registry as a
784 source of event log entries. However, if you do this, you will not be able
785 to see the events as you intended in the Event Log Viewer - it needs to be
786 able to access the registry to get the .dll name. The current version does
787 not do this.
788
789
790 .. method:: emit(record)
791
792 Determines the message ID, event category and event type, and then logs
793 the message in the NT event log.
794
795
796 .. method:: getEventCategory(record)
797
798 Returns the event category for the record. Override this if you want to
799 specify your own categories. This version returns 0.
800
801
802 .. method:: getEventType(record)
803
804 Returns the event type for the record. Override this if you want to
805 specify your own types. This version does a mapping using the handler's
806 typemap attribute, which is set up in :meth:`__init__` to a dictionary
807 which contains mappings for :const:`DEBUG`, :const:`INFO`,
808 :const:`WARNING`, :const:`ERROR` and :const:`CRITICAL`. If you are using
809 your own levels, you will either need to override this method or place a
810 suitable dictionary in the handler's *typemap* attribute.
811
812
813 .. method:: getMessageID(record)
814
815 Returns the message ID for the record. If you are using your own messages,
816 you could do this by having the *msg* passed to the logger being an ID
817 rather than a format string. Then, in here, you could use a dictionary
818 lookup to get the message ID. This version returns 1, which is the base
819 message ID in :file:`win32service.pyd`.
820
821.. _smtp-handler:
822
823SMTPHandler
824^^^^^^^^^^^
825
826The :class:`SMTPHandler` class, located in the :mod:`logging.handlers` module,
827supports sending logging messages to an email address via SMTP.
828
829
Vinay Sajip38a12af2012-03-26 17:17:39 +0100830.. class:: SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000831
832 Returns a new instance of the :class:`SMTPHandler` class. The instance is
833 initialized with the from and to addresses and subject line of the email. The
834 *toaddrs* should be a list of strings. To specify a non-standard SMTP port, use
835 the (host, port) tuple format for the *mailhost* argument. If you use a string,
836 the standard SMTP port is used. If your SMTP server requires authentication, you
837 can specify a (username, password) tuple for the *credentials* argument.
838
Vinay Sajip95259562011-08-01 11:31:52 +0100839 To specify the use of a secure protocol (TLS), pass in a tuple to the
840 *secure* argument. This will only be used when authentication credentials are
841 supplied. The tuple should be either an empty tuple, or a single-value tuple
842 with the name of a keyfile, or a 2-value tuple with the names of the keyfile
843 and certificate file. (This tuple is passed to the
844 :meth:`smtplib.SMTP.starttls` method.)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000845
Vinay Sajip38a12af2012-03-26 17:17:39 +0100846 A timeout can be specified for communication with the SMTP server using the
847 *timeout* argument.
848
849 .. versionadded:: 3.3
850 The *timeout* argument was added.
851
Vinay Sajipc63619b2010-12-19 12:56:57 +0000852 .. method:: emit(record)
853
854 Formats the record and sends it to the specified addressees.
855
856
857 .. method:: getSubject(record)
858
859 If you want to specify a subject line which is record-dependent, override
860 this method.
861
862.. _memory-handler:
863
864MemoryHandler
865^^^^^^^^^^^^^
866
867The :class:`MemoryHandler` class, located in the :mod:`logging.handlers` module,
868supports buffering of logging records in memory, periodically flushing them to a
869:dfn:`target` handler. Flushing occurs whenever the buffer is full, or when an
870event of a certain severity or greater is seen.
871
872:class:`MemoryHandler` is a subclass of the more general
873:class:`BufferingHandler`, which is an abstract class. This buffers logging
874records in memory. Whenever each record is added to the buffer, a check is made
875by calling :meth:`shouldFlush` to see if the buffer should be flushed. If it
Vinay Sajip8ece80f2012-03-26 17:09:58 +0100876should, then :meth:`flush` is expected to do the flushing.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000877
878
879.. class:: BufferingHandler(capacity)
880
Vinay Sajip84de34e2019-07-01 12:41:21 +0100881 Initializes the handler with a buffer of the specified capacity. Here,
882 *capacity* means the number of logging records buffered.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000883
884
885 .. method:: emit(record)
886
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +0200887 Append the record to the buffer. If :meth:`shouldFlush` returns true,
888 call :meth:`flush` to process the buffer.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000889
890
891 .. method:: flush()
892
893 You can override this to implement custom flushing behavior. This version
894 just zaps the buffer to empty.
895
896
897 .. method:: shouldFlush(record)
898
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +0200899 Return ``True`` if the buffer is up to capacity. This method can be
Vinay Sajipc63619b2010-12-19 12:56:57 +0000900 overridden to implement custom flushing strategies.
901
902
Vinay Sajipcccf6062016-07-22 16:27:31 +0100903.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000904
905 Returns a new instance of the :class:`MemoryHandler` class. The instance is
Vinay Sajip84de34e2019-07-01 12:41:21 +0100906 initialized with a buffer size of *capacity* (number of records buffered).
907 If *flushLevel* is not specified, :const:`ERROR` is used. If no *target* is
908 specified, the target will need to be set using :meth:`setTarget` before this
909 handler does anything useful. If *flushOnClose* is specified as ``False``,
910 then the buffer is *not* flushed when the handler is closed. If not specified
911 or specified as ``True``, the previous behaviour of flushing the buffer will
912 occur when the handler is closed.
Vinay Sajipcccf6062016-07-22 16:27:31 +0100913
914 .. versionchanged:: 3.6
915 The *flushOnClose* parameter was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000916
917
918 .. method:: close()
919
Ezio Melotti226231c2012-01-18 05:40:00 +0200920 Calls :meth:`flush`, sets the target to ``None`` and clears the
Vinay Sajipc63619b2010-12-19 12:56:57 +0000921 buffer.
922
923
924 .. method:: flush()
925
926 For a :class:`MemoryHandler`, flushing means just sending the buffered
927 records to the target, if there is one. The buffer is also cleared when
928 this happens. Override if you want different behavior.
929
930
931 .. method:: setTarget(target)
932
933 Sets the target handler for this handler.
934
935
936 .. method:: shouldFlush(record)
937
938 Checks for buffer full or a record at the *flushLevel* or higher.
939
940
941.. _http-handler:
942
943HTTPHandler
944^^^^^^^^^^^
945
946The :class:`HTTPHandler` class, located in the :mod:`logging.handlers` module,
Miss Islington (bot)6fc1efa2021-07-26 15:34:32 -0700947supports sending logging messages to a web server, using either ``GET`` or
Vinay Sajipc63619b2010-12-19 12:56:57 +0000948``POST`` semantics.
949
950
Benjamin Peterson43052a12014-11-23 20:36:44 -0600951.. class:: HTTPHandler(host, url, method='GET', secure=False, credentials=None, context=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000952
953 Returns a new instance of the :class:`HTTPHandler` class. The *host* can be
Benjamin Peterson43052a12014-11-23 20:36:44 -0600954 of the form ``host:port``, should you need to use a specific port number. If
955 no *method* is specified, ``GET`` is used. If *secure* is true, a HTTPS
956 connection will be used. The *context* parameter may be set to a
957 :class:`ssl.SSLContext` instance to configure the SSL settings used for the
958 HTTPS connection. If *credentials* is specified, it should be a 2-tuple
959 consisting of userid and password, which will be placed in a HTTP
Vinay Sajipc63619b2010-12-19 12:56:57 +0000960 'Authorization' header using Basic authentication. If you specify
961 credentials, you should also specify secure=True so that your userid and
962 password are not passed in cleartext across the wire.
963
Benjamin Petersona90e92d2014-11-23 20:38:37 -0600964 .. versionchanged:: 3.5
Benjamin Peterson43052a12014-11-23 20:36:44 -0600965 The *context* parameter was added.
966
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100967 .. method:: mapLogRecord(record)
968
969 Provides a dictionary, based on ``record``, which is to be URL-encoded
970 and sent to the web server. The default implementation just returns
971 ``record.__dict__``. This method can be overridden if e.g. only a
972 subset of :class:`~logging.LogRecord` is to be sent to the web server, or
973 if more specific customization of what's sent to the server is required.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000974
975 .. method:: emit(record)
976
Miss Islington (bot)6fc1efa2021-07-26 15:34:32 -0700977 Sends the record to the web server as a URL-encoded dictionary. The
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100978 :meth:`mapLogRecord` method is used to convert the record to the
979 dictionary to be sent.
980
Miss Islington (bot)6fc1efa2021-07-26 15:34:32 -0700981 .. note:: Since preparing a record for sending it to a web server is not
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100982 the same as a generic formatting operation, using
983 :meth:`~logging.Handler.setFormatter` to specify a
984 :class:`~logging.Formatter` for a :class:`HTTPHandler` has no effect.
985 Instead of calling :meth:`~logging.Handler.format`, this handler calls
986 :meth:`mapLogRecord` and then :func:`urllib.parse.urlencode` to encode the
Miss Islington (bot)6fc1efa2021-07-26 15:34:32 -0700987 dictionary in a form suitable for sending to a web server.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000988
989
990.. _queue-handler:
991
992
993QueueHandler
994^^^^^^^^^^^^
995
996.. versionadded:: 3.2
997
998The :class:`QueueHandler` class, located in the :mod:`logging.handlers` module,
999supports sending logging messages to a queue, such as those implemented in the
1000:mod:`queue` or :mod:`multiprocessing` modules.
1001
1002Along with the :class:`QueueListener` class, :class:`QueueHandler` can be used
1003to let handlers do their work on a separate thread from the one which does the
Miss Islington (bot)6fc1efa2021-07-26 15:34:32 -07001004logging. This is important in web applications and also other service
Vinay Sajipc63619b2010-12-19 12:56:57 +00001005applications where threads servicing clients need to respond as quickly as
1006possible, while any potentially slow operations (such as sending an email via
1007:class:`SMTPHandler`) are done on a separate thread.
1008
1009.. class:: QueueHandler(queue)
1010
1011 Returns a new instance of the :class:`QueueHandler` class. The instance is
Vinay Sajipe6b64b72019-07-01 18:45:07 +01001012 initialized with the queue to send messages to. The *queue* can be any
1013 queue-like object; it's used as-is by the :meth:`enqueue` method, which
1014 needs to know how to send messages to it. The queue is not *required* to
1015 have the task tracking API, which means that you can use
1016 :class:`~queue.SimpleQueue` instances for *queue*.
Vinay Sajipc63619b2010-12-19 12:56:57 +00001017
1018
1019 .. method:: emit(record)
1020
Vinay Sajip0f4e8132019-07-01 20:45:01 +01001021 Enqueues the result of preparing the LogRecord. Should an exception
1022 occur (e.g. because a bounded queue has filled up), the
1023 :meth:`~logging.Handler.handleError` method is called to handle the
1024 error. This can result in the record silently being dropped (if
1025 :attr:`logging.raiseExceptions` is ``False``) or a message printed to
1026 ``sys.stderr`` (if :attr:`logging.raiseExceptions` is ``True``).
Vinay Sajipc63619b2010-12-19 12:56:57 +00001027
1028 .. method:: prepare(record)
1029
1030 Prepares a record for queuing. The object returned by this
1031 method is enqueued.
1032
Cheryl Sabellad345bb42018-09-25 19:00:08 -04001033 The base implementation formats the record to merge the message,
1034 arguments, and exception information, if present. It also
1035 removes unpickleable items from the record in-place.
Vinay Sajipc63619b2010-12-19 12:56:57 +00001036
1037 You might want to override this method if you want to convert
1038 the record to a dict or JSON string, or send a modified copy
1039 of the record while leaving the original intact.
1040
1041 .. method:: enqueue(record)
1042
1043 Enqueues the record on the queue using ``put_nowait()``; you may
1044 want to override this if you want to use blocking behaviour, or a
Vinay Sajip9c10d6b2013-11-15 20:58:13 +00001045 timeout, or a customized queue implementation.
Vinay Sajipc63619b2010-12-19 12:56:57 +00001046
1047
1048
Éric Araujo5eada942011-08-19 00:41:23 +02001049.. _queue-listener:
Vinay Sajipc63619b2010-12-19 12:56:57 +00001050
1051QueueListener
1052^^^^^^^^^^^^^
1053
1054.. versionadded:: 3.2
1055
1056The :class:`QueueListener` class, located in the :mod:`logging.handlers`
1057module, supports receiving logging messages from a queue, such as those
1058implemented in the :mod:`queue` or :mod:`multiprocessing` modules. The
1059messages are received from a queue in an internal thread and passed, on
1060the same thread, to one or more handlers for processing. While
1061:class:`QueueListener` is not itself a handler, it is documented here
1062because it works hand-in-hand with :class:`QueueHandler`.
1063
1064Along with the :class:`QueueHandler` class, :class:`QueueListener` can be used
1065to let handlers do their work on a separate thread from the one which does the
Miss Islington (bot)6fc1efa2021-07-26 15:34:32 -07001066logging. This is important in web applications and also other service
Vinay Sajipc63619b2010-12-19 12:56:57 +00001067applications where threads servicing clients need to respond as quickly as
1068possible, while any potentially slow operations (such as sending an email via
1069:class:`SMTPHandler`) are done on a separate thread.
1070
Vinay Sajip365701a2015-02-09 19:49:00 +00001071.. class:: QueueListener(queue, *handlers, respect_handler_level=False)
Vinay Sajipc63619b2010-12-19 12:56:57 +00001072
1073 Returns a new instance of the :class:`QueueListener` class. The instance is
1074 initialized with the queue to send messages to and a list of handlers which
Martin Panter8f137832017-01-14 08:24:20 +00001075 will handle entries placed on the queue. The queue can be any queue-like
1076 object; it's passed as-is to the :meth:`dequeue` method, which needs
Vinay Sajipe6b64b72019-07-01 18:45:07 +01001077 to know how to get messages from it. The queue is not *required* to have the
1078 task tracking API (though it's used if available), which means that you can
1079 use :class:`~queue.SimpleQueue` instances for *queue*.
1080
1081 If ``respect_handler_level`` is ``True``, a handler's level is respected
1082 (compared with the level for the message) when deciding whether to pass
1083 messages to that handler; otherwise, the behaviour is as in previous Python
1084 versions - to always pass each message to each handler.
Vinay Sajip365701a2015-02-09 19:49:00 +00001085
1086 .. versionchanged:: 3.5
wwuckefd57412019-09-11 16:44:37 +10001087 The ``respect_handler_level`` argument was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +00001088
1089 .. method:: dequeue(block)
1090
1091 Dequeues a record and return it, optionally blocking.
1092
1093 The base implementation uses ``get()``. You may want to override this
1094 method if you want to use timeouts or work with custom queue
1095 implementations.
1096
1097 .. method:: prepare(record)
1098
1099 Prepare a record for handling.
1100
1101 This implementation just returns the passed-in record. You may want to
1102 override this method if you need to do any custom marshalling or
1103 manipulation of the record before passing it to the handlers.
1104
1105 .. method:: handle(record)
1106
1107 Handle a record.
1108
1109 This just loops through the handlers offering them the record
1110 to handle. The actual object passed to the handlers is that which
1111 is returned from :meth:`prepare`.
1112
1113 .. method:: start()
1114
1115 Starts the listener.
1116
1117 This starts up a background thread to monitor the queue for
1118 LogRecords to process.
1119
1120 .. method:: stop()
1121
1122 Stops the listener.
1123
1124 This asks the thread to terminate, and then waits for it to do so.
1125 Note that if you don't call this before your application exits, there
1126 may be some records still left on the queue, which won't be processed.
1127
Vinay Sajipa29a9dd2011-02-25 16:05:26 +00001128 .. method:: enqueue_sentinel()
1129
1130 Writes a sentinel to the queue to tell the listener to quit. This
1131 implementation uses ``put_nowait()``. You may want to override this
1132 method if you want to use timeouts or work with custom queue
1133 implementations.
1134
1135 .. versionadded:: 3.3
1136
Vinay Sajipc63619b2010-12-19 12:56:57 +00001137
1138.. seealso::
1139
1140 Module :mod:`logging`
1141 API reference for the logging module.
1142
1143 Module :mod:`logging.config`
1144 Configuration API for the logging module.
1145
1146