Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1 | :mod:`logging.handlers` --- Logging handlers |
| 2 | ============================================ |
| 3 | |
| 4 | .. module:: logging.handlers |
| 5 | :synopsis: Handlers for the logging module. |
| 6 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Vinay Sajip <vinay_sajip@red-dove.com> |
| 8 | .. sectionauthor:: Vinay Sajip <vinay_sajip@red-dove.com> |
| 9 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 10 | **Source code:** :source:`Lib/logging/handlers.py` |
| 11 | |
Vinay Sajip | 01094e1 | 2010-12-19 13:41:26 +0000 | [diff] [blame] | 12 | .. sidebar:: Important |
| 13 | |
| 14 | This page contains only reference information. For tutorials, |
| 15 | please see |
| 16 | |
| 17 | * :ref:`Basic Tutorial <logging-basic-tutorial>` |
| 18 | * :ref:`Advanced Tutorial <logging-advanced-tutorial>` |
| 19 | * :ref:`Logging Cookbook <logging-cookbook>` |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 20 | |
Vinay Sajip | 31b862d | 2013-09-05 23:01:07 +0100 | [diff] [blame] | 21 | -------------- |
| 22 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 23 | .. currentmodule:: logging |
| 24 | |
Vinay Sajip | 01094e1 | 2010-12-19 13:41:26 +0000 | [diff] [blame] | 25 | The following useful handlers are provided in the package. Note that three of |
| 26 | the handlers (:class:`StreamHandler`, :class:`FileHandler` and |
| 27 | :class:`NullHandler`) are actually defined in the :mod:`logging` module itself, |
| 28 | but have been documented here along with the other handlers. |
| 29 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 30 | .. _stream-handler: |
| 31 | |
| 32 | StreamHandler |
| 33 | ^^^^^^^^^^^^^ |
| 34 | |
| 35 | The :class:`StreamHandler` class, located in the core :mod:`logging` package, |
| 36 | sends logging output to streams such as *sys.stdout*, *sys.stderr* or any |
| 37 | file-like object (or, more precisely, any object which supports :meth:`write` |
| 38 | and :meth:`flush` methods). |
| 39 | |
| 40 | |
| 41 | .. class:: StreamHandler(stream=None) |
| 42 | |
| 43 | Returns a new instance of the :class:`StreamHandler` class. If *stream* is |
| 44 | specified, the instance will use it for logging output; otherwise, *sys.stderr* |
| 45 | will be used. |
| 46 | |
| 47 | |
| 48 | .. method:: emit(record) |
| 49 | |
| 50 | If a formatter is specified, it is used to format the record. The record |
Vinay Sajip | 689b68a | 2010-12-22 15:04:15 +0000 | [diff] [blame] | 51 | is then written to the stream with a terminator. If exception information |
| 52 | is present, it is formatted using :func:`traceback.print_exception` and |
| 53 | appended to the stream. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | .. method:: flush() |
| 57 | |
| 58 | Flushes the stream by calling its :meth:`flush` method. Note that the |
Vinay Sajip | 67f3977 | 2013-08-17 00:39:42 +0100 | [diff] [blame] | 59 | :meth:`close` method is inherited from :class:`~logging.Handler` and so |
| 60 | does no output, so an explicit :meth:`flush` call may be needed at times. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 61 | |
Vinay Sajip | 2543f50 | 2017-07-30 10:41:45 +0100 | [diff] [blame] | 62 | .. method:: setStream(stream) |
| 63 | |
| 64 | Sets the instance's stream to the specified value, if it is different. |
| 65 | The old stream is flushed before the new stream is set. |
| 66 | |
| 67 | :param stream: The stream that the handler should use. |
| 68 | |
| 69 | :return: the old stream, if the stream was changed, or *None* if it wasn't. |
| 70 | |
Andre Delfino | 18a2fc6 | 2019-06-29 18:57:39 -0300 | [diff] [blame^] | 71 | .. versionadded:: 3.7 |
Vinay Sajip | 2543f50 | 2017-07-30 10:41:45 +0100 | [diff] [blame] | 72 | |
| 73 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 74 | .. versionchanged:: 3.2 |
| 75 | The ``StreamHandler`` class now has a ``terminator`` attribute, default |
| 76 | value ``'\n'``, which is used as the terminator when writing a formatted |
| 77 | record to a stream. If you don't want this newline termination, you can |
| 78 | set the handler instance's ``terminator`` attribute to the empty string. |
Vinay Sajip | 689b68a | 2010-12-22 15:04:15 +0000 | [diff] [blame] | 79 | In earlier versions, the terminator was hardcoded as ``'\n'``. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 80 | |
Vinay Sajip | 2543f50 | 2017-07-30 10:41:45 +0100 | [diff] [blame] | 81 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 82 | .. _file-handler: |
| 83 | |
| 84 | FileHandler |
| 85 | ^^^^^^^^^^^ |
| 86 | |
| 87 | The :class:`FileHandler` class, located in the core :mod:`logging` package, |
| 88 | sends logging output to a disk file. It inherits the output functionality from |
| 89 | :class:`StreamHandler`. |
| 90 | |
| 91 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 92 | .. class:: FileHandler(filename, mode='a', encoding=None, delay=False, errors=None) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 93 | |
| 94 | Returns a new instance of the :class:`FileHandler` class. The specified file is |
| 95 | opened and used as the stream for logging. If *mode* is not specified, |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 96 | :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 97 | with that encoding. If *delay* is true, then file opening is deferred until the |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 98 | first call to :meth:`emit`. By default, the file grows indefinitely. If |
| 99 | *errors* is specified, it's used to determine how encoding errors are handled. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 100 | |
Vinay Sajip | 638e622 | 2016-07-22 18:23:04 +0100 | [diff] [blame] | 101 | .. versionchanged:: 3.6 |
| 102 | As well as string values, :class:`~pathlib.Path` objects are also accepted |
| 103 | for the *filename* argument. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 104 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 105 | .. versionchanged:: 3.9 |
| 106 | The *errors* parameter was added. |
| 107 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 108 | .. method:: close() |
| 109 | |
| 110 | Closes the file. |
| 111 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 112 | .. method:: emit(record) |
| 113 | |
| 114 | Outputs the record to the file. |
| 115 | |
| 116 | |
| 117 | .. _null-handler: |
| 118 | |
| 119 | NullHandler |
| 120 | ^^^^^^^^^^^ |
| 121 | |
| 122 | .. versionadded:: 3.1 |
| 123 | |
| 124 | The :class:`NullHandler` class, located in the core :mod:`logging` package, |
| 125 | does not do any formatting or output. It is essentially a 'no-op' handler |
| 126 | for use by library developers. |
| 127 | |
| 128 | .. class:: NullHandler() |
| 129 | |
| 130 | Returns a new instance of the :class:`NullHandler` class. |
| 131 | |
| 132 | .. method:: emit(record) |
| 133 | |
| 134 | This method does nothing. |
| 135 | |
| 136 | .. method:: handle(record) |
| 137 | |
| 138 | This method does nothing. |
| 139 | |
| 140 | .. method:: createLock() |
| 141 | |
| 142 | This method returns ``None`` for the lock, since there is no |
| 143 | underlying I/O to which access needs to be serialized. |
| 144 | |
| 145 | |
| 146 | See :ref:`library-config` for more information on how to use |
| 147 | :class:`NullHandler`. |
| 148 | |
| 149 | .. _watched-file-handler: |
| 150 | |
| 151 | WatchedFileHandler |
| 152 | ^^^^^^^^^^^^^^^^^^ |
| 153 | |
| 154 | .. currentmodule:: logging.handlers |
| 155 | |
| 156 | The :class:`WatchedFileHandler` class, located in the :mod:`logging.handlers` |
| 157 | module, is a :class:`FileHandler` which watches the file it is logging to. If |
| 158 | the file changes, it is closed and reopened using the file name. |
| 159 | |
| 160 | A file change can happen because of usage of programs such as *newsyslog* and |
| 161 | *logrotate* which perform log file rotation. This handler, intended for use |
| 162 | under Unix/Linux, watches the file to see if it has changed since the last emit. |
| 163 | (A file is deemed to have changed if its device or inode have changed.) If the |
| 164 | file has changed, the old file stream is closed, and the file opened to get a |
| 165 | new stream. |
| 166 | |
| 167 | This handler is not appropriate for use under Windows, because under Windows |
| 168 | open log files cannot be moved or renamed - logging opens the files with |
| 169 | exclusive locks - and so there is no need for such a handler. Furthermore, |
Vinay Sajip | 67f3977 | 2013-08-17 00:39:42 +0100 | [diff] [blame] | 170 | *ST_INO* is not supported under Windows; :func:`~os.stat` always returns zero |
| 171 | for this value. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 172 | |
| 173 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 174 | .. class:: WatchedFileHandler(filename, mode='a', encoding=None, delay=False, errors=None) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 175 | |
| 176 | Returns a new instance of the :class:`WatchedFileHandler` class. The specified |
| 177 | file is opened and used as the stream for logging. If *mode* is not specified, |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 178 | :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 179 | with that encoding. If *delay* is true, then file opening is deferred until the |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 180 | first call to :meth:`emit`. By default, the file grows indefinitely. If |
| 181 | *errors* is provided, it determines how encoding errors are handled. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 182 | |
Vinay Sajip | 638e622 | 2016-07-22 18:23:04 +0100 | [diff] [blame] | 183 | .. versionchanged:: 3.6 |
| 184 | As well as string values, :class:`~pathlib.Path` objects are also accepted |
| 185 | for the *filename* argument. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 186 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 187 | .. versionchanged:: 3.9 |
| 188 | The *errors* parameter was added. |
| 189 | |
Vinay Sajip | 29a1445 | 2015-10-01 20:54:41 +0100 | [diff] [blame] | 190 | .. method:: reopenIfNeeded() |
| 191 | |
| 192 | Checks to see if the file has changed. If it has, the existing stream is |
| 193 | flushed and closed and the file opened again, typically as a precursor to |
| 194 | outputting the record to the file. |
| 195 | |
Berker Peksag | 6f038ad | 2015-10-07 07:54:23 +0300 | [diff] [blame] | 196 | .. versionadded:: 3.6 |
| 197 | |
Vinay Sajip | 29a1445 | 2015-10-01 20:54:41 +0100 | [diff] [blame] | 198 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 199 | .. method:: emit(record) |
| 200 | |
Vinay Sajip | 29a1445 | 2015-10-01 20:54:41 +0100 | [diff] [blame] | 201 | Outputs the record to the file, but first calls :meth:`reopenIfNeeded` to |
| 202 | reopen the file if it has changed. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 203 | |
Vinay Sajip | 23b94d0 | 2012-01-04 12:02:26 +0000 | [diff] [blame] | 204 | .. _base-rotating-handler: |
| 205 | |
| 206 | BaseRotatingHandler |
| 207 | ^^^^^^^^^^^^^^^^^^^ |
| 208 | |
| 209 | The :class:`BaseRotatingHandler` class, located in the :mod:`logging.handlers` |
| 210 | module, is the base class for the rotating file handlers, |
| 211 | :class:`RotatingFileHandler` and :class:`TimedRotatingFileHandler`. You should |
| 212 | not need to instantiate this class, but it has attributes and methods you may |
| 213 | need to override. |
| 214 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 215 | .. class:: BaseRotatingHandler(filename, mode, encoding=None, delay=False, errors=None) |
Vinay Sajip | 23b94d0 | 2012-01-04 12:02:26 +0000 | [diff] [blame] | 216 | |
| 217 | The parameters are as for :class:`FileHandler`. The attributes are: |
| 218 | |
| 219 | .. attribute:: namer |
| 220 | |
| 221 | If this attribute is set to a callable, the :meth:`rotation_filename` |
| 222 | method delegates to this callable. The parameters passed to the callable |
| 223 | are those passed to :meth:`rotation_filename`. |
| 224 | |
| 225 | .. note:: The namer function is called quite a few times during rollover, |
| 226 | so it should be as simple and as fast as possible. It should also |
| 227 | return the same output every time for a given input, otherwise the |
| 228 | rollover behaviour may not work as expected. |
| 229 | |
| 230 | .. versionadded:: 3.3 |
| 231 | |
| 232 | |
| 233 | .. attribute:: BaseRotatingHandler.rotator |
| 234 | |
| 235 | If this attribute is set to a callable, the :meth:`rotate` method |
| 236 | delegates to this callable. The parameters passed to the callable are |
| 237 | those passed to :meth:`rotate`. |
| 238 | |
| 239 | .. versionadded:: 3.3 |
| 240 | |
| 241 | .. method:: BaseRotatingHandler.rotation_filename(default_name) |
| 242 | |
| 243 | Modify the filename of a log file when rotating. |
| 244 | |
| 245 | This is provided so that a custom filename can be provided. |
| 246 | |
| 247 | The default implementation calls the 'namer' attribute of the handler, |
| 248 | if it's callable, passing the default name to it. If the attribute isn't |
Ezio Melotti | 226231c | 2012-01-18 05:40:00 +0200 | [diff] [blame] | 249 | callable (the default is ``None``), the name is returned unchanged. |
Vinay Sajip | 23b94d0 | 2012-01-04 12:02:26 +0000 | [diff] [blame] | 250 | |
| 251 | :param default_name: The default name for the log file. |
| 252 | |
| 253 | .. versionadded:: 3.3 |
| 254 | |
| 255 | |
| 256 | .. method:: BaseRotatingHandler.rotate(source, dest) |
| 257 | |
| 258 | When rotating, rotate the current log. |
| 259 | |
| 260 | The default implementation calls the 'rotator' attribute of the handler, |
| 261 | if it's callable, passing the source and dest arguments to it. If the |
Ezio Melotti | 226231c | 2012-01-18 05:40:00 +0200 | [diff] [blame] | 262 | attribute isn't callable (the default is ``None``), the source is simply |
Vinay Sajip | 23b94d0 | 2012-01-04 12:02:26 +0000 | [diff] [blame] | 263 | renamed to the destination. |
| 264 | |
| 265 | :param source: The source filename. This is normally the base |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 266 | filename, e.g. 'test.log'. |
Vinay Sajip | 23b94d0 | 2012-01-04 12:02:26 +0000 | [diff] [blame] | 267 | :param dest: The destination filename. This is normally |
| 268 | what the source is rotated to, e.g. 'test.log.1'. |
| 269 | |
| 270 | .. versionadded:: 3.3 |
| 271 | |
| 272 | The reason the attributes exist is to save you having to subclass - you can use |
| 273 | the same callables for instances of :class:`RotatingFileHandler` and |
| 274 | :class:`TimedRotatingFileHandler`. If either the namer or rotator callable |
| 275 | raises an exception, this will be handled in the same way as any other |
| 276 | exception during an :meth:`emit` call, i.e. via the :meth:`handleError` method |
| 277 | of the handler. |
| 278 | |
| 279 | If you need to make more significant changes to rotation processing, you can |
| 280 | override the methods. |
| 281 | |
| 282 | For an example, see :ref:`cookbook-rotator-namer`. |
| 283 | |
| 284 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 285 | .. _rotating-file-handler: |
| 286 | |
| 287 | RotatingFileHandler |
| 288 | ^^^^^^^^^^^^^^^^^^^ |
| 289 | |
| 290 | The :class:`RotatingFileHandler` class, located in the :mod:`logging.handlers` |
| 291 | module, supports rotation of disk log files. |
| 292 | |
| 293 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 294 | .. class:: RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False, errors=None) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 295 | |
| 296 | Returns a new instance of the :class:`RotatingFileHandler` class. The specified |
| 297 | file is opened and used as the stream for logging. If *mode* is not specified, |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 298 | ``'a'`` is used. If *encoding* is not ``None``, it is used to open the file |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 299 | with that encoding. If *delay* is true, then file opening is deferred until the |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 300 | first call to :meth:`emit`. By default, the file grows indefinitely. If |
| 301 | *errors* is provided, it determines how encoding errors are handled. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 302 | |
| 303 | You can use the *maxBytes* and *backupCount* values to allow the file to |
| 304 | :dfn:`rollover` at a predetermined size. When the size is about to be exceeded, |
| 305 | the file is closed and a new file is silently opened for output. Rollover occurs |
Vinay Sajip | 53a21eb | 2016-12-31 11:06:57 +0000 | [diff] [blame] | 306 | whenever the current log file is nearly *maxBytes* in length; but if either of |
| 307 | *maxBytes* or *backupCount* is zero, rollover never occurs, so you generally want |
| 308 | to set *backupCount* to at least 1, and have a non-zero *maxBytes*. |
| 309 | When *backupCount* is non-zero, the system will save old log files by appending |
| 310 | the extensions '.1', '.2' etc., to the filename. For example, with a *backupCount* |
| 311 | of 5 and a base file name of :file:`app.log`, you would get :file:`app.log`, |
Vinay Sajip | ff37cfe | 2015-01-23 21:19:04 +0000 | [diff] [blame] | 312 | :file:`app.log.1`, :file:`app.log.2`, up to :file:`app.log.5`. The file being |
| 313 | written to is always :file:`app.log`. When this file is filled, it is closed |
| 314 | and renamed to :file:`app.log.1`, and if files :file:`app.log.1`, |
Vinay Sajip | 53a21eb | 2016-12-31 11:06:57 +0000 | [diff] [blame] | 315 | :file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`, |
| 316 | :file:`app.log.3` etc. respectively. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 317 | |
Vinay Sajip | 638e622 | 2016-07-22 18:23:04 +0100 | [diff] [blame] | 318 | .. versionchanged:: 3.6 |
| 319 | As well as string values, :class:`~pathlib.Path` objects are also accepted |
| 320 | for the *filename* argument. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 321 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 322 | .. versionchanged:: 3.9 |
| 323 | The *errors* parameter was added. |
| 324 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 325 | .. method:: doRollover() |
| 326 | |
| 327 | Does a rollover, as described above. |
| 328 | |
| 329 | |
| 330 | .. method:: emit(record) |
| 331 | |
| 332 | Outputs the record to the file, catering for rollover as described |
| 333 | previously. |
| 334 | |
| 335 | .. _timed-rotating-file-handler: |
| 336 | |
| 337 | TimedRotatingFileHandler |
| 338 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 339 | |
| 340 | The :class:`TimedRotatingFileHandler` class, located in the |
| 341 | :mod:`logging.handlers` module, supports rotation of disk log files at certain |
| 342 | timed intervals. |
| 343 | |
| 344 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 345 | .. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None, errors=None) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 346 | |
| 347 | Returns a new instance of the :class:`TimedRotatingFileHandler` class. The |
| 348 | specified file is opened and used as the stream for logging. On rotating it also |
| 349 | sets the filename suffix. Rotating happens based on the product of *when* and |
| 350 | *interval*. |
| 351 | |
| 352 | You can use the *when* to specify the type of *interval*. The list of possible |
| 353 | values is below. Note that they are not case sensitive. |
| 354 | |
Vinay Sajip | dd30830 | 2016-08-24 17:49:15 +0100 | [diff] [blame] | 355 | +----------------+----------------------------+-------------------------+ |
| 356 | | Value | Type of interval | If/how *atTime* is used | |
| 357 | +================+============================+=========================+ |
| 358 | | ``'S'`` | Seconds | Ignored | |
| 359 | +----------------+----------------------------+-------------------------+ |
| 360 | | ``'M'`` | Minutes | Ignored | |
| 361 | +----------------+----------------------------+-------------------------+ |
| 362 | | ``'H'`` | Hours | Ignored | |
| 363 | +----------------+----------------------------+-------------------------+ |
| 364 | | ``'D'`` | Days | Ignored | |
| 365 | +----------------+----------------------------+-------------------------+ |
| 366 | | ``'W0'-'W6'`` | Weekday (0=Monday) | Used to compute initial | |
| 367 | | | | rollover time | |
| 368 | +----------------+----------------------------+-------------------------+ |
| 369 | | ``'midnight'`` | Roll over at midnight, if | Used to compute initial | |
| 370 | | | *atTime* not specified, | rollover time | |
| 371 | | | else at time *atTime* | | |
| 372 | +----------------+----------------------------+-------------------------+ |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 373 | |
Vinay Sajip | 832d99b | 2013-03-08 23:24:30 +0000 | [diff] [blame] | 374 | When using weekday-based rotation, specify 'W0' for Monday, 'W1' for |
| 375 | Tuesday, and so on up to 'W6' for Sunday. In this case, the value passed for |
| 376 | *interval* isn't used. |
| 377 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 378 | The system will save old log files by appending extensions to the filename. |
| 379 | The extensions are date-and-time based, using the strftime format |
| 380 | ``%Y-%m-%d_%H-%M-%S`` or a leading portion thereof, depending on the |
| 381 | rollover interval. |
| 382 | |
| 383 | When computing the next rollover time for the first time (when the handler |
| 384 | is created), the last modification time of an existing log file, or else |
| 385 | the current time, is used to compute when the next rotation will occur. |
| 386 | |
| 387 | If the *utc* argument is true, times in UTC will be used; otherwise |
| 388 | local time is used. |
| 389 | |
| 390 | If *backupCount* is nonzero, at most *backupCount* files |
| 391 | will be kept, and if more would be created when rollover occurs, the oldest |
| 392 | one is deleted. The deletion logic uses the interval to determine which |
| 393 | files to delete, so changing the interval may leave old files lying around. |
| 394 | |
| 395 | If *delay* is true, then file opening is deferred until the first call to |
| 396 | :meth:`emit`. |
| 397 | |
Vinay Sajip | a713079 | 2013-04-12 17:04:23 +0100 | [diff] [blame] | 398 | If *atTime* is not ``None``, it must be a ``datetime.time`` instance which |
| 399 | specifies the time of day when rollover occurs, for the cases where rollover |
Vinay Sajip | dd30830 | 2016-08-24 17:49:15 +0100 | [diff] [blame] | 400 | is set to happen "at midnight" or "on a particular weekday". Note that in |
| 401 | these cases, the *atTime* value is effectively used to compute the *initial* |
| 402 | rollover, and subsequent rollovers would be calculated via the normal |
| 403 | interval calculation. |
| 404 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 405 | If *errors* is specified, it's used to determine how encoding errors are |
| 406 | handled. |
| 407 | |
Vinay Sajip | dd30830 | 2016-08-24 17:49:15 +0100 | [diff] [blame] | 408 | .. note:: Calculation of the initial rollover time is done when the handler |
| 409 | is initialised. Calculation of subsequent rollover times is done only |
| 410 | when rollover occurs, and rollover occurs only when emitting output. If |
| 411 | this is not kept in mind, it might lead to some confusion. For example, |
| 412 | if an interval of "every minute" is set, that does not mean you will |
| 413 | always see log files with times (in the filename) separated by a minute; |
| 414 | if, during application execution, logging output is generated more |
| 415 | frequently than once a minute, *then* you can expect to see log files |
| 416 | with times separated by a minute. If, on the other hand, logging messages |
| 417 | are only output once every five minutes (say), then there will be gaps in |
| 418 | the file times corresponding to the minutes where no output (and hence no |
| 419 | rollover) occurred. |
Vinay Sajip | a713079 | 2013-04-12 17:04:23 +0100 | [diff] [blame] | 420 | |
| 421 | .. versionchanged:: 3.4 |
| 422 | *atTime* parameter was added. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 423 | |
Vinay Sajip | 638e622 | 2016-07-22 18:23:04 +0100 | [diff] [blame] | 424 | .. versionchanged:: 3.6 |
| 425 | As well as string values, :class:`~pathlib.Path` objects are also accepted |
| 426 | for the *filename* argument. |
| 427 | |
Vinay Sajip | ca7b504 | 2019-06-17 17:40:52 +0100 | [diff] [blame] | 428 | .. versionchanged:: 3.9 |
| 429 | The *errors* parameter was added. |
| 430 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 431 | .. method:: doRollover() |
| 432 | |
| 433 | Does a rollover, as described above. |
| 434 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 435 | .. method:: emit(record) |
| 436 | |
| 437 | Outputs the record to the file, catering for rollover as described above. |
| 438 | |
| 439 | |
| 440 | .. _socket-handler: |
| 441 | |
| 442 | SocketHandler |
| 443 | ^^^^^^^^^^^^^ |
| 444 | |
| 445 | The :class:`SocketHandler` class, located in the :mod:`logging.handlers` module, |
| 446 | sends logging output to a network socket. The base class uses a TCP socket. |
| 447 | |
| 448 | |
| 449 | .. class:: SocketHandler(host, port) |
| 450 | |
| 451 | Returns a new instance of the :class:`SocketHandler` class intended to |
| 452 | communicate with a remote machine whose address is given by *host* and *port*. |
| 453 | |
Vinay Sajip | 5421f35 | 2013-09-27 18:18:28 +0100 | [diff] [blame] | 454 | .. versionchanged:: 3.4 |
| 455 | If ``port`` is specified as ``None``, a Unix domain socket is created |
| 456 | using the value in ``host`` - otherwise, a TCP socket is created. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 457 | |
| 458 | .. method:: close() |
| 459 | |
| 460 | Closes the socket. |
| 461 | |
| 462 | |
| 463 | .. method:: emit() |
| 464 | |
| 465 | Pickles the record's attribute dictionary and writes it to the socket in |
| 466 | binary format. If there is an error with the socket, silently drops the |
| 467 | packet. If the connection was previously lost, re-establishes the |
| 468 | connection. To unpickle the record at the receiving end into a |
Vinay Sajip | 67f3977 | 2013-08-17 00:39:42 +0100 | [diff] [blame] | 469 | :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord` |
| 470 | function. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 471 | |
| 472 | |
| 473 | .. method:: handleError() |
| 474 | |
| 475 | Handles an error which has occurred during :meth:`emit`. The most likely |
| 476 | cause is a lost connection. Closes the socket so that we can retry on the |
| 477 | next event. |
| 478 | |
| 479 | |
| 480 | .. method:: makeSocket() |
| 481 | |
| 482 | This is a factory method which allows subclasses to define the precise |
| 483 | type of socket they want. The default implementation creates a TCP socket |
| 484 | (:const:`socket.SOCK_STREAM`). |
| 485 | |
| 486 | |
| 487 | .. method:: makePickle(record) |
| 488 | |
| 489 | Pickles the record's attribute dictionary in binary format with a length |
Vinay Sajip | f06b569 | 2019-06-19 15:29:57 +0100 | [diff] [blame] | 490 | prefix, and returns it ready for transmission across the socket. The |
| 491 | details of this operation are equivalent to:: |
| 492 | |
| 493 | data = pickle.dumps(record_attr_dict, 1) |
| 494 | datalen = struct.pack('>L', len(data)) |
| 495 | return datalen + data |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 496 | |
| 497 | Note that pickles aren't completely secure. If you are concerned about |
| 498 | security, you may want to override this method to implement a more secure |
| 499 | mechanism. For example, you can sign pickles using HMAC and then verify |
| 500 | them on the receiving end, or alternatively you can disable unpickling of |
| 501 | global objects on the receiving end. |
| 502 | |
Georg Brandl | 08e278a | 2011-02-15 12:44:43 +0000 | [diff] [blame] | 503 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 504 | .. method:: send(packet) |
| 505 | |
Vinay Sajip | f06b569 | 2019-06-19 15:29:57 +0100 | [diff] [blame] | 506 | Send a pickled byte-string *packet* to the socket. The format of the sent |
| 507 | byte-string is as described in the documentation for |
| 508 | :meth:`~SocketHandler.makePickle`. |
| 509 | |
| 510 | This function allows for partial sends, which can happen when the network |
| 511 | is busy. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 512 | |
Georg Brandl | 08e278a | 2011-02-15 12:44:43 +0000 | [diff] [blame] | 513 | |
Georg Brandl | dbb9585 | 2011-02-15 12:41:17 +0000 | [diff] [blame] | 514 | .. method:: createSocket() |
| 515 | |
| 516 | Tries to create a socket; on failure, uses an exponential back-off |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 517 | algorithm. On initial failure, the handler will drop the message it was |
Georg Brandl | dbb9585 | 2011-02-15 12:41:17 +0000 | [diff] [blame] | 518 | trying to send. When subsequent messages are handled by the same |
| 519 | instance, it will not try connecting until some time has passed. The |
| 520 | default parameters are such that the initial delay is one second, and if |
| 521 | after that delay the connection still can't be made, the handler will |
| 522 | double the delay each time up to a maximum of 30 seconds. |
| 523 | |
| 524 | This behaviour is controlled by the following handler attributes: |
| 525 | |
| 526 | * ``retryStart`` (initial delay, defaulting to 1.0 seconds). |
| 527 | * ``retryFactor`` (multiplier, defaulting to 2.0). |
| 528 | * ``retryMax`` (maximum delay, defaulting to 30.0 seconds). |
| 529 | |
| 530 | This means that if the remote listener starts up *after* the handler has |
| 531 | been used, you could lose messages (since the handler won't even attempt |
| 532 | a connection until the delay has elapsed, but just silently drop messages |
| 533 | during the delay period). |
Georg Brandl | 08e278a | 2011-02-15 12:44:43 +0000 | [diff] [blame] | 534 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 535 | |
| 536 | .. _datagram-handler: |
| 537 | |
| 538 | DatagramHandler |
| 539 | ^^^^^^^^^^^^^^^ |
| 540 | |
| 541 | The :class:`DatagramHandler` class, located in the :mod:`logging.handlers` |
| 542 | module, inherits from :class:`SocketHandler` to support sending logging messages |
| 543 | over UDP sockets. |
| 544 | |
| 545 | |
| 546 | .. class:: DatagramHandler(host, port) |
| 547 | |
| 548 | Returns a new instance of the :class:`DatagramHandler` class intended to |
| 549 | communicate with a remote machine whose address is given by *host* and *port*. |
| 550 | |
Vinay Sajip | 5421f35 | 2013-09-27 18:18:28 +0100 | [diff] [blame] | 551 | .. versionchanged:: 3.4 |
| 552 | If ``port`` is specified as ``None``, a Unix domain socket is created |
Mike DePalatis | 233de02 | 2018-03-30 03:36:06 -0400 | [diff] [blame] | 553 | using the value in ``host`` - otherwise, a UDP socket is created. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 554 | |
| 555 | .. method:: emit() |
| 556 | |
| 557 | Pickles the record's attribute dictionary and writes it to the socket in |
| 558 | binary format. If there is an error with the socket, silently drops the |
| 559 | packet. To unpickle the record at the receiving end into a |
Vinay Sajip | 67f3977 | 2013-08-17 00:39:42 +0100 | [diff] [blame] | 560 | :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord` |
| 561 | function. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 562 | |
| 563 | |
| 564 | .. method:: makeSocket() |
| 565 | |
| 566 | The factory method of :class:`SocketHandler` is here overridden to create |
| 567 | a UDP socket (:const:`socket.SOCK_DGRAM`). |
| 568 | |
| 569 | |
| 570 | .. method:: send(s) |
| 571 | |
Vinay Sajip | f06b569 | 2019-06-19 15:29:57 +0100 | [diff] [blame] | 572 | Send a pickled byte-string to a socket. The format of the sent byte-string |
| 573 | is as described in the documentation for :meth:`SocketHandler.makePickle`. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 574 | |
| 575 | |
| 576 | .. _syslog-handler: |
| 577 | |
| 578 | SysLogHandler |
| 579 | ^^^^^^^^^^^^^ |
| 580 | |
| 581 | The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module, |
| 582 | supports sending logging messages to a remote or local Unix syslog. |
| 583 | |
| 584 | |
| 585 | .. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM) |
| 586 | |
| 587 | Returns a new instance of the :class:`SysLogHandler` class intended to |
| 588 | communicate with a remote Unix machine whose address is given by *address* in |
| 589 | the form of a ``(host, port)`` tuple. If *address* is not specified, |
| 590 | ``('localhost', 514)`` is used. The address is used to open a socket. An |
| 591 | alternative to providing a ``(host, port)`` tuple is providing an address as a |
| 592 | string, for example '/dev/log'. In this case, a Unix domain socket is used to |
| 593 | send the message to the syslog. If *facility* is not specified, |
| 594 | :const:`LOG_USER` is used. The type of socket opened depends on the |
| 595 | *socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus |
| 596 | opens a UDP socket. To open a TCP socket (for use with the newer syslog |
| 597 | daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`. |
| 598 | |
| 599 | Note that if your server is not listening on UDP port 514, |
| 600 | :class:`SysLogHandler` may appear not to work. In that case, check what |
| 601 | address you should be using for a domain socket - it's system dependent. |
| 602 | For example, on Linux it's usually '/dev/log' but on OS/X it's |
| 603 | '/var/run/syslog'. You'll need to check your platform and use the |
| 604 | appropriate address (you may need to do this check at runtime if your |
| 605 | application needs to run on several platforms). On Windows, you pretty |
| 606 | much have to use the UDP option. |
| 607 | |
| 608 | .. versionchanged:: 3.2 |
| 609 | *socktype* was added. |
| 610 | |
| 611 | |
| 612 | .. method:: close() |
| 613 | |
| 614 | Closes the socket to the remote host. |
| 615 | |
| 616 | |
| 617 | .. method:: emit(record) |
| 618 | |
| 619 | The record is formatted, and then sent to the syslog server. If exception |
| 620 | information is present, it is *not* sent to the server. |
| 621 | |
Vinay Sajip | 645e458 | 2011-06-10 18:52:50 +0100 | [diff] [blame] | 622 | .. versionchanged:: 3.2.1 |
| 623 | (See: :issue:`12168`.) In earlier versions, the message sent to the |
| 624 | syslog daemons was always terminated with a NUL byte, because early |
| 625 | versions of these daemons expected a NUL terminated message - even |
Serhiy Storchaka | 0a36ac1 | 2018-05-31 07:39:00 +0300 | [diff] [blame] | 626 | though it's not in the relevant specification (:rfc:`5424`). More recent |
Vinay Sajip | 645e458 | 2011-06-10 18:52:50 +0100 | [diff] [blame] | 627 | versions of these daemons don't expect the NUL byte but strip it off |
| 628 | if it's there, and even more recent daemons (which adhere more closely |
| 629 | to RFC 5424) pass the NUL byte on as part of the message. |
| 630 | |
| 631 | To enable easier handling of syslog messages in the face of all these |
| 632 | differing daemon behaviours, the appending of the NUL byte has been |
| 633 | made configurable, through the use of a class-level attribute, |
| 634 | ``append_nul``. This defaults to ``True`` (preserving the existing |
| 635 | behaviour) but can be set to ``False`` on a ``SysLogHandler`` instance |
| 636 | in order for that instance to *not* append the NUL terminator. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 637 | |
Vinay Sajip | 2353e35 | 2011-06-27 15:40:06 +0100 | [diff] [blame] | 638 | .. versionchanged:: 3.3 |
| 639 | (See: :issue:`12419`.) In earlier versions, there was no facility for |
| 640 | an "ident" or "tag" prefix to identify the source of the message. This |
| 641 | can now be specified using a class-level attribute, defaulting to |
| 642 | ``""`` to preserve existing behaviour, but which can be overridden on |
| 643 | a ``SysLogHandler`` instance in order for that instance to prepend |
| 644 | the ident to every message handled. Note that the provided ident must |
| 645 | be text, not bytes, and is prepended to the message exactly as is. |
| 646 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 647 | .. method:: encodePriority(facility, priority) |
| 648 | |
| 649 | Encodes the facility and priority into an integer. You can pass in strings |
| 650 | or integers - if strings are passed, internal mapping dictionaries are |
| 651 | used to convert them to integers. |
| 652 | |
| 653 | The symbolic ``LOG_`` values are defined in :class:`SysLogHandler` and |
| 654 | mirror the values defined in the ``sys/syslog.h`` header file. |
| 655 | |
| 656 | **Priorities** |
| 657 | |
| 658 | +--------------------------+---------------+ |
| 659 | | Name (string) | Symbolic value| |
| 660 | +==========================+===============+ |
| 661 | | ``alert`` | LOG_ALERT | |
| 662 | +--------------------------+---------------+ |
| 663 | | ``crit`` or ``critical`` | LOG_CRIT | |
| 664 | +--------------------------+---------------+ |
| 665 | | ``debug`` | LOG_DEBUG | |
| 666 | +--------------------------+---------------+ |
| 667 | | ``emerg`` or ``panic`` | LOG_EMERG | |
| 668 | +--------------------------+---------------+ |
| 669 | | ``err`` or ``error`` | LOG_ERR | |
| 670 | +--------------------------+---------------+ |
| 671 | | ``info`` | LOG_INFO | |
| 672 | +--------------------------+---------------+ |
| 673 | | ``notice`` | LOG_NOTICE | |
| 674 | +--------------------------+---------------+ |
| 675 | | ``warn`` or ``warning`` | LOG_WARNING | |
| 676 | +--------------------------+---------------+ |
| 677 | |
| 678 | **Facilities** |
| 679 | |
| 680 | +---------------+---------------+ |
| 681 | | Name (string) | Symbolic value| |
| 682 | +===============+===============+ |
| 683 | | ``auth`` | LOG_AUTH | |
| 684 | +---------------+---------------+ |
| 685 | | ``authpriv`` | LOG_AUTHPRIV | |
| 686 | +---------------+---------------+ |
| 687 | | ``cron`` | LOG_CRON | |
| 688 | +---------------+---------------+ |
| 689 | | ``daemon`` | LOG_DAEMON | |
| 690 | +---------------+---------------+ |
| 691 | | ``ftp`` | LOG_FTP | |
| 692 | +---------------+---------------+ |
| 693 | | ``kern`` | LOG_KERN | |
| 694 | +---------------+---------------+ |
| 695 | | ``lpr`` | LOG_LPR | |
| 696 | +---------------+---------------+ |
| 697 | | ``mail`` | LOG_MAIL | |
| 698 | +---------------+---------------+ |
| 699 | | ``news`` | LOG_NEWS | |
| 700 | +---------------+---------------+ |
| 701 | | ``syslog`` | LOG_SYSLOG | |
| 702 | +---------------+---------------+ |
| 703 | | ``user`` | LOG_USER | |
| 704 | +---------------+---------------+ |
| 705 | | ``uucp`` | LOG_UUCP | |
| 706 | +---------------+---------------+ |
| 707 | | ``local0`` | LOG_LOCAL0 | |
| 708 | +---------------+---------------+ |
| 709 | | ``local1`` | LOG_LOCAL1 | |
| 710 | +---------------+---------------+ |
| 711 | | ``local2`` | LOG_LOCAL2 | |
| 712 | +---------------+---------------+ |
| 713 | | ``local3`` | LOG_LOCAL3 | |
| 714 | +---------------+---------------+ |
| 715 | | ``local4`` | LOG_LOCAL4 | |
| 716 | +---------------+---------------+ |
| 717 | | ``local5`` | LOG_LOCAL5 | |
| 718 | +---------------+---------------+ |
| 719 | | ``local6`` | LOG_LOCAL6 | |
| 720 | +---------------+---------------+ |
| 721 | | ``local7`` | LOG_LOCAL7 | |
| 722 | +---------------+---------------+ |
| 723 | |
| 724 | .. method:: mapPriority(levelname) |
| 725 | |
| 726 | Maps a logging level name to a syslog priority name. |
| 727 | You may need to override this if you are using custom levels, or |
| 728 | if the default algorithm is not suitable for your needs. The |
| 729 | default algorithm maps ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR`` and |
| 730 | ``CRITICAL`` to the equivalent syslog names, and all other level |
| 731 | names to 'warning'. |
| 732 | |
| 733 | .. _nt-eventlog-handler: |
| 734 | |
| 735 | NTEventLogHandler |
| 736 | ^^^^^^^^^^^^^^^^^ |
| 737 | |
| 738 | The :class:`NTEventLogHandler` class, located in the :mod:`logging.handlers` |
| 739 | module, supports sending logging messages to a local Windows NT, Windows 2000 or |
| 740 | Windows XP event log. Before you can use it, you need Mark Hammond's Win32 |
| 741 | extensions for Python installed. |
| 742 | |
| 743 | |
| 744 | .. class:: NTEventLogHandler(appname, dllname=None, logtype='Application') |
| 745 | |
| 746 | Returns a new instance of the :class:`NTEventLogHandler` class. The *appname* is |
| 747 | used to define the application name as it appears in the event log. An |
| 748 | appropriate registry entry is created using this name. The *dllname* should give |
| 749 | the fully qualified pathname of a .dll or .exe which contains message |
| 750 | definitions to hold in the log (if not specified, ``'win32service.pyd'`` is used |
| 751 | - this is installed with the Win32 extensions and contains some basic |
| 752 | placeholder message definitions. Note that use of these placeholders will make |
| 753 | your event logs big, as the entire message source is held in the log. If you |
| 754 | want slimmer logs, you have to pass in the name of your own .dll or .exe which |
| 755 | contains the message definitions you want to use in the event log). The |
| 756 | *logtype* is one of ``'Application'``, ``'System'`` or ``'Security'``, and |
| 757 | defaults to ``'Application'``. |
| 758 | |
| 759 | |
| 760 | .. method:: close() |
| 761 | |
| 762 | At this point, you can remove the application name from the registry as a |
| 763 | source of event log entries. However, if you do this, you will not be able |
| 764 | to see the events as you intended in the Event Log Viewer - it needs to be |
| 765 | able to access the registry to get the .dll name. The current version does |
| 766 | not do this. |
| 767 | |
| 768 | |
| 769 | .. method:: emit(record) |
| 770 | |
| 771 | Determines the message ID, event category and event type, and then logs |
| 772 | the message in the NT event log. |
| 773 | |
| 774 | |
| 775 | .. method:: getEventCategory(record) |
| 776 | |
| 777 | Returns the event category for the record. Override this if you want to |
| 778 | specify your own categories. This version returns 0. |
| 779 | |
| 780 | |
| 781 | .. method:: getEventType(record) |
| 782 | |
| 783 | Returns the event type for the record. Override this if you want to |
| 784 | specify your own types. This version does a mapping using the handler's |
| 785 | typemap attribute, which is set up in :meth:`__init__` to a dictionary |
| 786 | which contains mappings for :const:`DEBUG`, :const:`INFO`, |
| 787 | :const:`WARNING`, :const:`ERROR` and :const:`CRITICAL`. If you are using |
| 788 | your own levels, you will either need to override this method or place a |
| 789 | suitable dictionary in the handler's *typemap* attribute. |
| 790 | |
| 791 | |
| 792 | .. method:: getMessageID(record) |
| 793 | |
| 794 | Returns the message ID for the record. If you are using your own messages, |
| 795 | you could do this by having the *msg* passed to the logger being an ID |
| 796 | rather than a format string. Then, in here, you could use a dictionary |
| 797 | lookup to get the message ID. This version returns 1, which is the base |
| 798 | message ID in :file:`win32service.pyd`. |
| 799 | |
| 800 | .. _smtp-handler: |
| 801 | |
| 802 | SMTPHandler |
| 803 | ^^^^^^^^^^^ |
| 804 | |
| 805 | The :class:`SMTPHandler` class, located in the :mod:`logging.handlers` module, |
| 806 | supports sending logging messages to an email address via SMTP. |
| 807 | |
| 808 | |
Vinay Sajip | 38a12af | 2012-03-26 17:17:39 +0100 | [diff] [blame] | 809 | .. class:: SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 810 | |
| 811 | Returns a new instance of the :class:`SMTPHandler` class. The instance is |
| 812 | initialized with the from and to addresses and subject line of the email. The |
| 813 | *toaddrs* should be a list of strings. To specify a non-standard SMTP port, use |
| 814 | the (host, port) tuple format for the *mailhost* argument. If you use a string, |
| 815 | the standard SMTP port is used. If your SMTP server requires authentication, you |
| 816 | can specify a (username, password) tuple for the *credentials* argument. |
| 817 | |
Vinay Sajip | 9525956 | 2011-08-01 11:31:52 +0100 | [diff] [blame] | 818 | To specify the use of a secure protocol (TLS), pass in a tuple to the |
| 819 | *secure* argument. This will only be used when authentication credentials are |
| 820 | supplied. The tuple should be either an empty tuple, or a single-value tuple |
| 821 | with the name of a keyfile, or a 2-value tuple with the names of the keyfile |
| 822 | and certificate file. (This tuple is passed to the |
| 823 | :meth:`smtplib.SMTP.starttls` method.) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 824 | |
Vinay Sajip | 38a12af | 2012-03-26 17:17:39 +0100 | [diff] [blame] | 825 | A timeout can be specified for communication with the SMTP server using the |
| 826 | *timeout* argument. |
| 827 | |
| 828 | .. versionadded:: 3.3 |
| 829 | The *timeout* argument was added. |
| 830 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 831 | .. method:: emit(record) |
| 832 | |
| 833 | Formats the record and sends it to the specified addressees. |
| 834 | |
| 835 | |
| 836 | .. method:: getSubject(record) |
| 837 | |
| 838 | If you want to specify a subject line which is record-dependent, override |
| 839 | this method. |
| 840 | |
| 841 | .. _memory-handler: |
| 842 | |
| 843 | MemoryHandler |
| 844 | ^^^^^^^^^^^^^ |
| 845 | |
| 846 | The :class:`MemoryHandler` class, located in the :mod:`logging.handlers` module, |
| 847 | supports buffering of logging records in memory, periodically flushing them to a |
| 848 | :dfn:`target` handler. Flushing occurs whenever the buffer is full, or when an |
| 849 | event of a certain severity or greater is seen. |
| 850 | |
| 851 | :class:`MemoryHandler` is a subclass of the more general |
| 852 | :class:`BufferingHandler`, which is an abstract class. This buffers logging |
| 853 | records in memory. Whenever each record is added to the buffer, a check is made |
| 854 | by calling :meth:`shouldFlush` to see if the buffer should be flushed. If it |
Vinay Sajip | 8ece80f | 2012-03-26 17:09:58 +0100 | [diff] [blame] | 855 | should, then :meth:`flush` is expected to do the flushing. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 856 | |
| 857 | |
| 858 | .. class:: BufferingHandler(capacity) |
| 859 | |
| 860 | Initializes the handler with a buffer of the specified capacity. |
| 861 | |
| 862 | |
| 863 | .. method:: emit(record) |
| 864 | |
| 865 | Appends the record to the buffer. If :meth:`shouldFlush` returns true, |
| 866 | calls :meth:`flush` to process the buffer. |
| 867 | |
| 868 | |
| 869 | .. method:: flush() |
| 870 | |
| 871 | You can override this to implement custom flushing behavior. This version |
| 872 | just zaps the buffer to empty. |
| 873 | |
| 874 | |
| 875 | .. method:: shouldFlush(record) |
| 876 | |
| 877 | Returns true if the buffer is up to capacity. This method can be |
| 878 | overridden to implement custom flushing strategies. |
| 879 | |
| 880 | |
Vinay Sajip | cccf606 | 2016-07-22 16:27:31 +0100 | [diff] [blame] | 881 | .. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 882 | |
| 883 | Returns a new instance of the :class:`MemoryHandler` class. The instance is |
| 884 | initialized with a buffer size of *capacity*. If *flushLevel* is not specified, |
| 885 | :const:`ERROR` is used. If no *target* is specified, the target will need to be |
Vinay Sajip | cccf606 | 2016-07-22 16:27:31 +0100 | [diff] [blame] | 886 | set using :meth:`setTarget` before this handler does anything useful. If |
| 887 | *flushOnClose* is specified as ``False``, then the buffer is *not* flushed when |
| 888 | the handler is closed. If not specified or specified as ``True``, the previous |
| 889 | behaviour of flushing the buffer will occur when the handler is closed. |
| 890 | |
| 891 | .. versionchanged:: 3.6 |
| 892 | The *flushOnClose* parameter was added. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 893 | |
| 894 | |
| 895 | .. method:: close() |
| 896 | |
Ezio Melotti | 226231c | 2012-01-18 05:40:00 +0200 | [diff] [blame] | 897 | Calls :meth:`flush`, sets the target to ``None`` and clears the |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 898 | buffer. |
| 899 | |
| 900 | |
| 901 | .. method:: flush() |
| 902 | |
| 903 | For a :class:`MemoryHandler`, flushing means just sending the buffered |
| 904 | records to the target, if there is one. The buffer is also cleared when |
| 905 | this happens. Override if you want different behavior. |
| 906 | |
| 907 | |
| 908 | .. method:: setTarget(target) |
| 909 | |
| 910 | Sets the target handler for this handler. |
| 911 | |
| 912 | |
| 913 | .. method:: shouldFlush(record) |
| 914 | |
| 915 | Checks for buffer full or a record at the *flushLevel* or higher. |
| 916 | |
| 917 | |
| 918 | .. _http-handler: |
| 919 | |
| 920 | HTTPHandler |
| 921 | ^^^^^^^^^^^ |
| 922 | |
| 923 | The :class:`HTTPHandler` class, located in the :mod:`logging.handlers` module, |
| 924 | supports sending logging messages to a Web server, using either ``GET`` or |
| 925 | ``POST`` semantics. |
| 926 | |
| 927 | |
Benjamin Peterson | 43052a1 | 2014-11-23 20:36:44 -0600 | [diff] [blame] | 928 | .. class:: HTTPHandler(host, url, method='GET', secure=False, credentials=None, context=None) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 929 | |
| 930 | Returns a new instance of the :class:`HTTPHandler` class. The *host* can be |
Benjamin Peterson | 43052a1 | 2014-11-23 20:36:44 -0600 | [diff] [blame] | 931 | of the form ``host:port``, should you need to use a specific port number. If |
| 932 | no *method* is specified, ``GET`` is used. If *secure* is true, a HTTPS |
| 933 | connection will be used. The *context* parameter may be set to a |
| 934 | :class:`ssl.SSLContext` instance to configure the SSL settings used for the |
| 935 | HTTPS connection. If *credentials* is specified, it should be a 2-tuple |
| 936 | consisting of userid and password, which will be placed in a HTTP |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 937 | 'Authorization' header using Basic authentication. If you specify |
| 938 | credentials, you should also specify secure=True so that your userid and |
| 939 | password are not passed in cleartext across the wire. |
| 940 | |
Benjamin Peterson | a90e92d | 2014-11-23 20:38:37 -0600 | [diff] [blame] | 941 | .. versionchanged:: 3.5 |
Benjamin Peterson | 43052a1 | 2014-11-23 20:36:44 -0600 | [diff] [blame] | 942 | The *context* parameter was added. |
| 943 | |
Vinay Sajip | c673a9a | 2014-05-30 18:59:27 +0100 | [diff] [blame] | 944 | .. method:: mapLogRecord(record) |
| 945 | |
| 946 | Provides a dictionary, based on ``record``, which is to be URL-encoded |
| 947 | and sent to the web server. The default implementation just returns |
| 948 | ``record.__dict__``. This method can be overridden if e.g. only a |
| 949 | subset of :class:`~logging.LogRecord` is to be sent to the web server, or |
| 950 | if more specific customization of what's sent to the server is required. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 951 | |
| 952 | .. method:: emit(record) |
| 953 | |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 954 | Sends the record to the Web server as a URL-encoded dictionary. The |
Vinay Sajip | c673a9a | 2014-05-30 18:59:27 +0100 | [diff] [blame] | 955 | :meth:`mapLogRecord` method is used to convert the record to the |
| 956 | dictionary to be sent. |
| 957 | |
Berker Peksag | 9c1dba2 | 2014-09-28 00:00:58 +0300 | [diff] [blame] | 958 | .. note:: Since preparing a record for sending it to a Web server is not |
Vinay Sajip | c673a9a | 2014-05-30 18:59:27 +0100 | [diff] [blame] | 959 | the same as a generic formatting operation, using |
| 960 | :meth:`~logging.Handler.setFormatter` to specify a |
| 961 | :class:`~logging.Formatter` for a :class:`HTTPHandler` has no effect. |
| 962 | Instead of calling :meth:`~logging.Handler.format`, this handler calls |
| 963 | :meth:`mapLogRecord` and then :func:`urllib.parse.urlencode` to encode the |
| 964 | dictionary in a form suitable for sending to a Web server. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 965 | |
| 966 | |
| 967 | .. _queue-handler: |
| 968 | |
| 969 | |
| 970 | QueueHandler |
| 971 | ^^^^^^^^^^^^ |
| 972 | |
| 973 | .. versionadded:: 3.2 |
| 974 | |
| 975 | The :class:`QueueHandler` class, located in the :mod:`logging.handlers` module, |
| 976 | supports sending logging messages to a queue, such as those implemented in the |
| 977 | :mod:`queue` or :mod:`multiprocessing` modules. |
| 978 | |
| 979 | Along with the :class:`QueueListener` class, :class:`QueueHandler` can be used |
| 980 | to let handlers do their work on a separate thread from the one which does the |
| 981 | logging. This is important in Web applications and also other service |
| 982 | applications where threads servicing clients need to respond as quickly as |
| 983 | possible, while any potentially slow operations (such as sending an email via |
| 984 | :class:`SMTPHandler`) are done on a separate thread. |
| 985 | |
| 986 | .. class:: QueueHandler(queue) |
| 987 | |
| 988 | Returns a new instance of the :class:`QueueHandler` class. The instance is |
Martin Panter | 8f13783 | 2017-01-14 08:24:20 +0000 | [diff] [blame] | 989 | initialized with the queue to send messages to. The queue can be any |
| 990 | queue-like object; it's used as-is by the :meth:`enqueue` method, which needs |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 991 | to know how to send messages to it. |
| 992 | |
| 993 | |
| 994 | .. method:: emit(record) |
| 995 | |
| 996 | Enqueues the result of preparing the LogRecord. |
| 997 | |
| 998 | .. method:: prepare(record) |
| 999 | |
| 1000 | Prepares a record for queuing. The object returned by this |
| 1001 | method is enqueued. |
| 1002 | |
Cheryl Sabella | d345bb4 | 2018-09-25 19:00:08 -0400 | [diff] [blame] | 1003 | The base implementation formats the record to merge the message, |
| 1004 | arguments, and exception information, if present. It also |
| 1005 | removes unpickleable items from the record in-place. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1006 | |
| 1007 | You might want to override this method if you want to convert |
| 1008 | the record to a dict or JSON string, or send a modified copy |
| 1009 | of the record while leaving the original intact. |
| 1010 | |
| 1011 | .. method:: enqueue(record) |
| 1012 | |
| 1013 | Enqueues the record on the queue using ``put_nowait()``; you may |
| 1014 | want to override this if you want to use blocking behaviour, or a |
Vinay Sajip | 9c10d6b | 2013-11-15 20:58:13 +0000 | [diff] [blame] | 1015 | timeout, or a customized queue implementation. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1016 | |
| 1017 | |
| 1018 | |
Éric Araujo | 5eada94 | 2011-08-19 00:41:23 +0200 | [diff] [blame] | 1019 | .. _queue-listener: |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1020 | |
| 1021 | QueueListener |
| 1022 | ^^^^^^^^^^^^^ |
| 1023 | |
| 1024 | .. versionadded:: 3.2 |
| 1025 | |
| 1026 | The :class:`QueueListener` class, located in the :mod:`logging.handlers` |
| 1027 | module, supports receiving logging messages from a queue, such as those |
| 1028 | implemented in the :mod:`queue` or :mod:`multiprocessing` modules. The |
| 1029 | messages are received from a queue in an internal thread and passed, on |
| 1030 | the same thread, to one or more handlers for processing. While |
| 1031 | :class:`QueueListener` is not itself a handler, it is documented here |
| 1032 | because it works hand-in-hand with :class:`QueueHandler`. |
| 1033 | |
| 1034 | Along with the :class:`QueueHandler` class, :class:`QueueListener` can be used |
| 1035 | to let handlers do their work on a separate thread from the one which does the |
| 1036 | logging. This is important in Web applications and also other service |
| 1037 | applications where threads servicing clients need to respond as quickly as |
| 1038 | possible, while any potentially slow operations (such as sending an email via |
| 1039 | :class:`SMTPHandler`) are done on a separate thread. |
| 1040 | |
Vinay Sajip | 365701a | 2015-02-09 19:49:00 +0000 | [diff] [blame] | 1041 | .. class:: QueueListener(queue, *handlers, respect_handler_level=False) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1042 | |
| 1043 | Returns a new instance of the :class:`QueueListener` class. The instance is |
| 1044 | initialized with the queue to send messages to and a list of handlers which |
Martin Panter | 8f13783 | 2017-01-14 08:24:20 +0000 | [diff] [blame] | 1045 | will handle entries placed on the queue. The queue can be any queue-like |
| 1046 | object; it's passed as-is to the :meth:`dequeue` method, which needs |
Vinay Sajip | 365701a | 2015-02-09 19:49:00 +0000 | [diff] [blame] | 1047 | to know how to get messages from it. If ``respect_handler_level`` is ``True``, |
| 1048 | a handler's level is respected (compared with the level for the message) when |
| 1049 | deciding whether to pass messages to that handler; otherwise, the behaviour |
| 1050 | is as in previous Python versions - to always pass each message to each |
| 1051 | handler. |
| 1052 | |
| 1053 | .. versionchanged:: 3.5 |
| 1054 | The ``respect_handler_levels`` argument was added. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1055 | |
| 1056 | .. method:: dequeue(block) |
| 1057 | |
| 1058 | Dequeues a record and return it, optionally blocking. |
| 1059 | |
| 1060 | The base implementation uses ``get()``. You may want to override this |
| 1061 | method if you want to use timeouts or work with custom queue |
| 1062 | implementations. |
| 1063 | |
| 1064 | .. method:: prepare(record) |
| 1065 | |
| 1066 | Prepare a record for handling. |
| 1067 | |
| 1068 | This implementation just returns the passed-in record. You may want to |
| 1069 | override this method if you need to do any custom marshalling or |
| 1070 | manipulation of the record before passing it to the handlers. |
| 1071 | |
| 1072 | .. method:: handle(record) |
| 1073 | |
| 1074 | Handle a record. |
| 1075 | |
| 1076 | This just loops through the handlers offering them the record |
| 1077 | to handle. The actual object passed to the handlers is that which |
| 1078 | is returned from :meth:`prepare`. |
| 1079 | |
| 1080 | .. method:: start() |
| 1081 | |
| 1082 | Starts the listener. |
| 1083 | |
| 1084 | This starts up a background thread to monitor the queue for |
| 1085 | LogRecords to process. |
| 1086 | |
| 1087 | .. method:: stop() |
| 1088 | |
| 1089 | Stops the listener. |
| 1090 | |
| 1091 | This asks the thread to terminate, and then waits for it to do so. |
| 1092 | Note that if you don't call this before your application exits, there |
| 1093 | may be some records still left on the queue, which won't be processed. |
| 1094 | |
Vinay Sajip | a29a9dd | 2011-02-25 16:05:26 +0000 | [diff] [blame] | 1095 | .. method:: enqueue_sentinel() |
| 1096 | |
| 1097 | Writes a sentinel to the queue to tell the listener to quit. This |
| 1098 | implementation uses ``put_nowait()``. You may want to override this |
| 1099 | method if you want to use timeouts or work with custom queue |
| 1100 | implementations. |
| 1101 | |
| 1102 | .. versionadded:: 3.3 |
| 1103 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1104 | |
| 1105 | .. seealso:: |
| 1106 | |
| 1107 | Module :mod:`logging` |
| 1108 | API reference for the logging module. |
| 1109 | |
| 1110 | Module :mod:`logging.config` |
| 1111 | Configuration API for the logging module. |
| 1112 | |
| 1113 | |