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