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 | +----------------+-----------------------+ |
Vinay Sajip | a7b584b | 2013-03-08 23:22:22 +0000 | [diff] [blame] | 239 | | ``'W0'-'W6'`` | Weekday (0=Monday) | |
Vinay Sajip | 5dbca9c | 2011-04-08 11:40:38 +0100 | [diff] [blame] | 240 | +----------------+-----------------------+ |
| 241 | | ``'midnight'`` | Roll over at midnight | |
| 242 | +----------------+-----------------------+ |
| 243 | |
Vinay Sajip | a7b584b | 2013-03-08 23:22:22 +0000 | [diff] [blame] | 244 | 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 Sajip | 5dbca9c | 2011-04-08 11:40:38 +0100 | [diff] [blame] | 248 | 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 Sajip | 5df091a | 2011-11-06 22:37:17 +0000 | [diff] [blame] | 269 | *delay* and *utc* were added. |
Vinay Sajip | 5dbca9c | 2011-04-08 11:40:38 +0100 | [diff] [blame] | 270 | |
| 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 | |
| 284 | SocketHandler |
| 285 | ^^^^^^^^^^^^^ |
| 286 | |
| 287 | The :class:`SocketHandler` class, located in the :mod:`logging.handlers` module, |
| 288 | sends 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 |
| 308 | :class:`LogRecord`, use the :func:`makeLogRecord` function. |
| 309 | |
| 310 | |
| 311 | .. method:: handleError() |
| 312 | |
| 313 | Handles an error which has occurred during :meth:`emit`. The most likely |
| 314 | cause is a lost connection. Closes the socket so that we can retry on the |
| 315 | next event. |
| 316 | |
| 317 | |
| 318 | .. method:: makeSocket() |
| 319 | |
| 320 | This is a factory method which allows subclasses to define the precise |
| 321 | type of socket they want. The default implementation creates a TCP socket |
| 322 | (:const:`socket.SOCK_STREAM`). |
| 323 | |
| 324 | |
| 325 | .. method:: makePickle(record) |
| 326 | |
| 327 | Pickles the record's attribute dictionary in binary format with a length |
| 328 | prefix, and returns it ready for transmission across the socket. |
| 329 | |
| 330 | Note that pickles aren't completely secure. If you are concerned about |
| 331 | security, you may want to override this method to implement a more secure |
| 332 | mechanism. For example, you can sign pickles using HMAC and then verify |
| 333 | them on the receiving end, or alternatively you can disable unpickling of |
| 334 | global objects on the receiving end. |
| 335 | |
| 336 | |
| 337 | .. method:: send(packet) |
| 338 | |
| 339 | Send a pickled string *packet* to the socket. This function allows for |
| 340 | partial sends which can happen when the network is busy. |
| 341 | |
| 342 | |
| 343 | .. method:: createSocket() |
| 344 | |
| 345 | Tries to create a socket; on failure, uses an exponential back-off |
| 346 | algorithm. On intial failure, the handler will drop the message it was |
| 347 | trying to send. When subsequent messages are handled by the same |
| 348 | instance, it will not try connecting until some time has passed. The |
| 349 | default parameters are such that the initial delay is one second, and if |
| 350 | after that delay the connection still can't be made, the handler will |
| 351 | double the delay each time up to a maximum of 30 seconds. |
| 352 | |
| 353 | This behaviour is controlled by the following handler attributes: |
| 354 | |
| 355 | * ``retryStart`` (initial delay, defaulting to 1.0 seconds). |
| 356 | * ``retryFactor`` (multiplier, defaulting to 2.0). |
| 357 | * ``retryMax`` (maximum delay, defaulting to 30.0 seconds). |
| 358 | |
| 359 | This means that if the remote listener starts up *after* the handler has |
| 360 | been used, you could lose messages (since the handler won't even attempt |
| 361 | a connection until the delay has elapsed, but just silently drop messages |
| 362 | during the delay period). |
| 363 | |
| 364 | |
| 365 | .. _datagram-handler: |
| 366 | |
| 367 | DatagramHandler |
| 368 | ^^^^^^^^^^^^^^^ |
| 369 | |
| 370 | The :class:`DatagramHandler` class, located in the :mod:`logging.handlers` |
| 371 | module, inherits from :class:`SocketHandler` to support sending logging messages |
| 372 | over UDP sockets. |
| 373 | |
| 374 | |
| 375 | .. class:: DatagramHandler(host, port) |
| 376 | |
| 377 | Returns a new instance of the :class:`DatagramHandler` class intended to |
| 378 | communicate with a remote machine whose address is given by *host* and *port*. |
| 379 | |
| 380 | |
| 381 | .. method:: emit() |
| 382 | |
| 383 | Pickles the record's attribute dictionary and writes it to the socket in |
| 384 | binary format. If there is an error with the socket, silently drops the |
| 385 | packet. To unpickle the record at the receiving end into a |
| 386 | :class:`LogRecord`, use the :func:`makeLogRecord` function. |
| 387 | |
| 388 | |
| 389 | .. method:: makeSocket() |
| 390 | |
| 391 | The factory method of :class:`SocketHandler` is here overridden to create |
| 392 | a UDP socket (:const:`socket.SOCK_DGRAM`). |
| 393 | |
| 394 | |
| 395 | .. method:: send(s) |
| 396 | |
| 397 | Send a pickled string to a socket. |
| 398 | |
| 399 | |
| 400 | .. _syslog-handler: |
| 401 | |
| 402 | SysLogHandler |
| 403 | ^^^^^^^^^^^^^ |
| 404 | |
| 405 | The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module, |
| 406 | supports sending logging messages to a remote or local Unix syslog. |
| 407 | |
| 408 | |
| 409 | .. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM) |
| 410 | |
| 411 | Returns a new instance of the :class:`SysLogHandler` class intended to |
| 412 | communicate with a remote Unix machine whose address is given by *address* in |
| 413 | the form of a ``(host, port)`` tuple. If *address* is not specified, |
| 414 | ``('localhost', 514)`` is used. The address is used to open a socket. An |
| 415 | alternative to providing a ``(host, port)`` tuple is providing an address as a |
| 416 | string, for example '/dev/log'. In this case, a Unix domain socket is used to |
| 417 | send the message to the syslog. If *facility* is not specified, |
| 418 | :const:`LOG_USER` is used. The type of socket opened depends on the |
| 419 | *socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus |
| 420 | opens a UDP socket. To open a TCP socket (for use with the newer syslog |
| 421 | daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`. |
| 422 | |
| 423 | Note that if your server is not listening on UDP port 514, |
| 424 | :class:`SysLogHandler` may appear not to work. In that case, check what |
| 425 | address you should be using for a domain socket - it's system dependent. |
| 426 | For example, on Linux it's usually '/dev/log' but on OS/X it's |
| 427 | '/var/run/syslog'. You'll need to check your platform and use the |
| 428 | appropriate address (you may need to do this check at runtime if your |
| 429 | application needs to run on several platforms). On Windows, you pretty |
| 430 | much have to use the UDP option. |
| 431 | |
| 432 | .. versionchanged:: 2.7 |
| 433 | *socktype* was added. |
| 434 | |
| 435 | |
| 436 | .. method:: close() |
| 437 | |
| 438 | Closes the socket to the remote host. |
| 439 | |
| 440 | |
| 441 | .. method:: emit(record) |
| 442 | |
| 443 | The record is formatted, and then sent to the syslog server. If exception |
| 444 | information is present, it is *not* sent to the server. |
| 445 | |
| 446 | |
| 447 | .. method:: encodePriority(facility, priority) |
| 448 | |
| 449 | Encodes the facility and priority into an integer. You can pass in strings |
| 450 | or integers - if strings are passed, internal mapping dictionaries are |
| 451 | used to convert them to integers. |
| 452 | |
| 453 | The symbolic ``LOG_`` values are defined in :class:`SysLogHandler` and |
| 454 | mirror the values defined in the ``sys/syslog.h`` header file. |
| 455 | |
| 456 | **Priorities** |
| 457 | |
| 458 | +--------------------------+---------------+ |
| 459 | | Name (string) | Symbolic value| |
| 460 | +==========================+===============+ |
| 461 | | ``alert`` | LOG_ALERT | |
| 462 | +--------------------------+---------------+ |
| 463 | | ``crit`` or ``critical`` | LOG_CRIT | |
| 464 | +--------------------------+---------------+ |
| 465 | | ``debug`` | LOG_DEBUG | |
| 466 | +--------------------------+---------------+ |
| 467 | | ``emerg`` or ``panic`` | LOG_EMERG | |
| 468 | +--------------------------+---------------+ |
| 469 | | ``err`` or ``error`` | LOG_ERR | |
| 470 | +--------------------------+---------------+ |
| 471 | | ``info`` | LOG_INFO | |
| 472 | +--------------------------+---------------+ |
| 473 | | ``notice`` | LOG_NOTICE | |
| 474 | +--------------------------+---------------+ |
| 475 | | ``warn`` or ``warning`` | LOG_WARNING | |
| 476 | +--------------------------+---------------+ |
| 477 | |
| 478 | **Facilities** |
| 479 | |
| 480 | +---------------+---------------+ |
| 481 | | Name (string) | Symbolic value| |
| 482 | +===============+===============+ |
| 483 | | ``auth`` | LOG_AUTH | |
| 484 | +---------------+---------------+ |
| 485 | | ``authpriv`` | LOG_AUTHPRIV | |
| 486 | +---------------+---------------+ |
| 487 | | ``cron`` | LOG_CRON | |
| 488 | +---------------+---------------+ |
| 489 | | ``daemon`` | LOG_DAEMON | |
| 490 | +---------------+---------------+ |
| 491 | | ``ftp`` | LOG_FTP | |
| 492 | +---------------+---------------+ |
| 493 | | ``kern`` | LOG_KERN | |
| 494 | +---------------+---------------+ |
| 495 | | ``lpr`` | LOG_LPR | |
| 496 | +---------------+---------------+ |
| 497 | | ``mail`` | LOG_MAIL | |
| 498 | +---------------+---------------+ |
| 499 | | ``news`` | LOG_NEWS | |
| 500 | +---------------+---------------+ |
| 501 | | ``syslog`` | LOG_SYSLOG | |
| 502 | +---------------+---------------+ |
| 503 | | ``user`` | LOG_USER | |
| 504 | +---------------+---------------+ |
| 505 | | ``uucp`` | LOG_UUCP | |
| 506 | +---------------+---------------+ |
| 507 | | ``local0`` | LOG_LOCAL0 | |
| 508 | +---------------+---------------+ |
| 509 | | ``local1`` | LOG_LOCAL1 | |
| 510 | +---------------+---------------+ |
| 511 | | ``local2`` | LOG_LOCAL2 | |
| 512 | +---------------+---------------+ |
| 513 | | ``local3`` | LOG_LOCAL3 | |
| 514 | +---------------+---------------+ |
| 515 | | ``local4`` | LOG_LOCAL4 | |
| 516 | +---------------+---------------+ |
| 517 | | ``local5`` | LOG_LOCAL5 | |
| 518 | +---------------+---------------+ |
| 519 | | ``local6`` | LOG_LOCAL6 | |
| 520 | +---------------+---------------+ |
| 521 | | ``local7`` | LOG_LOCAL7 | |
| 522 | +---------------+---------------+ |
| 523 | |
| 524 | .. method:: mapPriority(levelname) |
| 525 | |
| 526 | Maps a logging level name to a syslog priority name. |
| 527 | You may need to override this if you are using custom levels, or |
| 528 | if the default algorithm is not suitable for your needs. The |
| 529 | default algorithm maps ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR`` and |
| 530 | ``CRITICAL`` to the equivalent syslog names, and all other level |
| 531 | names to 'warning'. |
| 532 | |
| 533 | .. _nt-eventlog-handler: |
| 534 | |
| 535 | NTEventLogHandler |
| 536 | ^^^^^^^^^^^^^^^^^ |
| 537 | |
| 538 | The :class:`NTEventLogHandler` class, located in the :mod:`logging.handlers` |
| 539 | module, supports sending logging messages to a local Windows NT, Windows 2000 or |
| 540 | Windows XP event log. Before you can use it, you need Mark Hammond's Win32 |
| 541 | extensions for Python installed. |
| 542 | |
| 543 | |
| 544 | .. class:: NTEventLogHandler(appname, dllname=None, logtype='Application') |
| 545 | |
| 546 | Returns a new instance of the :class:`NTEventLogHandler` class. The *appname* is |
| 547 | used to define the application name as it appears in the event log. An |
| 548 | appropriate registry entry is created using this name. The *dllname* should give |
| 549 | the fully qualified pathname of a .dll or .exe which contains message |
| 550 | definitions to hold in the log (if not specified, ``'win32service.pyd'`` is used |
| 551 | - this is installed with the Win32 extensions and contains some basic |
| 552 | placeholder message definitions. Note that use of these placeholders will make |
| 553 | your event logs big, as the entire message source is held in the log. If you |
| 554 | want slimmer logs, you have to pass in the name of your own .dll or .exe which |
| 555 | contains the message definitions you want to use in the event log). The |
| 556 | *logtype* is one of ``'Application'``, ``'System'`` or ``'Security'``, and |
| 557 | defaults to ``'Application'``. |
| 558 | |
| 559 | |
| 560 | .. method:: close() |
| 561 | |
| 562 | At this point, you can remove the application name from the registry as a |
| 563 | source of event log entries. However, if you do this, you will not be able |
| 564 | to see the events as you intended in the Event Log Viewer - it needs to be |
| 565 | able to access the registry to get the .dll name. The current version does |
| 566 | not do this. |
| 567 | |
| 568 | |
| 569 | .. method:: emit(record) |
| 570 | |
| 571 | Determines the message ID, event category and event type, and then logs |
| 572 | the message in the NT event log. |
| 573 | |
| 574 | |
| 575 | .. method:: getEventCategory(record) |
| 576 | |
| 577 | Returns the event category for the record. Override this if you want to |
| 578 | specify your own categories. This version returns 0. |
| 579 | |
| 580 | |
| 581 | .. method:: getEventType(record) |
| 582 | |
| 583 | Returns the event type for the record. Override this if you want to |
| 584 | specify your own types. This version does a mapping using the handler's |
| 585 | typemap attribute, which is set up in :meth:`__init__` to a dictionary |
| 586 | which contains mappings for :const:`DEBUG`, :const:`INFO`, |
| 587 | :const:`WARNING`, :const:`ERROR` and :const:`CRITICAL`. If you are using |
| 588 | your own levels, you will either need to override this method or place a |
| 589 | suitable dictionary in the handler's *typemap* attribute. |
| 590 | |
| 591 | |
| 592 | .. method:: getMessageID(record) |
| 593 | |
| 594 | Returns the message ID for the record. If you are using your own messages, |
| 595 | you could do this by having the *msg* passed to the logger being an ID |
| 596 | rather than a format string. Then, in here, you could use a dictionary |
| 597 | lookup to get the message ID. This version returns 1, which is the base |
| 598 | message ID in :file:`win32service.pyd`. |
| 599 | |
| 600 | .. _smtp-handler: |
| 601 | |
| 602 | SMTPHandler |
| 603 | ^^^^^^^^^^^ |
| 604 | |
| 605 | The :class:`SMTPHandler` class, located in the :mod:`logging.handlers` module, |
| 606 | supports sending logging messages to an email address via SMTP. |
| 607 | |
| 608 | |
| 609 | .. class:: SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None) |
| 610 | |
| 611 | Returns a new instance of the :class:`SMTPHandler` class. The instance is |
| 612 | initialized with the from and to addresses and subject line of the email. |
| 613 | The *toaddrs* should be a list of strings. To specify a non-standard SMTP |
| 614 | port, use the (host, port) tuple format for the *mailhost* argument. If you |
| 615 | use a string, the standard SMTP port is used. If your SMTP server requires |
| 616 | authentication, you can specify a (username, password) tuple for the |
Vinay Sajip | 5d09ba4 | 2011-08-01 11:28:02 +0100 | [diff] [blame] | 617 | *credentials* argument. |
| 618 | |
| 619 | To specify the use of a secure protocol (TLS), pass in a tuple to the |
| 620 | *secure* argument. This will only be used when authentication credentials are |
| 621 | supplied. The tuple should be either an empty tuple, or a single-value tuple |
| 622 | with the name of a keyfile, or a 2-value tuple with the names of the keyfile |
| 623 | and certificate file. (This tuple is passed to the |
| 624 | :meth:`smtplib.SMTP.starttls` method.) |
Vinay Sajip | 5dbca9c | 2011-04-08 11:40:38 +0100 | [diff] [blame] | 625 | |
| 626 | .. versionchanged:: 2.6 |
| 627 | *credentials* was added. |
| 628 | |
| 629 | .. versionchanged:: 2.7 |
| 630 | *secure* was added. |
| 631 | |
| 632 | |
| 633 | .. method:: emit(record) |
| 634 | |
| 635 | Formats the record and sends it to the specified addressees. |
| 636 | |
| 637 | |
| 638 | .. method:: getSubject(record) |
| 639 | |
| 640 | If you want to specify a subject line which is record-dependent, override |
| 641 | this method. |
| 642 | |
| 643 | .. _memory-handler: |
| 644 | |
| 645 | MemoryHandler |
| 646 | ^^^^^^^^^^^^^ |
| 647 | |
| 648 | The :class:`MemoryHandler` class, located in the :mod:`logging.handlers` module, |
| 649 | supports buffering of logging records in memory, periodically flushing them to a |
| 650 | :dfn:`target` handler. Flushing occurs whenever the buffer is full, or when an |
| 651 | event of a certain severity or greater is seen. |
| 652 | |
| 653 | :class:`MemoryHandler` is a subclass of the more general |
| 654 | :class:`BufferingHandler`, which is an abstract class. This buffers logging |
| 655 | records in memory. Whenever each record is added to the buffer, a check is made |
| 656 | by calling :meth:`shouldFlush` to see if the buffer should be flushed. If it |
Vinay Sajip | 49d5fba | 2012-03-26 17:06:44 +0100 | [diff] [blame] | 657 | should, then :meth:`flush` is expected to do the flushing. |
Vinay Sajip | 5dbca9c | 2011-04-08 11:40:38 +0100 | [diff] [blame] | 658 | |
| 659 | |
| 660 | .. class:: BufferingHandler(capacity) |
| 661 | |
| 662 | Initializes the handler with a buffer of the specified capacity. |
| 663 | |
| 664 | |
| 665 | .. method:: emit(record) |
| 666 | |
| 667 | Appends the record to the buffer. If :meth:`shouldFlush` returns true, |
| 668 | calls :meth:`flush` to process the buffer. |
| 669 | |
| 670 | |
| 671 | .. method:: flush() |
| 672 | |
| 673 | You can override this to implement custom flushing behavior. This version |
| 674 | just zaps the buffer to empty. |
| 675 | |
| 676 | |
| 677 | .. method:: shouldFlush(record) |
| 678 | |
| 679 | Returns true if the buffer is up to capacity. This method can be |
| 680 | overridden to implement custom flushing strategies. |
| 681 | |
| 682 | |
| 683 | .. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None) |
| 684 | |
| 685 | Returns a new instance of the :class:`MemoryHandler` class. The instance is |
| 686 | initialized with a buffer size of *capacity*. If *flushLevel* is not specified, |
| 687 | :const:`ERROR` is used. If no *target* is specified, the target will need to be |
| 688 | set using :meth:`setTarget` before this handler does anything useful. |
| 689 | |
| 690 | |
| 691 | .. method:: close() |
| 692 | |
| 693 | Calls :meth:`flush`, sets the target to :const:`None` and clears the |
| 694 | buffer. |
| 695 | |
| 696 | |
| 697 | .. method:: flush() |
| 698 | |
| 699 | For a :class:`MemoryHandler`, flushing means just sending the buffered |
| 700 | records to the target, if there is one. The buffer is also cleared when |
| 701 | this happens. Override if you want different behavior. |
| 702 | |
| 703 | |
| 704 | .. method:: setTarget(target) |
Vinay Sajip | 5dbca9c | 2011-04-08 11:40:38 +0100 | [diff] [blame] | 705 | |
| 706 | Sets the target handler for this handler. |
| 707 | |
| 708 | |
| 709 | .. method:: shouldFlush(record) |
| 710 | |
| 711 | Checks for buffer full or a record at the *flushLevel* or higher. |
| 712 | |
| 713 | |
| 714 | .. _http-handler: |
| 715 | |
| 716 | HTTPHandler |
| 717 | ^^^^^^^^^^^ |
| 718 | |
| 719 | The :class:`HTTPHandler` class, located in the :mod:`logging.handlers` module, |
| 720 | supports sending logging messages to a Web server, using either ``GET`` or |
| 721 | ``POST`` semantics. |
| 722 | |
| 723 | |
| 724 | .. class:: HTTPHandler(host, url, method='GET') |
| 725 | |
| 726 | Returns a new instance of the :class:`HTTPHandler` class. The *host* can be |
| 727 | of the form ``host:port``, should you need to use a specific port number. |
| 728 | If no *method* is specified, ``GET`` is used. |
| 729 | |
| 730 | |
| 731 | .. method:: emit(record) |
| 732 | |
| 733 | Sends the record to the Web server as a percent-encoded dictionary. |
| 734 | |
| 735 | |
| 736 | .. seealso:: |
| 737 | |
| 738 | Module :mod:`logging` |
| 739 | API reference for the logging module. |
| 740 | |
| 741 | Module :mod:`logging.config` |
| 742 | Configuration API for the logging module. |
| 743 | |
| 744 | |