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