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