blob: 534c35c0e106d25c44cade92f9d5035c5a9f3b5b [file] [log] [blame]
Vinay Sajip5dbca9c2011-04-08 11:40:38 +01001:mod:`logging.handlers` --- Logging handlers
2============================================
3
4.. module:: logging.handlers
5 :synopsis: Handlers for the logging module.
6
7
8.. moduleauthor:: Vinay Sajip <vinay_sajip@red-dove.com>
9.. sectionauthor:: Vinay Sajip <vinay_sajip@red-dove.com>
10
11.. sidebar:: Important
12
13 This page contains only reference information. For tutorials,
14 please see
15
16 * :ref:`Basic Tutorial <logging-basic-tutorial>`
17 * :ref:`Advanced Tutorial <logging-advanced-tutorial>`
18 * :ref:`Logging Cookbook <logging-cookbook>`
19
20.. currentmodule:: logging
21
22The following useful handlers are provided in the package. Note that three of
23the handlers (:class:`StreamHandler`, :class:`FileHandler` and
24:class:`NullHandler`) are actually defined in the :mod:`logging` module itself,
25but have been documented here along with the other handlers.
26
27.. _stream-handler:
28
29StreamHandler
30^^^^^^^^^^^^^
31
32The :class:`StreamHandler` class, located in the core :mod:`logging` package,
33sends logging output to streams such as *sys.stdout*, *sys.stderr* or any
34file-like object (or, more precisely, any object which supports :meth:`write`
35and :meth:`flush` methods).
36
37
38.. class:: StreamHandler(stream=None)
39
40 Returns a new instance of the :class:`StreamHandler` class. If *stream* is
41 specified, the instance will use it for logging output; otherwise, *sys.stderr*
42 will be used.
43
44
45 .. method:: emit(record)
46
47 If a formatter is specified, it is used to format the record. The record
48 is then written to the stream with a newline terminator. If exception
49 information is present, it is formatted using
50 :func:`traceback.print_exception` and appended to the stream.
51
52
53 .. method:: flush()
54
55 Flushes the stream by calling its :meth:`flush` method. Note that the
Vinay Sajip10b51302013-08-17 00:38:48 +010056 :meth:`close` method is inherited from :class:`~logging.Handler` and so
57 does no output, so an explicit :meth:`flush` call may be needed at times.
Vinay Sajip5dbca9c2011-04-08 11:40:38 +010058
59.. _file-handler:
60
61FileHandler
62^^^^^^^^^^^
63
64The :class:`FileHandler` class, located in the core :mod:`logging` package,
65sends logging output to a disk file. It inherits the output functionality from
66:class:`StreamHandler`.
67
68
69.. class:: FileHandler(filename, mode='a', encoding=None, delay=False)
70
71 Returns a new instance of the :class:`FileHandler` class. The specified file is
72 opened and used as the stream for logging. If *mode* is not specified,
73 :const:`'a'` is used. If *encoding* is not *None*, it is used to open the file
74 with that encoding. If *delay* is true, then file opening is deferred until the
75 first call to :meth:`emit`. By default, the file grows indefinitely.
76
77 .. versionchanged:: 2.6
78 *delay* was added.
79
80 .. method:: close()
81
82 Closes the file.
83
84
85 .. method:: emit(record)
86
87 Outputs the record to the file.
88
89
90.. _null-handler:
91
92NullHandler
93^^^^^^^^^^^
94
95.. versionadded:: 2.7
96
97The :class:`NullHandler` class, located in the core :mod:`logging` package,
98does not do any formatting or output. It is essentially a 'no-op' handler
99for use by library developers.
100
101.. class:: NullHandler()
102
103 Returns a new instance of the :class:`NullHandler` class.
104
105 .. method:: emit(record)
106
107 This method does nothing.
108
109 .. method:: handle(record)
110
111 This method does nothing.
112
113 .. method:: createLock()
114
115 This method returns ``None`` for the lock, since there is no
116 underlying I/O to which access needs to be serialized.
117
118
119See :ref:`library-config` for more information on how to use
120:class:`NullHandler`.
121
122.. _watched-file-handler:
123
124WatchedFileHandler
125^^^^^^^^^^^^^^^^^^
126
127.. currentmodule:: logging.handlers
128
129.. versionadded:: 2.6
130
131The :class:`WatchedFileHandler` class, located in the :mod:`logging.handlers`
132module, is a :class:`FileHandler` which watches the file it is logging to. If
133the file changes, it is closed and reopened using the file name.
134
135A file change can happen because of usage of programs such as *newsyslog* and
136*logrotate* which perform log file rotation. This handler, intended for use
137under Unix/Linux, watches the file to see if it has changed since the last emit.
138(A file is deemed to have changed if its device or inode have changed.) If the
139file has changed, the old file stream is closed, and the file opened to get a
140new stream.
141
142This handler is not appropriate for use under Windows, because under Windows
143open log files cannot be moved or renamed - logging opens the files with
144exclusive locks - and so there is no need for such a handler. Furthermore,
Vinay Sajip10b51302013-08-17 00:38:48 +0100145*ST_INO* is not supported under Windows; :func:`~os.stat` always returns zero
146for this value.
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100147
148
149.. class:: WatchedFileHandler(filename[,mode[, encoding[, delay]]])
150
151 Returns a new instance of the :class:`WatchedFileHandler` class. The specified
152 file is opened and used as the stream for logging. If *mode* is not specified,
153 :const:`'a'` is used. If *encoding* is not *None*, it is used to open the file
154 with that encoding. If *delay* is true, then file opening is deferred until the
155 first call to :meth:`emit`. By default, the file grows indefinitely.
156
157
158 .. method:: emit(record)
159
160 Outputs the record to the file, but first checks to see if the file has
161 changed. If it has, the existing stream is flushed and closed and the
162 file opened again, before outputting the record to the file.
163
164.. _rotating-file-handler:
165
166RotatingFileHandler
167^^^^^^^^^^^^^^^^^^^
168
169The :class:`RotatingFileHandler` class, located in the :mod:`logging.handlers`
170module, supports rotation of disk log files.
171
172
173.. class:: RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)
174
175 Returns a new instance of the :class:`RotatingFileHandler` class. The specified
176 file is opened and used as the stream for logging. If *mode* is not specified,
177 ``'a'`` is used. If *encoding* is not *None*, it is used to open the file
178 with that encoding. If *delay* is true, then file opening is deferred until the
179 first call to :meth:`emit`. By default, the file grows indefinitely.
180
181 You can use the *maxBytes* and *backupCount* values to allow the file to
182 :dfn:`rollover` at a predetermined size. When the size is about to be exceeded,
183 the file is closed and a new file is silently opened for output. Rollover occurs
184 whenever the current log file is nearly *maxBytes* in length; if *maxBytes* is
185 zero, rollover never occurs. If *backupCount* is non-zero, the system will save
186 old log files by appending the extensions '.1', '.2' etc., to the filename. For
187 example, with a *backupCount* of 5 and a base file name of :file:`app.log`, you
188 would get :file:`app.log`, :file:`app.log.1`, :file:`app.log.2`, up to
189 :file:`app.log.5`. The file being written to is always :file:`app.log`. When
190 this file is filled, it is closed and renamed to :file:`app.log.1`, and if files
191 :file:`app.log.1`, :file:`app.log.2`, etc. exist, then they are renamed to
192 :file:`app.log.2`, :file:`app.log.3` etc. respectively.
193
194 .. versionchanged:: 2.6
195 *delay* was added.
196
197
198 .. method:: doRollover()
199
200 Does a rollover, as described above.
201
202
203 .. method:: emit(record)
204
205 Outputs the record to the file, catering for rollover as described
206 previously.
207
208.. _timed-rotating-file-handler:
209
210TimedRotatingFileHandler
211^^^^^^^^^^^^^^^^^^^^^^^^
212
213The :class:`TimedRotatingFileHandler` class, located in the
214:mod:`logging.handlers` module, supports rotation of disk log files at certain
215timed intervals.
216
217
218.. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
219
220 Returns a new instance of the :class:`TimedRotatingFileHandler` class. The
221 specified file is opened and used as the stream for logging. On rotating it also
222 sets the filename suffix. Rotating happens based on the product of *when* and
223 *interval*.
224
225 You can use the *when* to specify the type of *interval*. The list of possible
226 values is below. Note that they are not case sensitive.
227
228 +----------------+-----------------------+
229 | Value | Type of interval |
230 +================+=======================+
231 | ``'S'`` | Seconds |
232 +----------------+-----------------------+
233 | ``'M'`` | Minutes |
234 +----------------+-----------------------+
235 | ``'H'`` | Hours |
236 +----------------+-----------------------+
237 | ``'D'`` | Days |
238 +----------------+-----------------------+
Vinay Sajipa7b584b2013-03-08 23:22:22 +0000239 | ``'W0'-'W6'`` | Weekday (0=Monday) |
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100240 +----------------+-----------------------+
241 | ``'midnight'`` | Roll over at midnight |
242 +----------------+-----------------------+
243
Vinay Sajipa7b584b2013-03-08 23:22:22 +0000244 When using weekday-based rotation, specify 'W0' for Monday, 'W1' for
245 Tuesday, and so on up to 'W6' for Sunday. In this case, the value passed for
246 *interval* isn't used.
247
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100248 The system will save old log files by appending extensions to the filename.
249 The extensions are date-and-time based, using the strftime format
250 ``%Y-%m-%d_%H-%M-%S`` or a leading portion thereof, depending on the
251 rollover interval.
252
253 When computing the next rollover time for the first time (when the handler
254 is created), the last modification time of an existing log file, or else
255 the current time, is used to compute when the next rotation will occur.
256
257 If the *utc* argument is true, times in UTC will be used; otherwise
258 local time is used.
259
260 If *backupCount* is nonzero, at most *backupCount* files
261 will be kept, and if more would be created when rollover occurs, the oldest
262 one is deleted. The deletion logic uses the interval to determine which
263 files to delete, so changing the interval may leave old files lying around.
264
265 If *delay* is true, then file opening is deferred until the first call to
266 :meth:`emit`.
267
268 .. versionchanged:: 2.6
Vinay Sajip5df091a2011-11-06 22:37:17 +0000269 *delay* and *utc* were added.
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100270
271
272 .. method:: doRollover()
273
274 Does a rollover, as described above.
275
276
277 .. method:: emit(record)
278
279 Outputs the record to the file, catering for rollover as described above.
280
281
282.. _socket-handler:
283
284SocketHandler
285^^^^^^^^^^^^^
286
287The :class:`SocketHandler` class, located in the :mod:`logging.handlers` module,
288sends logging output to a network socket. The base class uses a TCP socket.
289
290
291.. class:: SocketHandler(host, port)
292
293 Returns a new instance of the :class:`SocketHandler` class intended to
294 communicate with a remote machine whose address is given by *host* and *port*.
295
296
297 .. method:: close()
298
299 Closes the socket.
300
301
302 .. method:: emit()
303
304 Pickles the record's attribute dictionary and writes it to the socket in
305 binary format. If there is an error with the socket, silently drops the
306 packet. If the connection was previously lost, re-establishes the
307 connection. To unpickle the record at the receiving end into a
Vinay Sajip10b51302013-08-17 00:38:48 +0100308 :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
309 function.
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100310
311
312 .. method:: handleError()
313
314 Handles an error which has occurred during :meth:`emit`. The most likely
315 cause is a lost connection. Closes the socket so that we can retry on the
316 next event.
317
318
319 .. method:: makeSocket()
320
321 This is a factory method which allows subclasses to define the precise
322 type of socket they want. The default implementation creates a TCP socket
323 (:const:`socket.SOCK_STREAM`).
324
325
326 .. method:: makePickle(record)
327
328 Pickles the record's attribute dictionary in binary format with a length
329 prefix, and returns it ready for transmission across the socket.
330
331 Note that pickles aren't completely secure. If you are concerned about
332 security, you may want to override this method to implement a more secure
333 mechanism. For example, you can sign pickles using HMAC and then verify
334 them on the receiving end, or alternatively you can disable unpickling of
335 global objects on the receiving end.
336
337
338 .. method:: send(packet)
339
340 Send a pickled string *packet* to the socket. This function allows for
341 partial sends which can happen when the network is busy.
342
343
344 .. method:: createSocket()
345
346 Tries to create a socket; on failure, uses an exponential back-off
347 algorithm. On intial failure, the handler will drop the message it was
348 trying to send. When subsequent messages are handled by the same
349 instance, it will not try connecting until some time has passed. The
350 default parameters are such that the initial delay is one second, and if
351 after that delay the connection still can't be made, the handler will
352 double the delay each time up to a maximum of 30 seconds.
353
354 This behaviour is controlled by the following handler attributes:
355
356 * ``retryStart`` (initial delay, defaulting to 1.0 seconds).
357 * ``retryFactor`` (multiplier, defaulting to 2.0).
358 * ``retryMax`` (maximum delay, defaulting to 30.0 seconds).
359
360 This means that if the remote listener starts up *after* the handler has
361 been used, you could lose messages (since the handler won't even attempt
362 a connection until the delay has elapsed, but just silently drop messages
363 during the delay period).
364
365
366.. _datagram-handler:
367
368DatagramHandler
369^^^^^^^^^^^^^^^
370
371The :class:`DatagramHandler` class, located in the :mod:`logging.handlers`
372module, inherits from :class:`SocketHandler` to support sending logging messages
373over UDP sockets.
374
375
376.. class:: DatagramHandler(host, port)
377
378 Returns a new instance of the :class:`DatagramHandler` class intended to
379 communicate with a remote machine whose address is given by *host* and *port*.
380
381
382 .. method:: emit()
383
384 Pickles the record's attribute dictionary and writes it to the socket in
385 binary format. If there is an error with the socket, silently drops the
386 packet. To unpickle the record at the receiving end into a
Vinay Sajip10b51302013-08-17 00:38:48 +0100387 :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
388 function.
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100389
390
391 .. method:: makeSocket()
392
393 The factory method of :class:`SocketHandler` is here overridden to create
394 a UDP socket (:const:`socket.SOCK_DGRAM`).
395
396
397 .. method:: send(s)
398
399 Send a pickled string to a socket.
400
401
402.. _syslog-handler:
403
404SysLogHandler
405^^^^^^^^^^^^^
406
407The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module,
408supports sending logging messages to a remote or local Unix syslog.
409
410
411.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
412
413 Returns a new instance of the :class:`SysLogHandler` class intended to
414 communicate with a remote Unix machine whose address is given by *address* in
415 the form of a ``(host, port)`` tuple. If *address* is not specified,
416 ``('localhost', 514)`` is used. The address is used to open a socket. An
417 alternative to providing a ``(host, port)`` tuple is providing an address as a
418 string, for example '/dev/log'. In this case, a Unix domain socket is used to
419 send the message to the syslog. If *facility* is not specified,
420 :const:`LOG_USER` is used. The type of socket opened depends on the
421 *socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus
422 opens a UDP socket. To open a TCP socket (for use with the newer syslog
423 daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`.
424
425 Note that if your server is not listening on UDP port 514,
426 :class:`SysLogHandler` may appear not to work. In that case, check what
427 address you should be using for a domain socket - it's system dependent.
428 For example, on Linux it's usually '/dev/log' but on OS/X it's
429 '/var/run/syslog'. You'll need to check your platform and use the
430 appropriate address (you may need to do this check at runtime if your
431 application needs to run on several platforms). On Windows, you pretty
432 much have to use the UDP option.
433
434 .. versionchanged:: 2.7
435 *socktype* was added.
436
437
438 .. method:: close()
439
440 Closes the socket to the remote host.
441
442
443 .. method:: emit(record)
444
445 The record is formatted, and then sent to the syslog server. If exception
446 information is present, it is *not* sent to the server.
447
448
449 .. method:: encodePriority(facility, priority)
450
451 Encodes the facility and priority into an integer. You can pass in strings
452 or integers - if strings are passed, internal mapping dictionaries are
453 used to convert them to integers.
454
455 The symbolic ``LOG_`` values are defined in :class:`SysLogHandler` and
456 mirror the values defined in the ``sys/syslog.h`` header file.
457
458 **Priorities**
459
460 +--------------------------+---------------+
461 | Name (string) | Symbolic value|
462 +==========================+===============+
463 | ``alert`` | LOG_ALERT |
464 +--------------------------+---------------+
465 | ``crit`` or ``critical`` | LOG_CRIT |
466 +--------------------------+---------------+
467 | ``debug`` | LOG_DEBUG |
468 +--------------------------+---------------+
469 | ``emerg`` or ``panic`` | LOG_EMERG |
470 +--------------------------+---------------+
471 | ``err`` or ``error`` | LOG_ERR |
472 +--------------------------+---------------+
473 | ``info`` | LOG_INFO |
474 +--------------------------+---------------+
475 | ``notice`` | LOG_NOTICE |
476 +--------------------------+---------------+
477 | ``warn`` or ``warning`` | LOG_WARNING |
478 +--------------------------+---------------+
479
480 **Facilities**
481
482 +---------------+---------------+
483 | Name (string) | Symbolic value|
484 +===============+===============+
485 | ``auth`` | LOG_AUTH |
486 +---------------+---------------+
487 | ``authpriv`` | LOG_AUTHPRIV |
488 +---------------+---------------+
489 | ``cron`` | LOG_CRON |
490 +---------------+---------------+
491 | ``daemon`` | LOG_DAEMON |
492 +---------------+---------------+
493 | ``ftp`` | LOG_FTP |
494 +---------------+---------------+
495 | ``kern`` | LOG_KERN |
496 +---------------+---------------+
497 | ``lpr`` | LOG_LPR |
498 +---------------+---------------+
499 | ``mail`` | LOG_MAIL |
500 +---------------+---------------+
501 | ``news`` | LOG_NEWS |
502 +---------------+---------------+
503 | ``syslog`` | LOG_SYSLOG |
504 +---------------+---------------+
505 | ``user`` | LOG_USER |
506 +---------------+---------------+
507 | ``uucp`` | LOG_UUCP |
508 +---------------+---------------+
509 | ``local0`` | LOG_LOCAL0 |
510 +---------------+---------------+
511 | ``local1`` | LOG_LOCAL1 |
512 +---------------+---------------+
513 | ``local2`` | LOG_LOCAL2 |
514 +---------------+---------------+
515 | ``local3`` | LOG_LOCAL3 |
516 +---------------+---------------+
517 | ``local4`` | LOG_LOCAL4 |
518 +---------------+---------------+
519 | ``local5`` | LOG_LOCAL5 |
520 +---------------+---------------+
521 | ``local6`` | LOG_LOCAL6 |
522 +---------------+---------------+
523 | ``local7`` | LOG_LOCAL7 |
524 +---------------+---------------+
525
526 .. method:: mapPriority(levelname)
527
528 Maps a logging level name to a syslog priority name.
529 You may need to override this if you are using custom levels, or
530 if the default algorithm is not suitable for your needs. The
531 default algorithm maps ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR`` and
532 ``CRITICAL`` to the equivalent syslog names, and all other level
533 names to 'warning'.
534
535.. _nt-eventlog-handler:
536
537NTEventLogHandler
538^^^^^^^^^^^^^^^^^
539
540The :class:`NTEventLogHandler` class, located in the :mod:`logging.handlers`
541module, supports sending logging messages to a local Windows NT, Windows 2000 or
542Windows XP event log. Before you can use it, you need Mark Hammond's Win32
543extensions for Python installed.
544
545
546.. class:: NTEventLogHandler(appname, dllname=None, logtype='Application')
547
548 Returns a new instance of the :class:`NTEventLogHandler` class. The *appname* is
549 used to define the application name as it appears in the event log. An
550 appropriate registry entry is created using this name. The *dllname* should give
551 the fully qualified pathname of a .dll or .exe which contains message
552 definitions to hold in the log (if not specified, ``'win32service.pyd'`` is used
553 - this is installed with the Win32 extensions and contains some basic
554 placeholder message definitions. Note that use of these placeholders will make
555 your event logs big, as the entire message source is held in the log. If you
556 want slimmer logs, you have to pass in the name of your own .dll or .exe which
557 contains the message definitions you want to use in the event log). The
558 *logtype* is one of ``'Application'``, ``'System'`` or ``'Security'``, and
559 defaults to ``'Application'``.
560
561
562 .. method:: close()
563
564 At this point, you can remove the application name from the registry as a
565 source of event log entries. However, if you do this, you will not be able
566 to see the events as you intended in the Event Log Viewer - it needs to be
567 able to access the registry to get the .dll name. The current version does
568 not do this.
569
570
571 .. method:: emit(record)
572
573 Determines the message ID, event category and event type, and then logs
574 the message in the NT event log.
575
576
577 .. method:: getEventCategory(record)
578
579 Returns the event category for the record. Override this if you want to
580 specify your own categories. This version returns 0.
581
582
583 .. method:: getEventType(record)
584
585 Returns the event type for the record. Override this if you want to
586 specify your own types. This version does a mapping using the handler's
587 typemap attribute, which is set up in :meth:`__init__` to a dictionary
588 which contains mappings for :const:`DEBUG`, :const:`INFO`,
589 :const:`WARNING`, :const:`ERROR` and :const:`CRITICAL`. If you are using
590 your own levels, you will either need to override this method or place a
591 suitable dictionary in the handler's *typemap* attribute.
592
593
594 .. method:: getMessageID(record)
595
596 Returns the message ID for the record. If you are using your own messages,
597 you could do this by having the *msg* passed to the logger being an ID
598 rather than a format string. Then, in here, you could use a dictionary
599 lookup to get the message ID. This version returns 1, which is the base
600 message ID in :file:`win32service.pyd`.
601
602.. _smtp-handler:
603
604SMTPHandler
605^^^^^^^^^^^
606
607The :class:`SMTPHandler` class, located in the :mod:`logging.handlers` module,
608supports sending logging messages to an email address via SMTP.
609
610
611.. class:: SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None)
612
613 Returns a new instance of the :class:`SMTPHandler` class. The instance is
614 initialized with the from and to addresses and subject line of the email.
615 The *toaddrs* should be a list of strings. To specify a non-standard SMTP
616 port, use the (host, port) tuple format for the *mailhost* argument. If you
617 use a string, the standard SMTP port is used. If your SMTP server requires
618 authentication, you can specify a (username, password) tuple for the
Vinay Sajip5d09ba42011-08-01 11:28:02 +0100619 *credentials* argument.
620
621 To specify the use of a secure protocol (TLS), pass in a tuple to the
622 *secure* argument. This will only be used when authentication credentials are
623 supplied. The tuple should be either an empty tuple, or a single-value tuple
624 with the name of a keyfile, or a 2-value tuple with the names of the keyfile
625 and certificate file. (This tuple is passed to the
626 :meth:`smtplib.SMTP.starttls` method.)
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100627
628 .. versionchanged:: 2.6
629 *credentials* was added.
630
631 .. versionchanged:: 2.7
632 *secure* was added.
633
634
635 .. method:: emit(record)
636
637 Formats the record and sends it to the specified addressees.
638
639
640 .. method:: getSubject(record)
641
642 If you want to specify a subject line which is record-dependent, override
643 this method.
644
645.. _memory-handler:
646
647MemoryHandler
648^^^^^^^^^^^^^
649
650The :class:`MemoryHandler` class, located in the :mod:`logging.handlers` module,
651supports buffering of logging records in memory, periodically flushing them to a
652:dfn:`target` handler. Flushing occurs whenever the buffer is full, or when an
653event of a certain severity or greater is seen.
654
655:class:`MemoryHandler` is a subclass of the more general
656:class:`BufferingHandler`, which is an abstract class. This buffers logging
657records in memory. Whenever each record is added to the buffer, a check is made
658by calling :meth:`shouldFlush` to see if the buffer should be flushed. If it
Vinay Sajip49d5fba2012-03-26 17:06:44 +0100659should, then :meth:`flush` is expected to do the flushing.
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100660
661
662.. class:: BufferingHandler(capacity)
663
664 Initializes the handler with a buffer of the specified capacity.
665
666
667 .. method:: emit(record)
668
669 Appends the record to the buffer. If :meth:`shouldFlush` returns true,
670 calls :meth:`flush` to process the buffer.
671
672
673 .. method:: flush()
674
675 You can override this to implement custom flushing behavior. This version
676 just zaps the buffer to empty.
677
678
679 .. method:: shouldFlush(record)
680
681 Returns true if the buffer is up to capacity. This method can be
682 overridden to implement custom flushing strategies.
683
684
685.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None)
686
687 Returns a new instance of the :class:`MemoryHandler` class. The instance is
688 initialized with a buffer size of *capacity*. If *flushLevel* is not specified,
689 :const:`ERROR` is used. If no *target* is specified, the target will need to be
690 set using :meth:`setTarget` before this handler does anything useful.
691
692
693 .. method:: close()
694
695 Calls :meth:`flush`, sets the target to :const:`None` and clears the
696 buffer.
697
698
699 .. method:: flush()
700
701 For a :class:`MemoryHandler`, flushing means just sending the buffered
702 records to the target, if there is one. The buffer is also cleared when
703 this happens. Override if you want different behavior.
704
705
706 .. method:: setTarget(target)
Vinay Sajip5dbca9c2011-04-08 11:40:38 +0100707
708 Sets the target handler for this handler.
709
710
711 .. method:: shouldFlush(record)
712
713 Checks for buffer full or a record at the *flushLevel* or higher.
714
715
716.. _http-handler:
717
718HTTPHandler
719^^^^^^^^^^^
720
721The :class:`HTTPHandler` class, located in the :mod:`logging.handlers` module,
722supports sending logging messages to a Web server, using either ``GET`` or
723``POST`` semantics.
724
725
726.. class:: HTTPHandler(host, url, method='GET')
727
728 Returns a new instance of the :class:`HTTPHandler` class. The *host* can be
729 of the form ``host:port``, should you need to use a specific port number.
730 If no *method* is specified, ``GET`` is used.
731
732
733 .. method:: emit(record)
734
735 Sends the record to the Web server as a percent-encoded dictionary.
736
737
738.. seealso::
739
740 Module :mod:`logging`
741 API reference for the logging module.
742
743 Module :mod:`logging.config`
744 Configuration API for the logging module.
745
746