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