blob: bdf16a8177e99476bd98438abfc9fc9f9a629084 [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
92.. class:: FileHandler(filename, mode='a', encoding=None, delay=False)
93
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
98 first call to :meth:`emit`. By default, the file grows indefinitely.
99
Vinay Sajip638e6222016-07-22 18:23:04 +0100100 .. versionchanged:: 3.6
101 As well as string values, :class:`~pathlib.Path` objects are also accepted
102 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000103
104 .. method:: close()
105
106 Closes the file.
107
108
109 .. method:: emit(record)
110
111 Outputs the record to the file.
112
113
114.. _null-handler:
115
116NullHandler
117^^^^^^^^^^^
118
119.. versionadded:: 3.1
120
121The :class:`NullHandler` class, located in the core :mod:`logging` package,
122does not do any formatting or output. It is essentially a 'no-op' handler
123for use by library developers.
124
125.. class:: NullHandler()
126
127 Returns a new instance of the :class:`NullHandler` class.
128
129 .. method:: emit(record)
130
131 This method does nothing.
132
133 .. method:: handle(record)
134
135 This method does nothing.
136
137 .. method:: createLock()
138
139 This method returns ``None`` for the lock, since there is no
140 underlying I/O to which access needs to be serialized.
141
142
143See :ref:`library-config` for more information on how to use
144:class:`NullHandler`.
145
146.. _watched-file-handler:
147
148WatchedFileHandler
149^^^^^^^^^^^^^^^^^^
150
151.. currentmodule:: logging.handlers
152
153The :class:`WatchedFileHandler` class, located in the :mod:`logging.handlers`
154module, is a :class:`FileHandler` which watches the file it is logging to. If
155the file changes, it is closed and reopened using the file name.
156
157A file change can happen because of usage of programs such as *newsyslog* and
158*logrotate* which perform log file rotation. This handler, intended for use
159under Unix/Linux, watches the file to see if it has changed since the last emit.
160(A file is deemed to have changed if its device or inode have changed.) If the
161file has changed, the old file stream is closed, and the file opened to get a
162new stream.
163
164This handler is not appropriate for use under Windows, because under Windows
165open log files cannot be moved or renamed - logging opens the files with
166exclusive locks - and so there is no need for such a handler. Furthermore,
Vinay Sajip67f39772013-08-17 00:39:42 +0100167*ST_INO* is not supported under Windows; :func:`~os.stat` always returns zero
168for this value.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000169
170
Zachary Ware2f47fb02016-08-09 16:20:41 -0500171.. class:: WatchedFileHandler(filename, mode='a', encoding=None, delay=False)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000172
173 Returns a new instance of the :class:`WatchedFileHandler` class. The specified
174 file is opened and used as the stream for logging. If *mode* is not specified,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300175 :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file
Vinay Sajipc63619b2010-12-19 12:56:57 +0000176 with that encoding. If *delay* is true, then file opening is deferred until the
177 first call to :meth:`emit`. By default, the file grows indefinitely.
178
Vinay Sajip638e6222016-07-22 18:23:04 +0100179 .. versionchanged:: 3.6
180 As well as string values, :class:`~pathlib.Path` objects are also accepted
181 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000182
Vinay Sajip29a14452015-10-01 20:54:41 +0100183 .. method:: reopenIfNeeded()
184
185 Checks to see if the file has changed. If it has, the existing stream is
186 flushed and closed and the file opened again, typically as a precursor to
187 outputting the record to the file.
188
Berker Peksag6f038ad2015-10-07 07:54:23 +0300189 .. versionadded:: 3.6
190
Vinay Sajip29a14452015-10-01 20:54:41 +0100191
Vinay Sajipc63619b2010-12-19 12:56:57 +0000192 .. method:: emit(record)
193
Vinay Sajip29a14452015-10-01 20:54:41 +0100194 Outputs the record to the file, but first calls :meth:`reopenIfNeeded` to
195 reopen the file if it has changed.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000196
Vinay Sajip23b94d02012-01-04 12:02:26 +0000197.. _base-rotating-handler:
198
199BaseRotatingHandler
200^^^^^^^^^^^^^^^^^^^
201
202The :class:`BaseRotatingHandler` class, located in the :mod:`logging.handlers`
203module, is the base class for the rotating file handlers,
204:class:`RotatingFileHandler` and :class:`TimedRotatingFileHandler`. You should
205not need to instantiate this class, but it has attributes and methods you may
206need to override.
207
208.. class:: BaseRotatingHandler(filename, mode, encoding=None, delay=False)
209
210 The parameters are as for :class:`FileHandler`. The attributes are:
211
212 .. attribute:: namer
213
214 If this attribute is set to a callable, the :meth:`rotation_filename`
215 method delegates to this callable. The parameters passed to the callable
216 are those passed to :meth:`rotation_filename`.
217
218 .. note:: The namer function is called quite a few times during rollover,
219 so it should be as simple and as fast as possible. It should also
220 return the same output every time for a given input, otherwise the
221 rollover behaviour may not work as expected.
222
223 .. versionadded:: 3.3
224
225
226 .. attribute:: BaseRotatingHandler.rotator
227
228 If this attribute is set to a callable, the :meth:`rotate` method
229 delegates to this callable. The parameters passed to the callable are
230 those passed to :meth:`rotate`.
231
232 .. versionadded:: 3.3
233
234 .. method:: BaseRotatingHandler.rotation_filename(default_name)
235
236 Modify the filename of a log file when rotating.
237
238 This is provided so that a custom filename can be provided.
239
240 The default implementation calls the 'namer' attribute of the handler,
241 if it's callable, passing the default name to it. If the attribute isn't
Ezio Melotti226231c2012-01-18 05:40:00 +0200242 callable (the default is ``None``), the name is returned unchanged.
Vinay Sajip23b94d02012-01-04 12:02:26 +0000243
244 :param default_name: The default name for the log file.
245
246 .. versionadded:: 3.3
247
248
249 .. method:: BaseRotatingHandler.rotate(source, dest)
250
251 When rotating, rotate the current log.
252
253 The default implementation calls the 'rotator' attribute of the handler,
254 if it's callable, passing the source and dest arguments to it. If the
Ezio Melotti226231c2012-01-18 05:40:00 +0200255 attribute isn't callable (the default is ``None``), the source is simply
Vinay Sajip23b94d02012-01-04 12:02:26 +0000256 renamed to the destination.
257
258 :param source: The source filename. This is normally the base
Martin Panterd21e0b52015-10-10 10:36:22 +0000259 filename, e.g. 'test.log'.
Vinay Sajip23b94d02012-01-04 12:02:26 +0000260 :param dest: The destination filename. This is normally
261 what the source is rotated to, e.g. 'test.log.1'.
262
263 .. versionadded:: 3.3
264
265The reason the attributes exist is to save you having to subclass - you can use
266the same callables for instances of :class:`RotatingFileHandler` and
267:class:`TimedRotatingFileHandler`. If either the namer or rotator callable
268raises an exception, this will be handled in the same way as any other
269exception during an :meth:`emit` call, i.e. via the :meth:`handleError` method
270of the handler.
271
272If you need to make more significant changes to rotation processing, you can
273override the methods.
274
275For an example, see :ref:`cookbook-rotator-namer`.
276
277
Vinay Sajipc63619b2010-12-19 12:56:57 +0000278.. _rotating-file-handler:
279
280RotatingFileHandler
281^^^^^^^^^^^^^^^^^^^
282
283The :class:`RotatingFileHandler` class, located in the :mod:`logging.handlers`
284module, supports rotation of disk log files.
285
286
Zachary Ware2f47fb02016-08-09 16:20:41 -0500287.. class:: RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000288
289 Returns a new instance of the :class:`RotatingFileHandler` class. The specified
290 file is opened and used as the stream for logging. If *mode* is not specified,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300291 ``'a'`` is used. If *encoding* is not ``None``, it is used to open the file
Vinay Sajipc63619b2010-12-19 12:56:57 +0000292 with that encoding. If *delay* is true, then file opening is deferred until the
293 first call to :meth:`emit`. By default, the file grows indefinitely.
294
295 You can use the *maxBytes* and *backupCount* values to allow the file to
296 :dfn:`rollover` at a predetermined size. When the size is about to be exceeded,
297 the file is closed and a new file is silently opened for output. Rollover occurs
Vinay Sajip53a21eb2016-12-31 11:06:57 +0000298 whenever the current log file is nearly *maxBytes* in length; but if either of
299 *maxBytes* or *backupCount* is zero, rollover never occurs, so you generally want
300 to set *backupCount* to at least 1, and have a non-zero *maxBytes*.
301 When *backupCount* is non-zero, the system will save old log files by appending
302 the extensions '.1', '.2' etc., to the filename. For example, with a *backupCount*
303 of 5 and a base file name of :file:`app.log`, you would get :file:`app.log`,
Vinay Sajipff37cfe2015-01-23 21:19:04 +0000304 :file:`app.log.1`, :file:`app.log.2`, up to :file:`app.log.5`. The file being
305 written to is always :file:`app.log`. When this file is filled, it is closed
306 and renamed to :file:`app.log.1`, and if files :file:`app.log.1`,
Vinay Sajip53a21eb2016-12-31 11:06:57 +0000307 :file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`,
308 :file:`app.log.3` etc. respectively.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000309
Vinay Sajip638e6222016-07-22 18:23:04 +0100310 .. versionchanged:: 3.6
311 As well as string values, :class:`~pathlib.Path` objects are also accepted
312 for the *filename* argument.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000313
314 .. method:: doRollover()
315
316 Does a rollover, as described above.
317
318
319 .. method:: emit(record)
320
321 Outputs the record to the file, catering for rollover as described
322 previously.
323
324.. _timed-rotating-file-handler:
325
326TimedRotatingFileHandler
327^^^^^^^^^^^^^^^^^^^^^^^^
328
329The :class:`TimedRotatingFileHandler` class, located in the
330:mod:`logging.handlers` module, supports rotation of disk log files at certain
331timed intervals.
332
333
Vinay Sajipa7130792013-04-12 17:04:23 +0100334.. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000335
336 Returns a new instance of the :class:`TimedRotatingFileHandler` class. The
337 specified file is opened and used as the stream for logging. On rotating it also
338 sets the filename suffix. Rotating happens based on the product of *when* and
339 *interval*.
340
341 You can use the *when* to specify the type of *interval*. The list of possible
342 values is below. Note that they are not case sensitive.
343
Vinay Sajipdd308302016-08-24 17:49:15 +0100344 +----------------+----------------------------+-------------------------+
345 | Value | Type of interval | If/how *atTime* is used |
346 +================+============================+=========================+
347 | ``'S'`` | Seconds | Ignored |
348 +----------------+----------------------------+-------------------------+
349 | ``'M'`` | Minutes | Ignored |
350 +----------------+----------------------------+-------------------------+
351 | ``'H'`` | Hours | Ignored |
352 +----------------+----------------------------+-------------------------+
353 | ``'D'`` | Days | Ignored |
354 +----------------+----------------------------+-------------------------+
355 | ``'W0'-'W6'`` | Weekday (0=Monday) | Used to compute initial |
356 | | | rollover time |
357 +----------------+----------------------------+-------------------------+
358 | ``'midnight'`` | Roll over at midnight, if | Used to compute initial |
359 | | *atTime* not specified, | rollover time |
360 | | else at time *atTime* | |
361 +----------------+----------------------------+-------------------------+
Vinay Sajipc63619b2010-12-19 12:56:57 +0000362
Vinay Sajip832d99b2013-03-08 23:24:30 +0000363 When using weekday-based rotation, specify 'W0' for Monday, 'W1' for
364 Tuesday, and so on up to 'W6' for Sunday. In this case, the value passed for
365 *interval* isn't used.
366
Vinay Sajipc63619b2010-12-19 12:56:57 +0000367 The system will save old log files by appending extensions to the filename.
368 The extensions are date-and-time based, using the strftime format
369 ``%Y-%m-%d_%H-%M-%S`` or a leading portion thereof, depending on the
370 rollover interval.
371
372 When computing the next rollover time for the first time (when the handler
373 is created), the last modification time of an existing log file, or else
374 the current time, is used to compute when the next rotation will occur.
375
376 If the *utc* argument is true, times in UTC will be used; otherwise
377 local time is used.
378
379 If *backupCount* is nonzero, at most *backupCount* files
380 will be kept, and if more would be created when rollover occurs, the oldest
381 one is deleted. The deletion logic uses the interval to determine which
382 files to delete, so changing the interval may leave old files lying around.
383
384 If *delay* is true, then file opening is deferred until the first call to
385 :meth:`emit`.
386
Vinay Sajipa7130792013-04-12 17:04:23 +0100387 If *atTime* is not ``None``, it must be a ``datetime.time`` instance which
388 specifies the time of day when rollover occurs, for the cases where rollover
Vinay Sajipdd308302016-08-24 17:49:15 +0100389 is set to happen "at midnight" or "on a particular weekday". Note that in
390 these cases, the *atTime* value is effectively used to compute the *initial*
391 rollover, and subsequent rollovers would be calculated via the normal
392 interval calculation.
393
394 .. note:: Calculation of the initial rollover time is done when the handler
395 is initialised. Calculation of subsequent rollover times is done only
396 when rollover occurs, and rollover occurs only when emitting output. If
397 this is not kept in mind, it might lead to some confusion. For example,
398 if an interval of "every minute" is set, that does not mean you will
399 always see log files with times (in the filename) separated by a minute;
400 if, during application execution, logging output is generated more
401 frequently than once a minute, *then* you can expect to see log files
402 with times separated by a minute. If, on the other hand, logging messages
403 are only output once every five minutes (say), then there will be gaps in
404 the file times corresponding to the minutes where no output (and hence no
405 rollover) occurred.
Vinay Sajipa7130792013-04-12 17:04:23 +0100406
407 .. versionchanged:: 3.4
408 *atTime* parameter was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000409
Vinay Sajip638e6222016-07-22 18:23:04 +0100410 .. versionchanged:: 3.6
411 As well as string values, :class:`~pathlib.Path` objects are also accepted
412 for the *filename* argument.
413
Vinay Sajipc63619b2010-12-19 12:56:57 +0000414 .. method:: doRollover()
415
416 Does a rollover, as described above.
417
Vinay Sajipc63619b2010-12-19 12:56:57 +0000418 .. method:: emit(record)
419
420 Outputs the record to the file, catering for rollover as described above.
421
422
423.. _socket-handler:
424
425SocketHandler
426^^^^^^^^^^^^^
427
428The :class:`SocketHandler` class, located in the :mod:`logging.handlers` module,
429sends logging output to a network socket. The base class uses a TCP socket.
430
431
432.. class:: SocketHandler(host, port)
433
434 Returns a new instance of the :class:`SocketHandler` class intended to
435 communicate with a remote machine whose address is given by *host* and *port*.
436
Vinay Sajip5421f352013-09-27 18:18:28 +0100437 .. versionchanged:: 3.4
438 If ``port`` is specified as ``None``, a Unix domain socket is created
439 using the value in ``host`` - otherwise, a TCP socket is created.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000440
441 .. method:: close()
442
443 Closes the socket.
444
445
446 .. method:: emit()
447
448 Pickles the record's attribute dictionary and writes it to the socket in
449 binary format. If there is an error with the socket, silently drops the
450 packet. If the connection was previously lost, re-establishes the
451 connection. To unpickle the record at the receiving end into a
Vinay Sajip67f39772013-08-17 00:39:42 +0100452 :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
453 function.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000454
455
456 .. method:: handleError()
457
458 Handles an error which has occurred during :meth:`emit`. The most likely
459 cause is a lost connection. Closes the socket so that we can retry on the
460 next event.
461
462
463 .. method:: makeSocket()
464
465 This is a factory method which allows subclasses to define the precise
466 type of socket they want. The default implementation creates a TCP socket
467 (:const:`socket.SOCK_STREAM`).
468
469
470 .. method:: makePickle(record)
471
472 Pickles the record's attribute dictionary in binary format with a length
473 prefix, and returns it ready for transmission across the socket.
474
475 Note that pickles aren't completely secure. If you are concerned about
476 security, you may want to override this method to implement a more secure
477 mechanism. For example, you can sign pickles using HMAC and then verify
478 them on the receiving end, or alternatively you can disable unpickling of
479 global objects on the receiving end.
480
Georg Brandl08e278a2011-02-15 12:44:43 +0000481
Vinay Sajipc63619b2010-12-19 12:56:57 +0000482 .. method:: send(packet)
483
484 Send a pickled string *packet* to the socket. This function allows for
485 partial sends which can happen when the network is busy.
486
Georg Brandl08e278a2011-02-15 12:44:43 +0000487
Georg Brandldbb95852011-02-15 12:41:17 +0000488 .. method:: createSocket()
489
490 Tries to create a socket; on failure, uses an exponential back-off
Donald Stufft8b852f12014-05-20 12:58:38 -0400491 algorithm. On initial failure, the handler will drop the message it was
Georg Brandldbb95852011-02-15 12:41:17 +0000492 trying to send. When subsequent messages are handled by the same
493 instance, it will not try connecting until some time has passed. The
494 default parameters are such that the initial delay is one second, and if
495 after that delay the connection still can't be made, the handler will
496 double the delay each time up to a maximum of 30 seconds.
497
498 This behaviour is controlled by the following handler attributes:
499
500 * ``retryStart`` (initial delay, defaulting to 1.0 seconds).
501 * ``retryFactor`` (multiplier, defaulting to 2.0).
502 * ``retryMax`` (maximum delay, defaulting to 30.0 seconds).
503
504 This means that if the remote listener starts up *after* the handler has
505 been used, you could lose messages (since the handler won't even attempt
506 a connection until the delay has elapsed, but just silently drop messages
507 during the delay period).
Georg Brandl08e278a2011-02-15 12:44:43 +0000508
Vinay Sajipc63619b2010-12-19 12:56:57 +0000509
510.. _datagram-handler:
511
512DatagramHandler
513^^^^^^^^^^^^^^^
514
515The :class:`DatagramHandler` class, located in the :mod:`logging.handlers`
516module, inherits from :class:`SocketHandler` to support sending logging messages
517over UDP sockets.
518
519
520.. class:: DatagramHandler(host, port)
521
522 Returns a new instance of the :class:`DatagramHandler` class intended to
523 communicate with a remote machine whose address is given by *host* and *port*.
524
Vinay Sajip5421f352013-09-27 18:18:28 +0100525 .. versionchanged:: 3.4
526 If ``port`` is specified as ``None``, a Unix domain socket is created
Mike DePalatis233de022018-03-30 03:36:06 -0400527 using the value in ``host`` - otherwise, a UDP socket is created.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000528
529 .. method:: emit()
530
531 Pickles the record's attribute dictionary and writes it to the socket in
532 binary format. If there is an error with the socket, silently drops the
533 packet. To unpickle the record at the receiving end into a
Vinay Sajip67f39772013-08-17 00:39:42 +0100534 :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
535 function.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000536
537
538 .. method:: makeSocket()
539
540 The factory method of :class:`SocketHandler` is here overridden to create
541 a UDP socket (:const:`socket.SOCK_DGRAM`).
542
543
544 .. method:: send(s)
545
546 Send a pickled string to a socket.
547
548
549.. _syslog-handler:
550
551SysLogHandler
552^^^^^^^^^^^^^
553
554The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module,
555supports sending logging messages to a remote or local Unix syslog.
556
557
558.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
559
560 Returns a new instance of the :class:`SysLogHandler` class intended to
561 communicate with a remote Unix machine whose address is given by *address* in
562 the form of a ``(host, port)`` tuple. If *address* is not specified,
563 ``('localhost', 514)`` is used. The address is used to open a socket. An
564 alternative to providing a ``(host, port)`` tuple is providing an address as a
565 string, for example '/dev/log'. In this case, a Unix domain socket is used to
566 send the message to the syslog. If *facility* is not specified,
567 :const:`LOG_USER` is used. The type of socket opened depends on the
568 *socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus
569 opens a UDP socket. To open a TCP socket (for use with the newer syslog
570 daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`.
571
572 Note that if your server is not listening on UDP port 514,
573 :class:`SysLogHandler` may appear not to work. In that case, check what
574 address you should be using for a domain socket - it's system dependent.
575 For example, on Linux it's usually '/dev/log' but on OS/X it's
576 '/var/run/syslog'. You'll need to check your platform and use the
577 appropriate address (you may need to do this check at runtime if your
578 application needs to run on several platforms). On Windows, you pretty
579 much have to use the UDP option.
580
581 .. versionchanged:: 3.2
582 *socktype* was added.
583
584
585 .. method:: close()
586
587 Closes the socket to the remote host.
588
589
590 .. method:: emit(record)
591
592 The record is formatted, and then sent to the syslog server. If exception
593 information is present, it is *not* sent to the server.
594
Vinay Sajip645e4582011-06-10 18:52:50 +0100595 .. versionchanged:: 3.2.1
596 (See: :issue:`12168`.) In earlier versions, the message sent to the
597 syslog daemons was always terminated with a NUL byte, because early
598 versions of these daemons expected a NUL terminated message - even
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300599 though it's not in the relevant specification (:rfc:`5424`). More recent
Vinay Sajip645e4582011-06-10 18:52:50 +0100600 versions of these daemons don't expect the NUL byte but strip it off
601 if it's there, and even more recent daemons (which adhere more closely
602 to RFC 5424) pass the NUL byte on as part of the message.
603
604 To enable easier handling of syslog messages in the face of all these
605 differing daemon behaviours, the appending of the NUL byte has been
606 made configurable, through the use of a class-level attribute,
607 ``append_nul``. This defaults to ``True`` (preserving the existing
608 behaviour) but can be set to ``False`` on a ``SysLogHandler`` instance
609 in order for that instance to *not* append the NUL terminator.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000610
Vinay Sajip2353e352011-06-27 15:40:06 +0100611 .. versionchanged:: 3.3
612 (See: :issue:`12419`.) In earlier versions, there was no facility for
613 an "ident" or "tag" prefix to identify the source of the message. This
614 can now be specified using a class-level attribute, defaulting to
615 ``""`` to preserve existing behaviour, but which can be overridden on
616 a ``SysLogHandler`` instance in order for that instance to prepend
617 the ident to every message handled. Note that the provided ident must
618 be text, not bytes, and is prepended to the message exactly as is.
619
Vinay Sajipc63619b2010-12-19 12:56:57 +0000620 .. method:: encodePriority(facility, priority)
621
622 Encodes the facility and priority into an integer. You can pass in strings
623 or integers - if strings are passed, internal mapping dictionaries are
624 used to convert them to integers.
625
626 The symbolic ``LOG_`` values are defined in :class:`SysLogHandler` and
627 mirror the values defined in the ``sys/syslog.h`` header file.
628
629 **Priorities**
630
631 +--------------------------+---------------+
632 | Name (string) | Symbolic value|
633 +==========================+===============+
634 | ``alert`` | LOG_ALERT |
635 +--------------------------+---------------+
636 | ``crit`` or ``critical`` | LOG_CRIT |
637 +--------------------------+---------------+
638 | ``debug`` | LOG_DEBUG |
639 +--------------------------+---------------+
640 | ``emerg`` or ``panic`` | LOG_EMERG |
641 +--------------------------+---------------+
642 | ``err`` or ``error`` | LOG_ERR |
643 +--------------------------+---------------+
644 | ``info`` | LOG_INFO |
645 +--------------------------+---------------+
646 | ``notice`` | LOG_NOTICE |
647 +--------------------------+---------------+
648 | ``warn`` or ``warning`` | LOG_WARNING |
649 +--------------------------+---------------+
650
651 **Facilities**
652
653 +---------------+---------------+
654 | Name (string) | Symbolic value|
655 +===============+===============+
656 | ``auth`` | LOG_AUTH |
657 +---------------+---------------+
658 | ``authpriv`` | LOG_AUTHPRIV |
659 +---------------+---------------+
660 | ``cron`` | LOG_CRON |
661 +---------------+---------------+
662 | ``daemon`` | LOG_DAEMON |
663 +---------------+---------------+
664 | ``ftp`` | LOG_FTP |
665 +---------------+---------------+
666 | ``kern`` | LOG_KERN |
667 +---------------+---------------+
668 | ``lpr`` | LOG_LPR |
669 +---------------+---------------+
670 | ``mail`` | LOG_MAIL |
671 +---------------+---------------+
672 | ``news`` | LOG_NEWS |
673 +---------------+---------------+
674 | ``syslog`` | LOG_SYSLOG |
675 +---------------+---------------+
676 | ``user`` | LOG_USER |
677 +---------------+---------------+
678 | ``uucp`` | LOG_UUCP |
679 +---------------+---------------+
680 | ``local0`` | LOG_LOCAL0 |
681 +---------------+---------------+
682 | ``local1`` | LOG_LOCAL1 |
683 +---------------+---------------+
684 | ``local2`` | LOG_LOCAL2 |
685 +---------------+---------------+
686 | ``local3`` | LOG_LOCAL3 |
687 +---------------+---------------+
688 | ``local4`` | LOG_LOCAL4 |
689 +---------------+---------------+
690 | ``local5`` | LOG_LOCAL5 |
691 +---------------+---------------+
692 | ``local6`` | LOG_LOCAL6 |
693 +---------------+---------------+
694 | ``local7`` | LOG_LOCAL7 |
695 +---------------+---------------+
696
697 .. method:: mapPriority(levelname)
698
699 Maps a logging level name to a syslog priority name.
700 You may need to override this if you are using custom levels, or
701 if the default algorithm is not suitable for your needs. The
702 default algorithm maps ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR`` and
703 ``CRITICAL`` to the equivalent syslog names, and all other level
704 names to 'warning'.
705
706.. _nt-eventlog-handler:
707
708NTEventLogHandler
709^^^^^^^^^^^^^^^^^
710
711The :class:`NTEventLogHandler` class, located in the :mod:`logging.handlers`
712module, supports sending logging messages to a local Windows NT, Windows 2000 or
713Windows XP event log. Before you can use it, you need Mark Hammond's Win32
714extensions for Python installed.
715
716
717.. class:: NTEventLogHandler(appname, dllname=None, logtype='Application')
718
719 Returns a new instance of the :class:`NTEventLogHandler` class. The *appname* is
720 used to define the application name as it appears in the event log. An
721 appropriate registry entry is created using this name. The *dllname* should give
722 the fully qualified pathname of a .dll or .exe which contains message
723 definitions to hold in the log (if not specified, ``'win32service.pyd'`` is used
724 - this is installed with the Win32 extensions and contains some basic
725 placeholder message definitions. Note that use of these placeholders will make
726 your event logs big, as the entire message source is held in the log. If you
727 want slimmer logs, you have to pass in the name of your own .dll or .exe which
728 contains the message definitions you want to use in the event log). The
729 *logtype* is one of ``'Application'``, ``'System'`` or ``'Security'``, and
730 defaults to ``'Application'``.
731
732
733 .. method:: close()
734
735 At this point, you can remove the application name from the registry as a
736 source of event log entries. However, if you do this, you will not be able
737 to see the events as you intended in the Event Log Viewer - it needs to be
738 able to access the registry to get the .dll name. The current version does
739 not do this.
740
741
742 .. method:: emit(record)
743
744 Determines the message ID, event category and event type, and then logs
745 the message in the NT event log.
746
747
748 .. method:: getEventCategory(record)
749
750 Returns the event category for the record. Override this if you want to
751 specify your own categories. This version returns 0.
752
753
754 .. method:: getEventType(record)
755
756 Returns the event type for the record. Override this if you want to
757 specify your own types. This version does a mapping using the handler's
758 typemap attribute, which is set up in :meth:`__init__` to a dictionary
759 which contains mappings for :const:`DEBUG`, :const:`INFO`,
760 :const:`WARNING`, :const:`ERROR` and :const:`CRITICAL`. If you are using
761 your own levels, you will either need to override this method or place a
762 suitable dictionary in the handler's *typemap* attribute.
763
764
765 .. method:: getMessageID(record)
766
767 Returns the message ID for the record. If you are using your own messages,
768 you could do this by having the *msg* passed to the logger being an ID
769 rather than a format string. Then, in here, you could use a dictionary
770 lookup to get the message ID. This version returns 1, which is the base
771 message ID in :file:`win32service.pyd`.
772
773.. _smtp-handler:
774
775SMTPHandler
776^^^^^^^^^^^
777
778The :class:`SMTPHandler` class, located in the :mod:`logging.handlers` module,
779supports sending logging messages to an email address via SMTP.
780
781
Vinay Sajip38a12af2012-03-26 17:17:39 +0100782.. class:: SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000783
784 Returns a new instance of the :class:`SMTPHandler` class. The instance is
785 initialized with the from and to addresses and subject line of the email. The
786 *toaddrs* should be a list of strings. To specify a non-standard SMTP port, use
787 the (host, port) tuple format for the *mailhost* argument. If you use a string,
788 the standard SMTP port is used. If your SMTP server requires authentication, you
789 can specify a (username, password) tuple for the *credentials* argument.
790
Vinay Sajip95259562011-08-01 11:31:52 +0100791 To specify the use of a secure protocol (TLS), pass in a tuple to the
792 *secure* argument. This will only be used when authentication credentials are
793 supplied. The tuple should be either an empty tuple, or a single-value tuple
794 with the name of a keyfile, or a 2-value tuple with the names of the keyfile
795 and certificate file. (This tuple is passed to the
796 :meth:`smtplib.SMTP.starttls` method.)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000797
Vinay Sajip38a12af2012-03-26 17:17:39 +0100798 A timeout can be specified for communication with the SMTP server using the
799 *timeout* argument.
800
801 .. versionadded:: 3.3
802 The *timeout* argument was added.
803
Vinay Sajipc63619b2010-12-19 12:56:57 +0000804 .. method:: emit(record)
805
806 Formats the record and sends it to the specified addressees.
807
808
809 .. method:: getSubject(record)
810
811 If you want to specify a subject line which is record-dependent, override
812 this method.
813
814.. _memory-handler:
815
816MemoryHandler
817^^^^^^^^^^^^^
818
819The :class:`MemoryHandler` class, located in the :mod:`logging.handlers` module,
820supports buffering of logging records in memory, periodically flushing them to a
821:dfn:`target` handler. Flushing occurs whenever the buffer is full, or when an
822event of a certain severity or greater is seen.
823
824:class:`MemoryHandler` is a subclass of the more general
825:class:`BufferingHandler`, which is an abstract class. This buffers logging
826records in memory. Whenever each record is added to the buffer, a check is made
827by calling :meth:`shouldFlush` to see if the buffer should be flushed. If it
Vinay Sajip8ece80f2012-03-26 17:09:58 +0100828should, then :meth:`flush` is expected to do the flushing.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000829
830
831.. class:: BufferingHandler(capacity)
832
833 Initializes the handler with a buffer of the specified capacity.
834
835
836 .. method:: emit(record)
837
838 Appends the record to the buffer. If :meth:`shouldFlush` returns true,
839 calls :meth:`flush` to process the buffer.
840
841
842 .. method:: flush()
843
844 You can override this to implement custom flushing behavior. This version
845 just zaps the buffer to empty.
846
847
848 .. method:: shouldFlush(record)
849
850 Returns true if the buffer is up to capacity. This method can be
851 overridden to implement custom flushing strategies.
852
853
Vinay Sajipcccf6062016-07-22 16:27:31 +0100854.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000855
856 Returns a new instance of the :class:`MemoryHandler` class. The instance is
857 initialized with a buffer size of *capacity*. If *flushLevel* is not specified,
858 :const:`ERROR` is used. If no *target* is specified, the target will need to be
Vinay Sajipcccf6062016-07-22 16:27:31 +0100859 set using :meth:`setTarget` before this handler does anything useful. If
860 *flushOnClose* is specified as ``False``, then the buffer is *not* flushed when
861 the handler is closed. If not specified or specified as ``True``, the previous
862 behaviour of flushing the buffer will occur when the handler is closed.
863
864 .. versionchanged:: 3.6
865 The *flushOnClose* parameter was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000866
867
868 .. method:: close()
869
Ezio Melotti226231c2012-01-18 05:40:00 +0200870 Calls :meth:`flush`, sets the target to ``None`` and clears the
Vinay Sajipc63619b2010-12-19 12:56:57 +0000871 buffer.
872
873
874 .. method:: flush()
875
876 For a :class:`MemoryHandler`, flushing means just sending the buffered
877 records to the target, if there is one. The buffer is also cleared when
878 this happens. Override if you want different behavior.
879
880
881 .. method:: setTarget(target)
882
883 Sets the target handler for this handler.
884
885
886 .. method:: shouldFlush(record)
887
888 Checks for buffer full or a record at the *flushLevel* or higher.
889
890
891.. _http-handler:
892
893HTTPHandler
894^^^^^^^^^^^
895
896The :class:`HTTPHandler` class, located in the :mod:`logging.handlers` module,
897supports sending logging messages to a Web server, using either ``GET`` or
898``POST`` semantics.
899
900
Benjamin Peterson43052a12014-11-23 20:36:44 -0600901.. class:: HTTPHandler(host, url, method='GET', secure=False, credentials=None, context=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000902
903 Returns a new instance of the :class:`HTTPHandler` class. The *host* can be
Benjamin Peterson43052a12014-11-23 20:36:44 -0600904 of the form ``host:port``, should you need to use a specific port number. If
905 no *method* is specified, ``GET`` is used. If *secure* is true, a HTTPS
906 connection will be used. The *context* parameter may be set to a
907 :class:`ssl.SSLContext` instance to configure the SSL settings used for the
908 HTTPS connection. If *credentials* is specified, it should be a 2-tuple
909 consisting of userid and password, which will be placed in a HTTP
Vinay Sajipc63619b2010-12-19 12:56:57 +0000910 'Authorization' header using Basic authentication. If you specify
911 credentials, you should also specify secure=True so that your userid and
912 password are not passed in cleartext across the wire.
913
Benjamin Petersona90e92d2014-11-23 20:38:37 -0600914 .. versionchanged:: 3.5
Benjamin Peterson43052a12014-11-23 20:36:44 -0600915 The *context* parameter was added.
916
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100917 .. method:: mapLogRecord(record)
918
919 Provides a dictionary, based on ``record``, which is to be URL-encoded
920 and sent to the web server. The default implementation just returns
921 ``record.__dict__``. This method can be overridden if e.g. only a
922 subset of :class:`~logging.LogRecord` is to be sent to the web server, or
923 if more specific customization of what's sent to the server is required.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000924
925 .. method:: emit(record)
926
Martin Panter6245cb32016-04-15 02:14:19 +0000927 Sends the record to the Web server as a URL-encoded dictionary. The
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100928 :meth:`mapLogRecord` method is used to convert the record to the
929 dictionary to be sent.
930
Berker Peksag9c1dba22014-09-28 00:00:58 +0300931 .. note:: Since preparing a record for sending it to a Web server is not
Vinay Sajipc673a9a2014-05-30 18:59:27 +0100932 the same as a generic formatting operation, using
933 :meth:`~logging.Handler.setFormatter` to specify a
934 :class:`~logging.Formatter` for a :class:`HTTPHandler` has no effect.
935 Instead of calling :meth:`~logging.Handler.format`, this handler calls
936 :meth:`mapLogRecord` and then :func:`urllib.parse.urlencode` to encode the
937 dictionary in a form suitable for sending to a Web server.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000938
939
940.. _queue-handler:
941
942
943QueueHandler
944^^^^^^^^^^^^
945
946.. versionadded:: 3.2
947
948The :class:`QueueHandler` class, located in the :mod:`logging.handlers` module,
949supports sending logging messages to a queue, such as those implemented in the
950:mod:`queue` or :mod:`multiprocessing` modules.
951
952Along with the :class:`QueueListener` class, :class:`QueueHandler` can be used
953to let handlers do their work on a separate thread from the one which does the
954logging. This is important in Web applications and also other service
955applications where threads servicing clients need to respond as quickly as
956possible, while any potentially slow operations (such as sending an email via
957:class:`SMTPHandler`) are done on a separate thread.
958
959.. class:: QueueHandler(queue)
960
961 Returns a new instance of the :class:`QueueHandler` class. The instance is
Martin Panter8f137832017-01-14 08:24:20 +0000962 initialized with the queue to send messages to. The queue can be any
963 queue-like object; it's used as-is by the :meth:`enqueue` method, which needs
Vinay Sajipc63619b2010-12-19 12:56:57 +0000964 to know how to send messages to it.
965
966
967 .. method:: emit(record)
968
969 Enqueues the result of preparing the LogRecord.
970
971 .. method:: prepare(record)
972
973 Prepares a record for queuing. The object returned by this
974 method is enqueued.
975
976 The base implementation formats the record to merge the message
977 and arguments, and removes unpickleable items from the record
978 in-place.
979
980 You might want to override this method if you want to convert
981 the record to a dict or JSON string, or send a modified copy
982 of the record while leaving the original intact.
983
984 .. method:: enqueue(record)
985
986 Enqueues the record on the queue using ``put_nowait()``; you may
987 want to override this if you want to use blocking behaviour, or a
Vinay Sajip9c10d6b2013-11-15 20:58:13 +0000988 timeout, or a customized queue implementation.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000989
990
991
Éric Araujo5eada942011-08-19 00:41:23 +0200992.. _queue-listener:
Vinay Sajipc63619b2010-12-19 12:56:57 +0000993
994QueueListener
995^^^^^^^^^^^^^
996
997.. versionadded:: 3.2
998
999The :class:`QueueListener` class, located in the :mod:`logging.handlers`
1000module, supports receiving logging messages from a queue, such as those
1001implemented in the :mod:`queue` or :mod:`multiprocessing` modules. The
1002messages are received from a queue in an internal thread and passed, on
1003the same thread, to one or more handlers for processing. While
1004:class:`QueueListener` is not itself a handler, it is documented here
1005because it works hand-in-hand with :class:`QueueHandler`.
1006
1007Along with the :class:`QueueHandler` class, :class:`QueueListener` can be used
1008to let handlers do their work on a separate thread from the one which does the
1009logging. This is important in Web applications and also other service
1010applications where threads servicing clients need to respond as quickly as
1011possible, while any potentially slow operations (such as sending an email via
1012:class:`SMTPHandler`) are done on a separate thread.
1013
Vinay Sajip365701a2015-02-09 19:49:00 +00001014.. class:: QueueListener(queue, *handlers, respect_handler_level=False)
Vinay Sajipc63619b2010-12-19 12:56:57 +00001015
1016 Returns a new instance of the :class:`QueueListener` class. The instance is
1017 initialized with the queue to send messages to and a list of handlers which
Martin Panter8f137832017-01-14 08:24:20 +00001018 will handle entries placed on the queue. The queue can be any queue-like
1019 object; it's passed as-is to the :meth:`dequeue` method, which needs
Vinay Sajip365701a2015-02-09 19:49:00 +00001020 to know how to get messages from it. If ``respect_handler_level`` is ``True``,
1021 a handler's level is respected (compared with the level for the message) when
1022 deciding whether to pass messages to that handler; otherwise, the behaviour
1023 is as in previous Python versions - to always pass each message to each
1024 handler.
1025
1026 .. versionchanged:: 3.5
1027 The ``respect_handler_levels`` argument was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +00001028
1029 .. method:: dequeue(block)
1030
1031 Dequeues a record and return it, optionally blocking.
1032
1033 The base implementation uses ``get()``. You may want to override this
1034 method if you want to use timeouts or work with custom queue
1035 implementations.
1036
1037 .. method:: prepare(record)
1038
1039 Prepare a record for handling.
1040
1041 This implementation just returns the passed-in record. You may want to
1042 override this method if you need to do any custom marshalling or
1043 manipulation of the record before passing it to the handlers.
1044
1045 .. method:: handle(record)
1046
1047 Handle a record.
1048
1049 This just loops through the handlers offering them the record
1050 to handle. The actual object passed to the handlers is that which
1051 is returned from :meth:`prepare`.
1052
1053 .. method:: start()
1054
1055 Starts the listener.
1056
1057 This starts up a background thread to monitor the queue for
1058 LogRecords to process.
1059
1060 .. method:: stop()
1061
1062 Stops the listener.
1063
1064 This asks the thread to terminate, and then waits for it to do so.
1065 Note that if you don't call this before your application exits, there
1066 may be some records still left on the queue, which won't be processed.
1067
Vinay Sajipa29a9dd2011-02-25 16:05:26 +00001068 .. method:: enqueue_sentinel()
1069
1070 Writes a sentinel to the queue to tell the listener to quit. This
1071 implementation uses ``put_nowait()``. You may want to override this
1072 method if you want to use timeouts or work with custom queue
1073 implementations.
1074
1075 .. versionadded:: 3.3
1076
Vinay Sajipc63619b2010-12-19 12:56:57 +00001077
1078.. seealso::
1079
1080 Module :mod:`logging`
1081 API reference for the logging module.
1082
1083 Module :mod:`logging.config`
1084 Configuration API for the logging module.
1085
1086