blob: 822f82dffcfdc25ad3f21444b59fc82a99adf090 [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
Vinay Sajip689b68a2010-12-22 15:04:15 +000051 is then written to the stream with a terminator. If exception information
52 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
71 .. versionadded:: 3.7
72
73
Vinay Sajipc63619b2010-12-19 12:56:57 +000074.. versionchanged:: 3.2
75 The ``StreamHandler`` class now has a ``terminator`` attribute, default
76 value ``'\n'``, which is used as the terminator when writing a formatted
77 record to a stream. If you don't want this newline termination, you can
78 set the handler instance's ``terminator`` attribute to the empty string.
Vinay Sajip689b68a2010-12-22 15:04:15 +000079 In earlier versions, the terminator was hardcoded as ``'\n'``.
Vinay Sajipc63619b2010-12-19 12:56:57 +000080
Vinay Sajip2543f502017-07-30 10:41:45 +010081
Vinay Sajipc63619b2010-12-19 12:56:57 +000082.. _file-handler:
83
84FileHandler
85^^^^^^^^^^^
86
87The :class:`FileHandler` class, located in the core :mod:`logging` package,
88sends logging output to a disk file. It inherits the output functionality from
89:class:`StreamHandler`.
90
91
Vinay Sajipca7b5042019-06-17 17:40:52 +010092.. class:: FileHandler(filename, mode='a', encoding=None, delay=False, errors=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +000093
94 Returns a new instance of the :class:`FileHandler` class. The specified file is
95 opened and used as the stream for logging. If *mode* is not specified,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +030096 :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file
Vinay Sajipc63619b2010-12-19 12:56:57 +000097 with that encoding. If *delay* is true, then file opening is deferred until the
Vinay Sajipca7b5042019-06-17 17:40:52 +010098 first call to :meth:`emit`. By default, the file grows indefinitely. If
99 *errors* is specified, it's used to determine how encoding errors are handled.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000100
Vinay Sajip638e6222016-07-22 18:23:04 +0100101 .. versionchanged:: 3.6
102 As well as string values, :class:`~pathlib.Path` objects are also accepted
103 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000104
Vinay Sajipca7b5042019-06-17 17:40:52 +0100105 .. versionchanged:: 3.9
106 The *errors* parameter was added.
107
Vinay Sajipc63619b2010-12-19 12:56:57 +0000108 .. method:: close()
109
110 Closes the file.
111
112
113 .. method:: emit(record)
114
115 Outputs the record to the file.
116
117
118.. _null-handler:
119
120NullHandler
121^^^^^^^^^^^
122
123.. versionadded:: 3.1
124
125The :class:`NullHandler` class, located in the core :mod:`logging` package,
126does not do any formatting or output. It is essentially a 'no-op' handler
127for use by library developers.
128
129.. class:: NullHandler()
130
131 Returns a new instance of the :class:`NullHandler` class.
132
133 .. method:: emit(record)
134
135 This method does nothing.
136
137 .. method:: handle(record)
138
139 This method does nothing.
140
141 .. method:: createLock()
142
143 This method returns ``None`` for the lock, since there is no
144 underlying I/O to which access needs to be serialized.
145
146
147See :ref:`library-config` for more information on how to use
148:class:`NullHandler`.
149
150.. _watched-file-handler:
151
152WatchedFileHandler
153^^^^^^^^^^^^^^^^^^
154
155.. currentmodule:: logging.handlers
156
157The :class:`WatchedFileHandler` class, located in the :mod:`logging.handlers`
158module, is a :class:`FileHandler` which watches the file it is logging to. If
159the file changes, it is closed and reopened using the file name.
160
161A file change can happen because of usage of programs such as *newsyslog* and
162*logrotate* which perform log file rotation. This handler, intended for use
163under Unix/Linux, watches the file to see if it has changed since the last emit.
164(A file is deemed to have changed if its device or inode have changed.) If the
165file has changed, the old file stream is closed, and the file opened to get a
166new stream.
167
168This handler is not appropriate for use under Windows, because under Windows
169open log files cannot be moved or renamed - logging opens the files with
170exclusive locks - and so there is no need for such a handler. Furthermore,
Vinay Sajip67f39772013-08-17 00:39:42 +0100171*ST_INO* is not supported under Windows; :func:`~os.stat` always returns zero
172for this value.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000173
174
Vinay Sajipca7b5042019-06-17 17:40:52 +0100175.. class:: WatchedFileHandler(filename, mode='a', encoding=None, delay=False, errors=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000176
177 Returns a new instance of the :class:`WatchedFileHandler` class. The specified
178 file is opened and used as the stream for logging. If *mode* is not specified,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300179 :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file
Vinay Sajipc63619b2010-12-19 12:56:57 +0000180 with that encoding. If *delay* is true, then file opening is deferred until the
Vinay Sajipca7b5042019-06-17 17:40:52 +0100181 first call to :meth:`emit`. By default, the file grows indefinitely. If
182 *errors* is provided, it determines how encoding errors are handled.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000183
Vinay Sajip638e6222016-07-22 18:23:04 +0100184 .. versionchanged:: 3.6
185 As well as string values, :class:`~pathlib.Path` objects are also accepted
186 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000187
Vinay Sajipca7b5042019-06-17 17:40:52 +0100188 .. versionchanged:: 3.9
189 The *errors* parameter was added.
190
Vinay Sajip29a14452015-10-01 20:54:41 +0100191 .. method:: reopenIfNeeded()
192
193 Checks to see if the file has changed. If it has, the existing stream is
194 flushed and closed and the file opened again, typically as a precursor to
195 outputting the record to the file.
196
Berker Peksag6f038ad2015-10-07 07:54:23 +0300197 .. versionadded:: 3.6
198
Vinay Sajip29a14452015-10-01 20:54:41 +0100199
Vinay Sajipc63619b2010-12-19 12:56:57 +0000200 .. method:: emit(record)
201
Vinay Sajip29a14452015-10-01 20:54:41 +0100202 Outputs the record to the file, but first calls :meth:`reopenIfNeeded` to
203 reopen the file if it has changed.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000204
Vinay Sajip23b94d02012-01-04 12:02:26 +0000205.. _base-rotating-handler:
206
207BaseRotatingHandler
208^^^^^^^^^^^^^^^^^^^
209
210The :class:`BaseRotatingHandler` class, located in the :mod:`logging.handlers`
211module, is the base class for the rotating file handlers,
212:class:`RotatingFileHandler` and :class:`TimedRotatingFileHandler`. You should
213not need to instantiate this class, but it has attributes and methods you may
214need to override.
215
Vinay Sajipca7b5042019-06-17 17:40:52 +0100216.. class:: BaseRotatingHandler(filename, mode, encoding=None, delay=False, errors=None)
Vinay Sajip23b94d02012-01-04 12:02:26 +0000217
218 The parameters are as for :class:`FileHandler`. The attributes are:
219
220 .. attribute:: namer
221
222 If this attribute is set to a callable, the :meth:`rotation_filename`
223 method delegates to this callable. The parameters passed to the callable
224 are those passed to :meth:`rotation_filename`.
225
226 .. note:: The namer function is called quite a few times during rollover,
227 so it should be as simple and as fast as possible. It should also
228 return the same output every time for a given input, otherwise the
229 rollover behaviour may not work as expected.
230
231 .. versionadded:: 3.3
232
233
234 .. attribute:: BaseRotatingHandler.rotator
235
236 If this attribute is set to a callable, the :meth:`rotate` method
237 delegates to this callable. The parameters passed to the callable are
238 those passed to :meth:`rotate`.
239
240 .. versionadded:: 3.3
241
242 .. method:: BaseRotatingHandler.rotation_filename(default_name)
243
244 Modify the filename of a log file when rotating.
245
246 This is provided so that a custom filename can be provided.
247
248 The default implementation calls the 'namer' attribute of the handler,
249 if it's callable, passing the default name to it. If the attribute isn't
Ezio Melotti226231c2012-01-18 05:40:00 +0200250 callable (the default is ``None``), the name is returned unchanged.
Vinay Sajip23b94d02012-01-04 12:02:26 +0000251
252 :param default_name: The default name for the log file.
253
254 .. versionadded:: 3.3
255
256
257 .. method:: BaseRotatingHandler.rotate(source, dest)
258
259 When rotating, rotate the current log.
260
261 The default implementation calls the 'rotator' attribute of the handler,
262 if it's callable, passing the source and dest arguments to it. If the
Ezio Melotti226231c2012-01-18 05:40:00 +0200263 attribute isn't callable (the default is ``None``), the source is simply
Vinay Sajip23b94d02012-01-04 12:02:26 +0000264 renamed to the destination.
265
266 :param source: The source filename. This is normally the base
Martin Panterd21e0b52015-10-10 10:36:22 +0000267 filename, e.g. 'test.log'.
Vinay Sajip23b94d02012-01-04 12:02:26 +0000268 :param dest: The destination filename. This is normally
269 what the source is rotated to, e.g. 'test.log.1'.
270
271 .. versionadded:: 3.3
272
273The reason the attributes exist is to save you having to subclass - you can use
274the same callables for instances of :class:`RotatingFileHandler` and
275:class:`TimedRotatingFileHandler`. If either the namer or rotator callable
276raises an exception, this will be handled in the same way as any other
277exception during an :meth:`emit` call, i.e. via the :meth:`handleError` method
278of the handler.
279
280If you need to make more significant changes to rotation processing, you can
281override the methods.
282
283For an example, see :ref:`cookbook-rotator-namer`.
284
285
Vinay Sajipc63619b2010-12-19 12:56:57 +0000286.. _rotating-file-handler:
287
288RotatingFileHandler
289^^^^^^^^^^^^^^^^^^^
290
291The :class:`RotatingFileHandler` class, located in the :mod:`logging.handlers`
292module, supports rotation of disk log files.
293
294
Vinay Sajipca7b5042019-06-17 17:40:52 +0100295.. class:: RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False, errors=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000296
297 Returns a new instance of the :class:`RotatingFileHandler` class. The specified
298 file is opened and used as the stream for logging. If *mode* is not specified,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300299 ``'a'`` is used. If *encoding* is not ``None``, it is used to open the file
Vinay Sajipc63619b2010-12-19 12:56:57 +0000300 with that encoding. If *delay* is true, then file opening is deferred until the
Vinay Sajipca7b5042019-06-17 17:40:52 +0100301 first call to :meth:`emit`. By default, the file grows indefinitely. If
302 *errors* is provided, it determines how encoding errors are handled.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000303
304 You can use the *maxBytes* and *backupCount* values to allow the file to
305 :dfn:`rollover` at a predetermined size. When the size is about to be exceeded,
306 the file is closed and a new file is silently opened for output. Rollover occurs
Vinay Sajip53a21eb2016-12-31 11:06:57 +0000307 whenever the current log file is nearly *maxBytes* in length; but if either of
308 *maxBytes* or *backupCount* is zero, rollover never occurs, so you generally want
309 to set *backupCount* to at least 1, and have a non-zero *maxBytes*.
310 When *backupCount* is non-zero, the system will save old log files by appending
311 the extensions '.1', '.2' etc., to the filename. For example, with a *backupCount*
312 of 5 and a base file name of :file:`app.log`, you would get :file:`app.log`,
Vinay Sajipff37cfe2015-01-23 21:19:04 +0000313 :file:`app.log.1`, :file:`app.log.2`, up to :file:`app.log.5`. The file being
314 written to is always :file:`app.log`. When this file is filled, it is closed
315 and renamed to :file:`app.log.1`, and if files :file:`app.log.1`,
Vinay Sajip53a21eb2016-12-31 11:06:57 +0000316 :file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`,
317 :file:`app.log.3` etc. respectively.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000318
Vinay Sajip638e6222016-07-22 18:23:04 +0100319 .. versionchanged:: 3.6
320 As well as string values, :class:`~pathlib.Path` objects are also accepted
321 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000322
Vinay Sajipca7b5042019-06-17 17:40:52 +0100323 .. versionchanged:: 3.9
324 The *errors* parameter was added.
325
Vinay Sajipc63619b2010-12-19 12:56:57 +0000326 .. method:: doRollover()
327
328 Does a rollover, as described above.
329
330
331 .. method:: emit(record)
332
333 Outputs the record to the file, catering for rollover as described
334 previously.
335
336.. _timed-rotating-file-handler:
337
338TimedRotatingFileHandler
339^^^^^^^^^^^^^^^^^^^^^^^^
340
341The :class:`TimedRotatingFileHandler` class, located in the
342:mod:`logging.handlers` module, supports rotation of disk log files at certain
343timed intervals.
344
345
Vinay Sajipca7b5042019-06-17 17:40:52 +0100346.. 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 +0000347
348 Returns a new instance of the :class:`TimedRotatingFileHandler` class. The
349 specified file is opened and used as the stream for logging. On rotating it also
350 sets the filename suffix. Rotating happens based on the product of *when* and
351 *interval*.
352
353 You can use the *when* to specify the type of *interval*. The list of possible
354 values is below. Note that they are not case sensitive.
355
Vinay Sajipdd308302016-08-24 17:49:15 +0100356 +----------------+----------------------------+-------------------------+
357 | Value | Type of interval | If/how *atTime* is used |
358 +================+============================+=========================+
359 | ``'S'`` | Seconds | Ignored |
360 +----------------+----------------------------+-------------------------+
361 | ``'M'`` | Minutes | Ignored |
362 +----------------+----------------------------+-------------------------+
363 | ``'H'`` | Hours | Ignored |
364 +----------------+----------------------------+-------------------------+
365 | ``'D'`` | Days | Ignored |
366 +----------------+----------------------------+-------------------------+
367 | ``'W0'-'W6'`` | Weekday (0=Monday) | Used to compute initial |
368 | | | rollover time |
369 +----------------+----------------------------+-------------------------+
370 | ``'midnight'`` | Roll over at midnight, if | Used to compute initial |
371 | | *atTime* not specified, | rollover time |
372 | | else at time *atTime* | |
373 +----------------+----------------------------+-------------------------+
Vinay Sajipc63619b2010-12-19 12:56:57 +0000374
Vinay Sajip832d99b2013-03-08 23:24:30 +0000375 When using weekday-based rotation, specify 'W0' for Monday, 'W1' for
376 Tuesday, and so on up to 'W6' for Sunday. In this case, the value passed for
377 *interval* isn't used.
378
Vinay Sajipc63619b2010-12-19 12:56:57 +0000379 The system will save old log files by appending extensions to the filename.
380 The extensions are date-and-time based, using the strftime format
381 ``%Y-%m-%d_%H-%M-%S`` or a leading portion thereof, depending on the
382 rollover interval.
383
384 When computing the next rollover time for the first time (when the handler
385 is created), the last modification time of an existing log file, or else
386 the current time, is used to compute when the next rotation will occur.
387
388 If the *utc* argument is true, times in UTC will be used; otherwise
389 local time is used.
390
391 If *backupCount* is nonzero, at most *backupCount* files
392 will be kept, and if more would be created when rollover occurs, the oldest
393 one is deleted. The deletion logic uses the interval to determine which
394 files to delete, so changing the interval may leave old files lying around.
395
396 If *delay* is true, then file opening is deferred until the first call to
397 :meth:`emit`.
398
Vinay Sajipa7130792013-04-12 17:04:23 +0100399 If *atTime* is not ``None``, it must be a ``datetime.time`` instance which
400 specifies the time of day when rollover occurs, for the cases where rollover
Vinay Sajipdd308302016-08-24 17:49:15 +0100401 is set to happen "at midnight" or "on a particular weekday". Note that in
402 these cases, the *atTime* value is effectively used to compute the *initial*
403 rollover, and subsequent rollovers would be calculated via the normal
404 interval calculation.
405
Vinay Sajipca7b5042019-06-17 17:40:52 +0100406 If *errors* is specified, it's used to determine how encoding errors are
407 handled.
408
Vinay Sajipdd308302016-08-24 17:49:15 +0100409 .. note:: Calculation of the initial rollover time is done when the handler
410 is initialised. Calculation of subsequent rollover times is done only
411 when rollover occurs, and rollover occurs only when emitting output. If
412 this is not kept in mind, it might lead to some confusion. For example,
413 if an interval of "every minute" is set, that does not mean you will
414 always see log files with times (in the filename) separated by a minute;
415 if, during application execution, logging output is generated more
416 frequently than once a minute, *then* you can expect to see log files
417 with times separated by a minute. If, on the other hand, logging messages
418 are only output once every five minutes (say), then there will be gaps in
419 the file times corresponding to the minutes where no output (and hence no
420 rollover) occurred.
Vinay Sajipa7130792013-04-12 17:04:23 +0100421
422 .. versionchanged:: 3.4
423 *atTime* parameter was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000424
Vinay Sajip638e6222016-07-22 18:23:04 +0100425 .. versionchanged:: 3.6
426 As well as string values, :class:`~pathlib.Path` objects are also accepted
427 for the *filename* argument.
428
Vinay Sajipca7b5042019-06-17 17:40:52 +0100429 .. versionchanged:: 3.9
430 The *errors* parameter was added.
431
Vinay Sajipc63619b2010-12-19 12:56:57 +0000432 .. method:: doRollover()
433
434 Does a rollover, as described above.
435
Vinay Sajipc63619b2010-12-19 12:56:57 +0000436 .. method:: emit(record)
437
438 Outputs the record to the file, catering for rollover as described above.
439
440
441.. _socket-handler:
442
443SocketHandler
444^^^^^^^^^^^^^
445
446The :class:`SocketHandler` class, located in the :mod:`logging.handlers` module,
447sends logging output to a network socket. The base class uses a TCP socket.
448
449
450.. class:: SocketHandler(host, port)
451
452 Returns a new instance of the :class:`SocketHandler` class intended to
453 communicate with a remote machine whose address is given by *host* and *port*.
454
Vinay Sajip5421f352013-09-27 18:18:28 +0100455 .. versionchanged:: 3.4
456 If ``port`` is specified as ``None``, a Unix domain socket is created
457 using the value in ``host`` - otherwise, a TCP socket is created.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000458
459 .. method:: close()
460
461 Closes the socket.
462
463
464 .. method:: emit()
465
466 Pickles the record's attribute dictionary and writes it to the socket in
467 binary format. If there is an error with the socket, silently drops the
468 packet. If the connection was previously lost, re-establishes the
469 connection. To unpickle the record at the receiving end into a
Vinay Sajip67f39772013-08-17 00:39:42 +0100470 :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
471 function.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000472
473
474 .. method:: handleError()
475
476 Handles an error which has occurred during :meth:`emit`. The most likely
477 cause is a lost connection. Closes the socket so that we can retry on the
478 next event.
479
480
481 .. method:: makeSocket()
482
483 This is a factory method which allows subclasses to define the precise
484 type of socket they want. The default implementation creates a TCP socket
485 (:const:`socket.SOCK_STREAM`).
486
487
488 .. method:: makePickle(record)
489
490 Pickles the record's attribute dictionary in binary format with a length
491 prefix, and returns it ready for transmission across the socket.
492
493 Note that pickles aren't completely secure. If you are concerned about
494 security, you may want to override this method to implement a more secure
495 mechanism. For example, you can sign pickles using HMAC and then verify
496 them on the receiving end, or alternatively you can disable unpickling of
497 global objects on the receiving end.
498
Georg Brandl08e278a2011-02-15 12:44:43 +0000499
Vinay Sajipc63619b2010-12-19 12:56:57 +0000500 .. method:: send(packet)
501
502 Send a pickled string *packet* to the socket. This function allows for
503 partial sends which can happen when the network is busy.
504
Georg Brandl08e278a2011-02-15 12:44:43 +0000505
Georg Brandldbb95852011-02-15 12:41:17 +0000506 .. method:: createSocket()
507
508 Tries to create a socket; on failure, uses an exponential back-off
Donald Stufft8b852f12014-05-20 12:58:38 -0400509 algorithm. On initial failure, the handler will drop the message it was
Georg Brandldbb95852011-02-15 12:41:17 +0000510 trying to send. When subsequent messages are handled by the same
511 instance, it will not try connecting until some time has passed. The
512 default parameters are such that the initial delay is one second, and if
513 after that delay the connection still can't be made, the handler will
514 double the delay each time up to a maximum of 30 seconds.
515
516 This behaviour is controlled by the following handler attributes:
517
518 * ``retryStart`` (initial delay, defaulting to 1.0 seconds).
519 * ``retryFactor`` (multiplier, defaulting to 2.0).
520 * ``retryMax`` (maximum delay, defaulting to 30.0 seconds).
521
522 This means that if the remote listener starts up *after* the handler has
523 been used, you could lose messages (since the handler won't even attempt
524 a connection until the delay has elapsed, but just silently drop messages
525 during the delay period).
Georg Brandl08e278a2011-02-15 12:44:43 +0000526
Vinay Sajipc63619b2010-12-19 12:56:57 +0000527
528.. _datagram-handler:
529
530DatagramHandler
531^^^^^^^^^^^^^^^
532
533The :class:`DatagramHandler` class, located in the :mod:`logging.handlers`
534module, inherits from :class:`SocketHandler` to support sending logging messages
535over UDP sockets.
536
537
538.. class:: DatagramHandler(host, port)
539
540 Returns a new instance of the :class:`DatagramHandler` class intended to
541 communicate with a remote machine whose address is given by *host* and *port*.
542
Vinay Sajip5421f352013-09-27 18:18:28 +0100543 .. versionchanged:: 3.4
544 If ``port`` is specified as ``None``, a Unix domain socket is created
Mike DePalatis233de022018-03-30 03:36:06 -0400545 using the value in ``host`` - otherwise, a UDP socket is created.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000546
547 .. method:: emit()
548
549 Pickles the record's attribute dictionary and writes it to the socket in
550 binary format. If there is an error with the socket, silently drops the
551 packet. To unpickle the record at the receiving end into a
Vinay Sajip67f39772013-08-17 00:39:42 +0100552 :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
553 function.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000554
555
556 .. method:: makeSocket()
557
558 The factory method of :class:`SocketHandler` is here overridden to create
559 a UDP socket (:const:`socket.SOCK_DGRAM`).
560
561
562 .. method:: send(s)
563
564 Send a pickled string to a socket.
565
566
567.. _syslog-handler:
568
569SysLogHandler
570^^^^^^^^^^^^^
571
572The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module,
573supports sending logging messages to a remote or local Unix syslog.
574
575
576.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
577
578 Returns a new instance of the :class:`SysLogHandler` class intended to
579 communicate with a remote Unix machine whose address is given by *address* in
580 the form of a ``(host, port)`` tuple. If *address* is not specified,
581 ``('localhost', 514)`` is used. The address is used to open a socket. An
582 alternative to providing a ``(host, port)`` tuple is providing an address as a
583 string, for example '/dev/log'. In this case, a Unix domain socket is used to
584 send the message to the syslog. If *facility* is not specified,
585 :const:`LOG_USER` is used. The type of socket opened depends on the
586 *socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus
587 opens a UDP socket. To open a TCP socket (for use with the newer syslog
588 daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`.
589
590 Note that if your server is not listening on UDP port 514,
591 :class:`SysLogHandler` may appear not to work. In that case, check what
592 address you should be using for a domain socket - it's system dependent.
593 For example, on Linux it's usually '/dev/log' but on OS/X it's
594 '/var/run/syslog'. You'll need to check your platform and use the
595 appropriate address (you may need to do this check at runtime if your
596 application needs to run on several platforms). On Windows, you pretty
597 much have to use the UDP option.
598
599 .. versionchanged:: 3.2
600 *socktype* was added.
601
602
603 .. method:: close()
604
605 Closes the socket to the remote host.
606
607
608 .. method:: emit(record)
609
610 The record is formatted, and then sent to the syslog server. If exception
611 information is present, it is *not* sent to the server.
612
Vinay Sajip645e4582011-06-10 18:52:50 +0100613 .. versionchanged:: 3.2.1
614 (See: :issue:`12168`.) In earlier versions, the message sent to the
615 syslog daemons was always terminated with a NUL byte, because early
616 versions of these daemons expected a NUL terminated message - even
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300617 though it's not in the relevant specification (:rfc:`5424`). More recent
Vinay Sajip645e4582011-06-10 18:52:50 +0100618 versions of these daemons don't expect the NUL byte but strip it off
619 if it's there, and even more recent daemons (which adhere more closely
620 to RFC 5424) pass the NUL byte on as part of the message.
621
622 To enable easier handling of syslog messages in the face of all these
623 differing daemon behaviours, the appending of the NUL byte has been
624 made configurable, through the use of a class-level attribute,
625 ``append_nul``. This defaults to ``True`` (preserving the existing
626 behaviour) but can be set to ``False`` on a ``SysLogHandler`` instance
627 in order for that instance to *not* append the NUL terminator.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000628
Vinay Sajip2353e352011-06-27 15:40:06 +0100629 .. versionchanged:: 3.3
630 (See: :issue:`12419`.) In earlier versions, there was no facility for
631 an "ident" or "tag" prefix to identify the source of the message. This
632 can now be specified using a class-level attribute, defaulting to
633 ``""`` to preserve existing behaviour, but which can be overridden on
634 a ``SysLogHandler`` instance in order for that instance to prepend
635 the ident to every message handled. Note that the provided ident must
636 be text, not bytes, and is prepended to the message exactly as is.
637
Vinay Sajipc63619b2010-12-19 12:56:57 +0000638 .. method:: encodePriority(facility, priority)
639
640 Encodes the facility and priority into an integer. You can pass in strings
641 or integers - if strings are passed, internal mapping dictionaries are
642 used to convert them to integers.
643
644 The symbolic ``LOG_`` values are defined in :class:`SysLogHandler` and
645 mirror the values defined in the ``sys/syslog.h`` header file.
646
647 **Priorities**
648
649 +--------------------------+---------------+
650 | Name (string) | Symbolic value|
651 +==========================+===============+
652 | ``alert`` | LOG_ALERT |
653 +--------------------------+---------------+
654 | ``crit`` or ``critical`` | LOG_CRIT |
655 +--------------------------+---------------+
656 | ``debug`` | LOG_DEBUG |
657 +--------------------------+---------------+
658 | ``emerg`` or ``panic`` | LOG_EMERG |
659 +--------------------------+---------------+
660 | ``err`` or ``error`` | LOG_ERR |
661 +--------------------------+---------------+
662 | ``info`` | LOG_INFO |
663 +--------------------------+---------------+
664 | ``notice`` | LOG_NOTICE |
665 +--------------------------+---------------+
666 | ``warn`` or ``warning`` | LOG_WARNING |
667 +--------------------------+---------------+
668
669 **Facilities**
670
671 +---------------+---------------+
672 | Name (string) | Symbolic value|
673 +===============+===============+
674 | ``auth`` | LOG_AUTH |
675 +---------------+---------------+
676 | ``authpriv`` | LOG_AUTHPRIV |
677 +---------------+---------------+
678 | ``cron`` | LOG_CRON |
679 +---------------+---------------+
680 | ``daemon`` | LOG_DAEMON |
681 +---------------+---------------+
682 | ``ftp`` | LOG_FTP |
683 +---------------+---------------+
684 | ``kern`` | LOG_KERN |
685 +---------------+---------------+
686 | ``lpr`` | LOG_LPR |
687 +---------------+---------------+
688 | ``mail`` | LOG_MAIL |
689 +---------------+---------------+
690 | ``news`` | LOG_NEWS |
691 +---------------+---------------+
692 | ``syslog`` | LOG_SYSLOG |
693 +---------------+---------------+
694 | ``user`` | LOG_USER |
695 +---------------+---------------+
696 | ``uucp`` | LOG_UUCP |
697 +---------------+---------------+
698 | ``local0`` | LOG_LOCAL0 |
699 +---------------+---------------+
700 | ``local1`` | LOG_LOCAL1 |
701 +---------------+---------------+
702 | ``local2`` | LOG_LOCAL2 |
703 +---------------+---------------+
704 | ``local3`` | LOG_LOCAL3 |
705 +---------------+---------------+
706 | ``local4`` | LOG_LOCAL4 |
707 +---------------+---------------+
708 | ``local5`` | LOG_LOCAL5 |
709 +---------------+---------------+
710 | ``local6`` | LOG_LOCAL6 |
711 +---------------+---------------+
712 | ``local7`` | LOG_LOCAL7 |
713 +---------------+---------------+
714
715 .. method:: mapPriority(levelname)
716
717 Maps a logging level name to a syslog priority name.
718 You may need to override this if you are using custom levels, or
719 if the default algorithm is not suitable for your needs. The
720 default algorithm maps ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR`` and
721 ``CRITICAL`` to the equivalent syslog names, and all other level
722 names to 'warning'.
723
724.. _nt-eventlog-handler:
725
726NTEventLogHandler
727^^^^^^^^^^^^^^^^^
728
729The :class:`NTEventLogHandler` class, located in the :mod:`logging.handlers`
730module, supports sending logging messages to a local Windows NT, Windows 2000 or
731Windows XP event log. Before you can use it, you need Mark Hammond's Win32
732extensions for Python installed.
733
734
735.. class:: NTEventLogHandler(appname, dllname=None, logtype='Application')
736
737 Returns a new instance of the :class:`NTEventLogHandler` class. The *appname* is
738 used to define the application name as it appears in the event log. An
739 appropriate registry entry is created using this name. The *dllname* should give
740 the fully qualified pathname of a .dll or .exe which contains message
741 definitions to hold in the log (if not specified, ``'win32service.pyd'`` is used
742 - this is installed with the Win32 extensions and contains some basic
743 placeholder message definitions. Note that use of these placeholders will make
744 your event logs big, as the entire message source is held in the log. If you
745 want slimmer logs, you have to pass in the name of your own .dll or .exe which
746 contains the message definitions you want to use in the event log). The
747 *logtype* is one of ``'Application'``, ``'System'`` or ``'Security'``, and
748 defaults to ``'Application'``.
749
750
751 .. method:: close()
752
753 At this point, you can remove the application name from the registry as a
754 source of event log entries. However, if you do this, you will not be able
755 to see the events as you intended in the Event Log Viewer - it needs to be
756 able to access the registry to get the .dll name. The current version does
757 not do this.
758
759
760 .. method:: emit(record)
761
762 Determines the message ID, event category and event type, and then logs
763 the message in the NT event log.
764
765
766 .. method:: getEventCategory(record)
767
768 Returns the event category for the record. Override this if you want to
769 specify your own categories. This version returns 0.
770
771
772 .. method:: getEventType(record)
773
774 Returns the event type for the record. Override this if you want to
775 specify your own types. This version does a mapping using the handler's
776 typemap attribute, which is set up in :meth:`__init__` to a dictionary
777 which contains mappings for :const:`DEBUG`, :const:`INFO`,
778 :const:`WARNING`, :const:`ERROR` and :const:`CRITICAL`. If you are using
779 your own levels, you will either need to override this method or place a
780 suitable dictionary in the handler's *typemap* attribute.
781
782
783 .. method:: getMessageID(record)
784
785 Returns the message ID for the record. If you are using your own messages,
786 you could do this by having the *msg* passed to the logger being an ID
787 rather than a format string. Then, in here, you could use a dictionary
788 lookup to get the message ID. This version returns 1, which is the base
789 message ID in :file:`win32service.pyd`.
790
791.. _smtp-handler:
792
793SMTPHandler
794^^^^^^^^^^^
795
796The :class:`SMTPHandler` class, located in the :mod:`logging.handlers` module,
797supports sending logging messages to an email address via SMTP.
798
799
Vinay Sajip38a12af2012-03-26 17:17:39 +0100800.. class:: SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000801
802 Returns a new instance of the :class:`SMTPHandler` class. The instance is
803 initialized with the from and to addresses and subject line of the email. The
804 *toaddrs* should be a list of strings. To specify a non-standard SMTP port, use
805 the (host, port) tuple format for the *mailhost* argument. If you use a string,
806 the standard SMTP port is used. If your SMTP server requires authentication, you
807 can specify a (username, password) tuple for the *credentials* argument.
808
Vinay Sajip95259562011-08-01 11:31:52 +0100809 To specify the use of a secure protocol (TLS), pass in a tuple to the
810 *secure* argument. This will only be used when authentication credentials are
811 supplied. The tuple should be either an empty tuple, or a single-value tuple
812 with the name of a keyfile, or a 2-value tuple with the names of the keyfile
813 and certificate file. (This tuple is passed to the
814 :meth:`smtplib.SMTP.starttls` method.)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000815
Vinay Sajip38a12af2012-03-26 17:17:39 +0100816 A timeout can be specified for communication with the SMTP server using the
817 *timeout* argument.
818
819 .. versionadded:: 3.3
820 The *timeout* argument was added.
821
Vinay Sajipc63619b2010-12-19 12:56:57 +0000822 .. method:: emit(record)
823
824 Formats the record and sends it to the specified addressees.
825
826
827 .. method:: getSubject(record)
828
829 If you want to specify a subject line which is record-dependent, override
830 this method.
831
832.. _memory-handler:
833
834MemoryHandler
835^^^^^^^^^^^^^
836
837The :class:`MemoryHandler` class, located in the :mod:`logging.handlers` module,
838supports buffering of logging records in memory, periodically flushing them to a
839:dfn:`target` handler. Flushing occurs whenever the buffer is full, or when an
840event of a certain severity or greater is seen.
841
842:class:`MemoryHandler` is a subclass of the more general
843:class:`BufferingHandler`, which is an abstract class. This buffers logging
844records in memory. Whenever each record is added to the buffer, a check is made
845by calling :meth:`shouldFlush` to see if the buffer should be flushed. If it
Vinay Sajip8ece80f2012-03-26 17:09:58 +0100846should, then :meth:`flush` is expected to do the flushing.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000847
848
849.. class:: BufferingHandler(capacity)
850
851 Initializes the handler with a buffer of the specified capacity.
852
853
854 .. method:: emit(record)
855
856 Appends the record to the buffer. If :meth:`shouldFlush` returns true,
857 calls :meth:`flush` to process the buffer.
858
859
860 .. method:: flush()
861
862 You can override this to implement custom flushing behavior. This version
863 just zaps the buffer to empty.
864
865
866 .. method:: shouldFlush(record)
867
868 Returns true if the buffer is up to capacity. This method can be
869 overridden to implement custom flushing strategies.
870
871
Vinay Sajipcccf6062016-07-22 16:27:31 +0100872.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000873
874 Returns a new instance of the :class:`MemoryHandler` class. The instance is
875 initialized with a buffer size of *capacity*. If *flushLevel* is not specified,
876 :const:`ERROR` is used. If no *target* is specified, the target will need to be
Vinay Sajipcccf6062016-07-22 16:27:31 +0100877 set using :meth:`setTarget` before this handler does anything useful. If
878 *flushOnClose* is specified as ``False``, then the buffer is *not* flushed when
879 the handler is closed. If not specified or specified as ``True``, the previous
880 behaviour of flushing the buffer will occur when the handler is closed.
881
882 .. versionchanged:: 3.6
883 The *flushOnClose* parameter was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000884
885
886 .. method:: close()
887
Ezio Melotti226231c2012-01-18 05:40:00 +0200888 Calls :meth:`flush`, sets the target to ``None`` and clears the
Vinay Sajipc63619b2010-12-19 12:56:57 +0000889 buffer.
890
891
892 .. method:: flush()
893
894 For a :class:`MemoryHandler`, flushing means just sending the buffered
895 records to the target, if there is one. The buffer is also cleared when
896 this happens. Override if you want different behavior.
897
898
899 .. method:: setTarget(target)
900
901 Sets the target handler for this handler.
902
903
904 .. method:: shouldFlush(record)
905
906 Checks for buffer full or a record at the *flushLevel* or higher.
907
908
909.. _http-handler:
910
911HTTPHandler
912^^^^^^^^^^^
913
914The :class:`HTTPHandler` class, located in the :mod:`logging.handlers` module,
915supports sending logging messages to a Web server, using either ``GET`` or
916``POST`` semantics.
917
918
Benjamin Peterson43052a12014-11-23 20:36:44 -0600919.. class:: HTTPHandler(host, url, method='GET', secure=False, credentials=None, context=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000920
921 Returns a new instance of the :class:`HTTPHandler` class. The *host* can be
Benjamin Peterson43052a12014-11-23 20:36:44 -0600922 of the form ``host:port``, should you need to use a specific port number. If
923 no *method* is specified, ``GET`` is used. If *secure* is true, a HTTPS
924 connection will be used. The *context* parameter may be set to a
925 :class:`ssl.SSLContext` instance to configure the SSL settings used for the
926 HTTPS connection. If *credentials* is specified, it should be a 2-tuple
927 consisting of userid and password, which will be placed in a HTTP
Vinay Sajipc63619b2010-12-19 12:56:57 +0000928 'Authorization' header using Basic authentication. If you specify
929 credentials, you should also specify secure=True so that your userid and
930 password are not passed in cleartext across the wire.
931
Benjamin Petersona90e92d2014-11-23 20:38:37 -0600932 .. versionchanged:: 3.5
Benjamin Peterson43052a12014-11-23 20:36:44 -0600933 The *context* parameter was added.
934
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100935 .. method:: mapLogRecord(record)
936
937 Provides a dictionary, based on ``record``, which is to be URL-encoded
938 and sent to the web server. The default implementation just returns
939 ``record.__dict__``. This method can be overridden if e.g. only a
940 subset of :class:`~logging.LogRecord` is to be sent to the web server, or
941 if more specific customization of what's sent to the server is required.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000942
943 .. method:: emit(record)
944
Martin Panter6245cb32016-04-15 02:14:19 +0000945 Sends the record to the Web server as a URL-encoded dictionary. The
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100946 :meth:`mapLogRecord` method is used to convert the record to the
947 dictionary to be sent.
948
Berker Peksag9c1dba22014-09-28 00:00:58 +0300949 .. note:: Since preparing a record for sending it to a Web server is not
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100950 the same as a generic formatting operation, using
951 :meth:`~logging.Handler.setFormatter` to specify a
952 :class:`~logging.Formatter` for a :class:`HTTPHandler` has no effect.
953 Instead of calling :meth:`~logging.Handler.format`, this handler calls
954 :meth:`mapLogRecord` and then :func:`urllib.parse.urlencode` to encode the
955 dictionary in a form suitable for sending to a Web server.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000956
957
958.. _queue-handler:
959
960
961QueueHandler
962^^^^^^^^^^^^
963
964.. versionadded:: 3.2
965
966The :class:`QueueHandler` class, located in the :mod:`logging.handlers` module,
967supports sending logging messages to a queue, such as those implemented in the
968:mod:`queue` or :mod:`multiprocessing` modules.
969
970Along with the :class:`QueueListener` class, :class:`QueueHandler` can be used
971to let handlers do their work on a separate thread from the one which does the
972logging. This is important in Web applications and also other service
973applications where threads servicing clients need to respond as quickly as
974possible, while any potentially slow operations (such as sending an email via
975:class:`SMTPHandler`) are done on a separate thread.
976
977.. class:: QueueHandler(queue)
978
979 Returns a new instance of the :class:`QueueHandler` class. The instance is
Martin Panter8f137832017-01-14 08:24:20 +0000980 initialized with the queue to send messages to. The queue can be any
981 queue-like object; it's used as-is by the :meth:`enqueue` method, which needs
Vinay Sajipc63619b2010-12-19 12:56:57 +0000982 to know how to send messages to it.
983
984
985 .. method:: emit(record)
986
987 Enqueues the result of preparing the LogRecord.
988
989 .. method:: prepare(record)
990
991 Prepares a record for queuing. The object returned by this
992 method is enqueued.
993
Cheryl Sabellad345bb42018-09-25 19:00:08 -0400994 The base implementation formats the record to merge the message,
995 arguments, and exception information, if present. It also
996 removes unpickleable items from the record in-place.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000997
998 You might want to override this method if you want to convert
999 the record to a dict or JSON string, or send a modified copy
1000 of the record while leaving the original intact.
1001
1002 .. method:: enqueue(record)
1003
1004 Enqueues the record on the queue using ``put_nowait()``; you may
1005 want to override this if you want to use blocking behaviour, or a
Vinay Sajip9c10d6b2013-11-15 20:58:13 +00001006 timeout, or a customized queue implementation.
Vinay Sajipc63619b2010-12-19 12:56:57 +00001007
1008
1009
Éric Araujo5eada942011-08-19 00:41:23 +02001010.. _queue-listener:
Vinay Sajipc63619b2010-12-19 12:56:57 +00001011
1012QueueListener
1013^^^^^^^^^^^^^
1014
1015.. versionadded:: 3.2
1016
1017The :class:`QueueListener` class, located in the :mod:`logging.handlers`
1018module, supports receiving logging messages from a queue, such as those
1019implemented in the :mod:`queue` or :mod:`multiprocessing` modules. The
1020messages are received from a queue in an internal thread and passed, on
1021the same thread, to one or more handlers for processing. While
1022:class:`QueueListener` is not itself a handler, it is documented here
1023because it works hand-in-hand with :class:`QueueHandler`.
1024
1025Along with the :class:`QueueHandler` class, :class:`QueueListener` can be used
1026to let handlers do their work on a separate thread from the one which does the
1027logging. This is important in Web applications and also other service
1028applications where threads servicing clients need to respond as quickly as
1029possible, while any potentially slow operations (such as sending an email via
1030:class:`SMTPHandler`) are done on a separate thread.
1031
Vinay Sajip365701a2015-02-09 19:49:00 +00001032.. class:: QueueListener(queue, *handlers, respect_handler_level=False)
Vinay Sajipc63619b2010-12-19 12:56:57 +00001033
1034 Returns a new instance of the :class:`QueueListener` class. The instance is
1035 initialized with the queue to send messages to and a list of handlers which
Martin Panter8f137832017-01-14 08:24:20 +00001036 will handle entries placed on the queue. The queue can be any queue-like
1037 object; it's passed as-is to the :meth:`dequeue` method, which needs
Vinay Sajip365701a2015-02-09 19:49:00 +00001038 to know how to get messages from it. If ``respect_handler_level`` is ``True``,
1039 a handler's level is respected (compared with the level for the message) when
1040 deciding whether to pass messages to that handler; otherwise, the behaviour
1041 is as in previous Python versions - to always pass each message to each
1042 handler.
1043
1044 .. versionchanged:: 3.5
1045 The ``respect_handler_levels`` argument was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +00001046
1047 .. method:: dequeue(block)
1048
1049 Dequeues a record and return it, optionally blocking.
1050
1051 The base implementation uses ``get()``. You may want to override this
1052 method if you want to use timeouts or work with custom queue
1053 implementations.
1054
1055 .. method:: prepare(record)
1056
1057 Prepare a record for handling.
1058
1059 This implementation just returns the passed-in record. You may want to
1060 override this method if you need to do any custom marshalling or
1061 manipulation of the record before passing it to the handlers.
1062
1063 .. method:: handle(record)
1064
1065 Handle a record.
1066
1067 This just loops through the handlers offering them the record
1068 to handle. The actual object passed to the handlers is that which
1069 is returned from :meth:`prepare`.
1070
1071 .. method:: start()
1072
1073 Starts the listener.
1074
1075 This starts up a background thread to monitor the queue for
1076 LogRecords to process.
1077
1078 .. method:: stop()
1079
1080 Stops the listener.
1081
1082 This asks the thread to terminate, and then waits for it to do so.
1083 Note that if you don't call this before your application exits, there
1084 may be some records still left on the queue, which won't be processed.
1085
Vinay Sajipa29a9dd2011-02-25 16:05:26 +00001086 .. method:: enqueue_sentinel()
1087
1088 Writes a sentinel to the queue to tell the listener to quit. This
1089 implementation uses ``put_nowait()``. You may want to override this
1090 method if you want to use timeouts or work with custom queue
1091 implementations.
1092
1093 .. versionadded:: 3.3
1094
Vinay Sajipc63619b2010-12-19 12:56:57 +00001095
1096.. seealso::
1097
1098 Module :mod:`logging`
1099 API reference for the logging module.
1100
1101 Module :mod:`logging.config`
1102 Configuration API for the logging module.
1103
1104