17db96d56Sopenharmony_ci:mod:`logging.handlers` --- Logging handlers 27db96d56Sopenharmony_ci============================================ 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: logging.handlers 57db96d56Sopenharmony_ci :synopsis: Handlers for the logging module. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. moduleauthor:: Vinay Sajip <vinay_sajip@red-dove.com> 87db96d56Sopenharmony_ci.. sectionauthor:: Vinay Sajip <vinay_sajip@red-dove.com> 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ci**Source code:** :source:`Lib/logging/handlers.py` 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci.. sidebar:: Important 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci This page contains only reference information. For tutorials, 157db96d56Sopenharmony_ci please see 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ci * :ref:`Basic Tutorial <logging-basic-tutorial>` 187db96d56Sopenharmony_ci * :ref:`Advanced Tutorial <logging-advanced-tutorial>` 197db96d56Sopenharmony_ci * :ref:`Logging Cookbook <logging-cookbook>` 207db96d56Sopenharmony_ci 217db96d56Sopenharmony_ci-------------- 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ci.. currentmodule:: logging 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ciThe following useful handlers are provided in the package. Note that three of 267db96d56Sopenharmony_cithe handlers (:class:`StreamHandler`, :class:`FileHandler` and 277db96d56Sopenharmony_ci:class:`NullHandler`) are actually defined in the :mod:`logging` module itself, 287db96d56Sopenharmony_cibut have been documented here along with the other handlers. 297db96d56Sopenharmony_ci 307db96d56Sopenharmony_ci.. _stream-handler: 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ciStreamHandler 337db96d56Sopenharmony_ci^^^^^^^^^^^^^ 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ciThe :class:`StreamHandler` class, located in the core :mod:`logging` package, 367db96d56Sopenharmony_cisends logging output to streams such as *sys.stdout*, *sys.stderr* or any 377db96d56Sopenharmony_cifile-like object (or, more precisely, any object which supports :meth:`write` 387db96d56Sopenharmony_ciand :meth:`flush` methods). 397db96d56Sopenharmony_ci 407db96d56Sopenharmony_ci 417db96d56Sopenharmony_ci.. class:: StreamHandler(stream=None) 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ci Returns a new instance of the :class:`StreamHandler` class. If *stream* is 447db96d56Sopenharmony_ci specified, the instance will use it for logging output; otherwise, *sys.stderr* 457db96d56Sopenharmony_ci will be used. 467db96d56Sopenharmony_ci 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ci .. method:: emit(record) 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci If a formatter is specified, it is used to format the record. The record 517db96d56Sopenharmony_ci is then written to the stream followed by :attr:`terminator`. If exception information 527db96d56Sopenharmony_ci is present, it is formatted using :func:`traceback.print_exception` and 537db96d56Sopenharmony_ci appended to the stream. 547db96d56Sopenharmony_ci 557db96d56Sopenharmony_ci 567db96d56Sopenharmony_ci .. method:: flush() 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ci Flushes the stream by calling its :meth:`flush` method. Note that the 597db96d56Sopenharmony_ci :meth:`close` method is inherited from :class:`~logging.Handler` and so 607db96d56Sopenharmony_ci does no output, so an explicit :meth:`flush` call may be needed at times. 617db96d56Sopenharmony_ci 627db96d56Sopenharmony_ci .. method:: setStream(stream) 637db96d56Sopenharmony_ci 647db96d56Sopenharmony_ci Sets the instance's stream to the specified value, if it is different. 657db96d56Sopenharmony_ci The old stream is flushed before the new stream is set. 667db96d56Sopenharmony_ci 677db96d56Sopenharmony_ci :param stream: The stream that the handler should use. 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci :return: the old stream, if the stream was changed, or *None* if it wasn't. 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci .. versionadded:: 3.7 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ci .. attribute:: terminator 747db96d56Sopenharmony_ci 757db96d56Sopenharmony_ci String used as the terminator when writing a formatted record to a stream. 767db96d56Sopenharmony_ci Default value is ``'\n'``. 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci If you don't want a newline termination, you can set the handler instance's 797db96d56Sopenharmony_ci ``terminator`` attribute to the empty string. 807db96d56Sopenharmony_ci 817db96d56Sopenharmony_ci In earlier versions, the terminator was hardcoded as ``'\n'``. 827db96d56Sopenharmony_ci 837db96d56Sopenharmony_ci .. versionadded:: 3.2 847db96d56Sopenharmony_ci 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci.. _file-handler: 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ciFileHandler 897db96d56Sopenharmony_ci^^^^^^^^^^^ 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ciThe :class:`FileHandler` class, located in the core :mod:`logging` package, 927db96d56Sopenharmony_cisends logging output to a disk file. It inherits the output functionality from 937db96d56Sopenharmony_ci:class:`StreamHandler`. 947db96d56Sopenharmony_ci 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci.. class:: FileHandler(filename, mode='a', encoding=None, delay=False, errors=None) 977db96d56Sopenharmony_ci 987db96d56Sopenharmony_ci Returns a new instance of the :class:`FileHandler` class. The specified file is 997db96d56Sopenharmony_ci opened and used as the stream for logging. If *mode* is not specified, 1007db96d56Sopenharmony_ci :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file 1017db96d56Sopenharmony_ci with that encoding. If *delay* is true, then file opening is deferred until the 1027db96d56Sopenharmony_ci first call to :meth:`emit`. By default, the file grows indefinitely. If 1037db96d56Sopenharmony_ci *errors* is specified, it's used to determine how encoding errors are handled. 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ci .. versionchanged:: 3.6 1067db96d56Sopenharmony_ci As well as string values, :class:`~pathlib.Path` objects are also accepted 1077db96d56Sopenharmony_ci for the *filename* argument. 1087db96d56Sopenharmony_ci 1097db96d56Sopenharmony_ci .. versionchanged:: 3.9 1107db96d56Sopenharmony_ci The *errors* parameter was added. 1117db96d56Sopenharmony_ci 1127db96d56Sopenharmony_ci .. method:: close() 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ci Closes the file. 1157db96d56Sopenharmony_ci 1167db96d56Sopenharmony_ci .. method:: emit(record) 1177db96d56Sopenharmony_ci 1187db96d56Sopenharmony_ci Outputs the record to the file. 1197db96d56Sopenharmony_ci 1207db96d56Sopenharmony_ci Note that if the file was closed due to logging shutdown at exit and the file 1217db96d56Sopenharmony_ci mode is 'w', the record will not be emitted (see :issue:`42378`). 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_ci 1247db96d56Sopenharmony_ci.. _null-handler: 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ciNullHandler 1277db96d56Sopenharmony_ci^^^^^^^^^^^ 1287db96d56Sopenharmony_ci 1297db96d56Sopenharmony_ci.. versionadded:: 3.1 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ciThe :class:`NullHandler` class, located in the core :mod:`logging` package, 1327db96d56Sopenharmony_cidoes not do any formatting or output. It is essentially a 'no-op' handler 1337db96d56Sopenharmony_cifor use by library developers. 1347db96d56Sopenharmony_ci 1357db96d56Sopenharmony_ci.. class:: NullHandler() 1367db96d56Sopenharmony_ci 1377db96d56Sopenharmony_ci Returns a new instance of the :class:`NullHandler` class. 1387db96d56Sopenharmony_ci 1397db96d56Sopenharmony_ci .. method:: emit(record) 1407db96d56Sopenharmony_ci 1417db96d56Sopenharmony_ci This method does nothing. 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_ci .. method:: handle(record) 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci This method does nothing. 1467db96d56Sopenharmony_ci 1477db96d56Sopenharmony_ci .. method:: createLock() 1487db96d56Sopenharmony_ci 1497db96d56Sopenharmony_ci This method returns ``None`` for the lock, since there is no 1507db96d56Sopenharmony_ci underlying I/O to which access needs to be serialized. 1517db96d56Sopenharmony_ci 1527db96d56Sopenharmony_ci 1537db96d56Sopenharmony_ciSee :ref:`library-config` for more information on how to use 1547db96d56Sopenharmony_ci:class:`NullHandler`. 1557db96d56Sopenharmony_ci 1567db96d56Sopenharmony_ci.. _watched-file-handler: 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ciWatchedFileHandler 1597db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^ 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ci.. currentmodule:: logging.handlers 1627db96d56Sopenharmony_ci 1637db96d56Sopenharmony_ciThe :class:`WatchedFileHandler` class, located in the :mod:`logging.handlers` 1647db96d56Sopenharmony_cimodule, is a :class:`FileHandler` which watches the file it is logging to. If 1657db96d56Sopenharmony_cithe file changes, it is closed and reopened using the file name. 1667db96d56Sopenharmony_ci 1677db96d56Sopenharmony_ciA file change can happen because of usage of programs such as *newsyslog* and 1687db96d56Sopenharmony_ci*logrotate* which perform log file rotation. This handler, intended for use 1697db96d56Sopenharmony_ciunder Unix/Linux, watches the file to see if it has changed since the last emit. 1707db96d56Sopenharmony_ci(A file is deemed to have changed if its device or inode have changed.) If the 1717db96d56Sopenharmony_cifile has changed, the old file stream is closed, and the file opened to get a 1727db96d56Sopenharmony_cinew stream. 1737db96d56Sopenharmony_ci 1747db96d56Sopenharmony_ciThis handler is not appropriate for use under Windows, because under Windows 1757db96d56Sopenharmony_ciopen log files cannot be moved or renamed - logging opens the files with 1767db96d56Sopenharmony_ciexclusive locks - and so there is no need for such a handler. Furthermore, 1777db96d56Sopenharmony_ci*ST_INO* is not supported under Windows; :func:`~os.stat` always returns zero 1787db96d56Sopenharmony_cifor this value. 1797db96d56Sopenharmony_ci 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ci.. class:: WatchedFileHandler(filename, mode='a', encoding=None, delay=False, errors=None) 1827db96d56Sopenharmony_ci 1837db96d56Sopenharmony_ci Returns a new instance of the :class:`WatchedFileHandler` class. The specified 1847db96d56Sopenharmony_ci file is opened and used as the stream for logging. If *mode* is not specified, 1857db96d56Sopenharmony_ci :const:`'a'` is used. If *encoding* is not ``None``, it is used to open the file 1867db96d56Sopenharmony_ci with that encoding. If *delay* is true, then file opening is deferred until the 1877db96d56Sopenharmony_ci first call to :meth:`emit`. By default, the file grows indefinitely. If 1887db96d56Sopenharmony_ci *errors* is provided, it determines how encoding errors are handled. 1897db96d56Sopenharmony_ci 1907db96d56Sopenharmony_ci .. versionchanged:: 3.6 1917db96d56Sopenharmony_ci As well as string values, :class:`~pathlib.Path` objects are also accepted 1927db96d56Sopenharmony_ci for the *filename* argument. 1937db96d56Sopenharmony_ci 1947db96d56Sopenharmony_ci .. versionchanged:: 3.9 1957db96d56Sopenharmony_ci The *errors* parameter was added. 1967db96d56Sopenharmony_ci 1977db96d56Sopenharmony_ci .. method:: reopenIfNeeded() 1987db96d56Sopenharmony_ci 1997db96d56Sopenharmony_ci Checks to see if the file has changed. If it has, the existing stream is 2007db96d56Sopenharmony_ci flushed and closed and the file opened again, typically as a precursor to 2017db96d56Sopenharmony_ci outputting the record to the file. 2027db96d56Sopenharmony_ci 2037db96d56Sopenharmony_ci .. versionadded:: 3.6 2047db96d56Sopenharmony_ci 2057db96d56Sopenharmony_ci 2067db96d56Sopenharmony_ci .. method:: emit(record) 2077db96d56Sopenharmony_ci 2087db96d56Sopenharmony_ci Outputs the record to the file, but first calls :meth:`reopenIfNeeded` to 2097db96d56Sopenharmony_ci reopen the file if it has changed. 2107db96d56Sopenharmony_ci 2117db96d56Sopenharmony_ci.. _base-rotating-handler: 2127db96d56Sopenharmony_ci 2137db96d56Sopenharmony_ciBaseRotatingHandler 2147db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^ 2157db96d56Sopenharmony_ci 2167db96d56Sopenharmony_ciThe :class:`BaseRotatingHandler` class, located in the :mod:`logging.handlers` 2177db96d56Sopenharmony_cimodule, is the base class for the rotating file handlers, 2187db96d56Sopenharmony_ci:class:`RotatingFileHandler` and :class:`TimedRotatingFileHandler`. You should 2197db96d56Sopenharmony_cinot need to instantiate this class, but it has attributes and methods you may 2207db96d56Sopenharmony_cineed to override. 2217db96d56Sopenharmony_ci 2227db96d56Sopenharmony_ci.. class:: BaseRotatingHandler(filename, mode, encoding=None, delay=False, errors=None) 2237db96d56Sopenharmony_ci 2247db96d56Sopenharmony_ci The parameters are as for :class:`FileHandler`. The attributes are: 2257db96d56Sopenharmony_ci 2267db96d56Sopenharmony_ci .. attribute:: namer 2277db96d56Sopenharmony_ci 2287db96d56Sopenharmony_ci If this attribute is set to a callable, the :meth:`rotation_filename` 2297db96d56Sopenharmony_ci method delegates to this callable. The parameters passed to the callable 2307db96d56Sopenharmony_ci are those passed to :meth:`rotation_filename`. 2317db96d56Sopenharmony_ci 2327db96d56Sopenharmony_ci .. note:: The namer function is called quite a few times during rollover, 2337db96d56Sopenharmony_ci so it should be as simple and as fast as possible. It should also 2347db96d56Sopenharmony_ci return the same output every time for a given input, otherwise the 2357db96d56Sopenharmony_ci rollover behaviour may not work as expected. 2367db96d56Sopenharmony_ci 2377db96d56Sopenharmony_ci It's also worth noting that care should be taken when using a namer to 2387db96d56Sopenharmony_ci preserve certain attributes in the filename which are used during rotation. 2397db96d56Sopenharmony_ci For example, :class:`RotatingFileHandler` expects to have a set of log files 2407db96d56Sopenharmony_ci whose names contain successive integers, so that rotation works as expected, 2417db96d56Sopenharmony_ci and :class:`TimedRotatingFileHandler` deletes old log files (based on the 2427db96d56Sopenharmony_ci ``backupCount`` parameter passed to the handler's initializer) by determining 2437db96d56Sopenharmony_ci the oldest files to delete. For this to happen, the filenames should be 2447db96d56Sopenharmony_ci sortable using the date/time portion of the filename, and a namer needs to 2457db96d56Sopenharmony_ci respect this. (If a namer is wanted that doesn't respect this scheme, it will 2467db96d56Sopenharmony_ci need to be used in a subclass of :class:`TimedRotatingFileHandler` which 2477db96d56Sopenharmony_ci overrides the :meth:`~TimedRotatingFileHandler.getFilesToDelete` method to 2487db96d56Sopenharmony_ci fit in with the custom naming scheme.) 2497db96d56Sopenharmony_ci 2507db96d56Sopenharmony_ci .. versionadded:: 3.3 2517db96d56Sopenharmony_ci 2527db96d56Sopenharmony_ci 2537db96d56Sopenharmony_ci .. attribute:: BaseRotatingHandler.rotator 2547db96d56Sopenharmony_ci 2557db96d56Sopenharmony_ci If this attribute is set to a callable, the :meth:`rotate` method 2567db96d56Sopenharmony_ci delegates to this callable. The parameters passed to the callable are 2577db96d56Sopenharmony_ci those passed to :meth:`rotate`. 2587db96d56Sopenharmony_ci 2597db96d56Sopenharmony_ci .. versionadded:: 3.3 2607db96d56Sopenharmony_ci 2617db96d56Sopenharmony_ci .. method:: BaseRotatingHandler.rotation_filename(default_name) 2627db96d56Sopenharmony_ci 2637db96d56Sopenharmony_ci Modify the filename of a log file when rotating. 2647db96d56Sopenharmony_ci 2657db96d56Sopenharmony_ci This is provided so that a custom filename can be provided. 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ci The default implementation calls the 'namer' attribute of the handler, 2687db96d56Sopenharmony_ci if it's callable, passing the default name to it. If the attribute isn't 2697db96d56Sopenharmony_ci callable (the default is ``None``), the name is returned unchanged. 2707db96d56Sopenharmony_ci 2717db96d56Sopenharmony_ci :param default_name: The default name for the log file. 2727db96d56Sopenharmony_ci 2737db96d56Sopenharmony_ci .. versionadded:: 3.3 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ci 2767db96d56Sopenharmony_ci .. method:: BaseRotatingHandler.rotate(source, dest) 2777db96d56Sopenharmony_ci 2787db96d56Sopenharmony_ci When rotating, rotate the current log. 2797db96d56Sopenharmony_ci 2807db96d56Sopenharmony_ci The default implementation calls the 'rotator' attribute of the handler, 2817db96d56Sopenharmony_ci if it's callable, passing the source and dest arguments to it. If the 2827db96d56Sopenharmony_ci attribute isn't callable (the default is ``None``), the source is simply 2837db96d56Sopenharmony_ci renamed to the destination. 2847db96d56Sopenharmony_ci 2857db96d56Sopenharmony_ci :param source: The source filename. This is normally the base 2867db96d56Sopenharmony_ci filename, e.g. 'test.log'. 2877db96d56Sopenharmony_ci :param dest: The destination filename. This is normally 2887db96d56Sopenharmony_ci what the source is rotated to, e.g. 'test.log.1'. 2897db96d56Sopenharmony_ci 2907db96d56Sopenharmony_ci .. versionadded:: 3.3 2917db96d56Sopenharmony_ci 2927db96d56Sopenharmony_ciThe reason the attributes exist is to save you having to subclass - you can use 2937db96d56Sopenharmony_cithe same callables for instances of :class:`RotatingFileHandler` and 2947db96d56Sopenharmony_ci:class:`TimedRotatingFileHandler`. If either the namer or rotator callable 2957db96d56Sopenharmony_ciraises an exception, this will be handled in the same way as any other 2967db96d56Sopenharmony_ciexception during an :meth:`emit` call, i.e. via the :meth:`handleError` method 2977db96d56Sopenharmony_ciof the handler. 2987db96d56Sopenharmony_ci 2997db96d56Sopenharmony_ciIf you need to make more significant changes to rotation processing, you can 3007db96d56Sopenharmony_cioverride the methods. 3017db96d56Sopenharmony_ci 3027db96d56Sopenharmony_ciFor an example, see :ref:`cookbook-rotator-namer`. 3037db96d56Sopenharmony_ci 3047db96d56Sopenharmony_ci 3057db96d56Sopenharmony_ci.. _rotating-file-handler: 3067db96d56Sopenharmony_ci 3077db96d56Sopenharmony_ciRotatingFileHandler 3087db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^ 3097db96d56Sopenharmony_ci 3107db96d56Sopenharmony_ciThe :class:`RotatingFileHandler` class, located in the :mod:`logging.handlers` 3117db96d56Sopenharmony_cimodule, supports rotation of disk log files. 3127db96d56Sopenharmony_ci 3137db96d56Sopenharmony_ci 3147db96d56Sopenharmony_ci.. class:: RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False, errors=None) 3157db96d56Sopenharmony_ci 3167db96d56Sopenharmony_ci Returns a new instance of the :class:`RotatingFileHandler` class. The specified 3177db96d56Sopenharmony_ci file is opened and used as the stream for logging. If *mode* is not specified, 3187db96d56Sopenharmony_ci ``'a'`` is used. If *encoding* is not ``None``, it is used to open the file 3197db96d56Sopenharmony_ci with that encoding. If *delay* is true, then file opening is deferred until the 3207db96d56Sopenharmony_ci first call to :meth:`emit`. By default, the file grows indefinitely. If 3217db96d56Sopenharmony_ci *errors* is provided, it determines how encoding errors are handled. 3227db96d56Sopenharmony_ci 3237db96d56Sopenharmony_ci You can use the *maxBytes* and *backupCount* values to allow the file to 3247db96d56Sopenharmony_ci :dfn:`rollover` at a predetermined size. When the size is about to be exceeded, 3257db96d56Sopenharmony_ci the file is closed and a new file is silently opened for output. Rollover occurs 3267db96d56Sopenharmony_ci whenever the current log file is nearly *maxBytes* in length; but if either of 3277db96d56Sopenharmony_ci *maxBytes* or *backupCount* is zero, rollover never occurs, so you generally want 3287db96d56Sopenharmony_ci to set *backupCount* to at least 1, and have a non-zero *maxBytes*. 3297db96d56Sopenharmony_ci When *backupCount* is non-zero, the system will save old log files by appending 3307db96d56Sopenharmony_ci the extensions '.1', '.2' etc., to the filename. For example, with a *backupCount* 3317db96d56Sopenharmony_ci of 5 and a base file name of :file:`app.log`, you would get :file:`app.log`, 3327db96d56Sopenharmony_ci :file:`app.log.1`, :file:`app.log.2`, up to :file:`app.log.5`. The file being 3337db96d56Sopenharmony_ci written to is always :file:`app.log`. When this file is filled, it is closed 3347db96d56Sopenharmony_ci and renamed to :file:`app.log.1`, and if files :file:`app.log.1`, 3357db96d56Sopenharmony_ci :file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`, 3367db96d56Sopenharmony_ci :file:`app.log.3` etc. respectively. 3377db96d56Sopenharmony_ci 3387db96d56Sopenharmony_ci .. versionchanged:: 3.6 3397db96d56Sopenharmony_ci As well as string values, :class:`~pathlib.Path` objects are also accepted 3407db96d56Sopenharmony_ci for the *filename* argument. 3417db96d56Sopenharmony_ci 3427db96d56Sopenharmony_ci .. versionchanged:: 3.9 3437db96d56Sopenharmony_ci The *errors* parameter was added. 3447db96d56Sopenharmony_ci 3457db96d56Sopenharmony_ci .. method:: doRollover() 3467db96d56Sopenharmony_ci 3477db96d56Sopenharmony_ci Does a rollover, as described above. 3487db96d56Sopenharmony_ci 3497db96d56Sopenharmony_ci 3507db96d56Sopenharmony_ci .. method:: emit(record) 3517db96d56Sopenharmony_ci 3527db96d56Sopenharmony_ci Outputs the record to the file, catering for rollover as described 3537db96d56Sopenharmony_ci previously. 3547db96d56Sopenharmony_ci 3557db96d56Sopenharmony_ci.. _timed-rotating-file-handler: 3567db96d56Sopenharmony_ci 3577db96d56Sopenharmony_ciTimedRotatingFileHandler 3587db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^ 3597db96d56Sopenharmony_ci 3607db96d56Sopenharmony_ciThe :class:`TimedRotatingFileHandler` class, located in the 3617db96d56Sopenharmony_ci:mod:`logging.handlers` module, supports rotation of disk log files at certain 3627db96d56Sopenharmony_citimed intervals. 3637db96d56Sopenharmony_ci 3647db96d56Sopenharmony_ci 3657db96d56Sopenharmony_ci.. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None, errors=None) 3667db96d56Sopenharmony_ci 3677db96d56Sopenharmony_ci Returns a new instance of the :class:`TimedRotatingFileHandler` class. The 3687db96d56Sopenharmony_ci specified file is opened and used as the stream for logging. On rotating it also 3697db96d56Sopenharmony_ci sets the filename suffix. Rotating happens based on the product of *when* and 3707db96d56Sopenharmony_ci *interval*. 3717db96d56Sopenharmony_ci 3727db96d56Sopenharmony_ci You can use the *when* to specify the type of *interval*. The list of possible 3737db96d56Sopenharmony_ci values is below. Note that they are not case sensitive. 3747db96d56Sopenharmony_ci 3757db96d56Sopenharmony_ci +----------------+----------------------------+-------------------------+ 3767db96d56Sopenharmony_ci | Value | Type of interval | If/how *atTime* is used | 3777db96d56Sopenharmony_ci +================+============================+=========================+ 3787db96d56Sopenharmony_ci | ``'S'`` | Seconds | Ignored | 3797db96d56Sopenharmony_ci +----------------+----------------------------+-------------------------+ 3807db96d56Sopenharmony_ci | ``'M'`` | Minutes | Ignored | 3817db96d56Sopenharmony_ci +----------------+----------------------------+-------------------------+ 3827db96d56Sopenharmony_ci | ``'H'`` | Hours | Ignored | 3837db96d56Sopenharmony_ci +----------------+----------------------------+-------------------------+ 3847db96d56Sopenharmony_ci | ``'D'`` | Days | Ignored | 3857db96d56Sopenharmony_ci +----------------+----------------------------+-------------------------+ 3867db96d56Sopenharmony_ci | ``'W0'-'W6'`` | Weekday (0=Monday) | Used to compute initial | 3877db96d56Sopenharmony_ci | | | rollover time | 3887db96d56Sopenharmony_ci +----------------+----------------------------+-------------------------+ 3897db96d56Sopenharmony_ci | ``'midnight'`` | Roll over at midnight, if | Used to compute initial | 3907db96d56Sopenharmony_ci | | *atTime* not specified, | rollover time | 3917db96d56Sopenharmony_ci | | else at time *atTime* | | 3927db96d56Sopenharmony_ci +----------------+----------------------------+-------------------------+ 3937db96d56Sopenharmony_ci 3947db96d56Sopenharmony_ci When using weekday-based rotation, specify 'W0' for Monday, 'W1' for 3957db96d56Sopenharmony_ci Tuesday, and so on up to 'W6' for Sunday. In this case, the value passed for 3967db96d56Sopenharmony_ci *interval* isn't used. 3977db96d56Sopenharmony_ci 3987db96d56Sopenharmony_ci The system will save old log files by appending extensions to the filename. 3997db96d56Sopenharmony_ci The extensions are date-and-time based, using the strftime format 4007db96d56Sopenharmony_ci ``%Y-%m-%d_%H-%M-%S`` or a leading portion thereof, depending on the 4017db96d56Sopenharmony_ci rollover interval. 4027db96d56Sopenharmony_ci 4037db96d56Sopenharmony_ci When computing the next rollover time for the first time (when the handler 4047db96d56Sopenharmony_ci is created), the last modification time of an existing log file, or else 4057db96d56Sopenharmony_ci the current time, is used to compute when the next rotation will occur. 4067db96d56Sopenharmony_ci 4077db96d56Sopenharmony_ci If the *utc* argument is true, times in UTC will be used; otherwise 4087db96d56Sopenharmony_ci local time is used. 4097db96d56Sopenharmony_ci 4107db96d56Sopenharmony_ci If *backupCount* is nonzero, at most *backupCount* files 4117db96d56Sopenharmony_ci will be kept, and if more would be created when rollover occurs, the oldest 4127db96d56Sopenharmony_ci one is deleted. The deletion logic uses the interval to determine which 4137db96d56Sopenharmony_ci files to delete, so changing the interval may leave old files lying around. 4147db96d56Sopenharmony_ci 4157db96d56Sopenharmony_ci If *delay* is true, then file opening is deferred until the first call to 4167db96d56Sopenharmony_ci :meth:`emit`. 4177db96d56Sopenharmony_ci 4187db96d56Sopenharmony_ci If *atTime* is not ``None``, it must be a ``datetime.time`` instance which 4197db96d56Sopenharmony_ci specifies the time of day when rollover occurs, for the cases where rollover 4207db96d56Sopenharmony_ci is set to happen "at midnight" or "on a particular weekday". Note that in 4217db96d56Sopenharmony_ci these cases, the *atTime* value is effectively used to compute the *initial* 4227db96d56Sopenharmony_ci rollover, and subsequent rollovers would be calculated via the normal 4237db96d56Sopenharmony_ci interval calculation. 4247db96d56Sopenharmony_ci 4257db96d56Sopenharmony_ci If *errors* is specified, it's used to determine how encoding errors are 4267db96d56Sopenharmony_ci handled. 4277db96d56Sopenharmony_ci 4287db96d56Sopenharmony_ci .. note:: Calculation of the initial rollover time is done when the handler 4297db96d56Sopenharmony_ci is initialised. Calculation of subsequent rollover times is done only 4307db96d56Sopenharmony_ci when rollover occurs, and rollover occurs only when emitting output. If 4317db96d56Sopenharmony_ci this is not kept in mind, it might lead to some confusion. For example, 4327db96d56Sopenharmony_ci if an interval of "every minute" is set, that does not mean you will 4337db96d56Sopenharmony_ci always see log files with times (in the filename) separated by a minute; 4347db96d56Sopenharmony_ci if, during application execution, logging output is generated more 4357db96d56Sopenharmony_ci frequently than once a minute, *then* you can expect to see log files 4367db96d56Sopenharmony_ci with times separated by a minute. If, on the other hand, logging messages 4377db96d56Sopenharmony_ci are only output once every five minutes (say), then there will be gaps in 4387db96d56Sopenharmony_ci the file times corresponding to the minutes where no output (and hence no 4397db96d56Sopenharmony_ci rollover) occurred. 4407db96d56Sopenharmony_ci 4417db96d56Sopenharmony_ci .. versionchanged:: 3.4 4427db96d56Sopenharmony_ci *atTime* parameter was added. 4437db96d56Sopenharmony_ci 4447db96d56Sopenharmony_ci .. versionchanged:: 3.6 4457db96d56Sopenharmony_ci As well as string values, :class:`~pathlib.Path` objects are also accepted 4467db96d56Sopenharmony_ci for the *filename* argument. 4477db96d56Sopenharmony_ci 4487db96d56Sopenharmony_ci .. versionchanged:: 3.9 4497db96d56Sopenharmony_ci The *errors* parameter was added. 4507db96d56Sopenharmony_ci 4517db96d56Sopenharmony_ci .. method:: doRollover() 4527db96d56Sopenharmony_ci 4537db96d56Sopenharmony_ci Does a rollover, as described above. 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ci .. method:: emit(record) 4567db96d56Sopenharmony_ci 4577db96d56Sopenharmony_ci Outputs the record to the file, catering for rollover as described above. 4587db96d56Sopenharmony_ci 4597db96d56Sopenharmony_ci .. method:: getFilesToDelete() 4607db96d56Sopenharmony_ci 4617db96d56Sopenharmony_ci Returns a list of filenames which should be deleted as part of rollover. These 4627db96d56Sopenharmony_ci are the absolute paths of the oldest backup log files written by the handler. 4637db96d56Sopenharmony_ci 4647db96d56Sopenharmony_ci.. _socket-handler: 4657db96d56Sopenharmony_ci 4667db96d56Sopenharmony_ciSocketHandler 4677db96d56Sopenharmony_ci^^^^^^^^^^^^^ 4687db96d56Sopenharmony_ci 4697db96d56Sopenharmony_ciThe :class:`SocketHandler` class, located in the :mod:`logging.handlers` module, 4707db96d56Sopenharmony_cisends logging output to a network socket. The base class uses a TCP socket. 4717db96d56Sopenharmony_ci 4727db96d56Sopenharmony_ci 4737db96d56Sopenharmony_ci.. class:: SocketHandler(host, port) 4747db96d56Sopenharmony_ci 4757db96d56Sopenharmony_ci Returns a new instance of the :class:`SocketHandler` class intended to 4767db96d56Sopenharmony_ci communicate with a remote machine whose address is given by *host* and *port*. 4777db96d56Sopenharmony_ci 4787db96d56Sopenharmony_ci .. versionchanged:: 3.4 4797db96d56Sopenharmony_ci If ``port`` is specified as ``None``, a Unix domain socket is created 4807db96d56Sopenharmony_ci using the value in ``host`` - otherwise, a TCP socket is created. 4817db96d56Sopenharmony_ci 4827db96d56Sopenharmony_ci .. method:: close() 4837db96d56Sopenharmony_ci 4847db96d56Sopenharmony_ci Closes the socket. 4857db96d56Sopenharmony_ci 4867db96d56Sopenharmony_ci 4877db96d56Sopenharmony_ci .. method:: emit() 4887db96d56Sopenharmony_ci 4897db96d56Sopenharmony_ci Pickles the record's attribute dictionary and writes it to the socket in 4907db96d56Sopenharmony_ci binary format. If there is an error with the socket, silently drops the 4917db96d56Sopenharmony_ci packet. If the connection was previously lost, re-establishes the 4927db96d56Sopenharmony_ci connection. To unpickle the record at the receiving end into a 4937db96d56Sopenharmony_ci :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord` 4947db96d56Sopenharmony_ci function. 4957db96d56Sopenharmony_ci 4967db96d56Sopenharmony_ci 4977db96d56Sopenharmony_ci .. method:: handleError() 4987db96d56Sopenharmony_ci 4997db96d56Sopenharmony_ci Handles an error which has occurred during :meth:`emit`. The most likely 5007db96d56Sopenharmony_ci cause is a lost connection. Closes the socket so that we can retry on the 5017db96d56Sopenharmony_ci next event. 5027db96d56Sopenharmony_ci 5037db96d56Sopenharmony_ci 5047db96d56Sopenharmony_ci .. method:: makeSocket() 5057db96d56Sopenharmony_ci 5067db96d56Sopenharmony_ci This is a factory method which allows subclasses to define the precise 5077db96d56Sopenharmony_ci type of socket they want. The default implementation creates a TCP socket 5087db96d56Sopenharmony_ci (:const:`socket.SOCK_STREAM`). 5097db96d56Sopenharmony_ci 5107db96d56Sopenharmony_ci 5117db96d56Sopenharmony_ci .. method:: makePickle(record) 5127db96d56Sopenharmony_ci 5137db96d56Sopenharmony_ci Pickles the record's attribute dictionary in binary format with a length 5147db96d56Sopenharmony_ci prefix, and returns it ready for transmission across the socket. The 5157db96d56Sopenharmony_ci details of this operation are equivalent to:: 5167db96d56Sopenharmony_ci 5177db96d56Sopenharmony_ci data = pickle.dumps(record_attr_dict, 1) 5187db96d56Sopenharmony_ci datalen = struct.pack('>L', len(data)) 5197db96d56Sopenharmony_ci return datalen + data 5207db96d56Sopenharmony_ci 5217db96d56Sopenharmony_ci Note that pickles aren't completely secure. If you are concerned about 5227db96d56Sopenharmony_ci security, you may want to override this method to implement a more secure 5237db96d56Sopenharmony_ci mechanism. For example, you can sign pickles using HMAC and then verify 5247db96d56Sopenharmony_ci them on the receiving end, or alternatively you can disable unpickling of 5257db96d56Sopenharmony_ci global objects on the receiving end. 5267db96d56Sopenharmony_ci 5277db96d56Sopenharmony_ci 5287db96d56Sopenharmony_ci .. method:: send(packet) 5297db96d56Sopenharmony_ci 5307db96d56Sopenharmony_ci Send a pickled byte-string *packet* to the socket. The format of the sent 5317db96d56Sopenharmony_ci byte-string is as described in the documentation for 5327db96d56Sopenharmony_ci :meth:`~SocketHandler.makePickle`. 5337db96d56Sopenharmony_ci 5347db96d56Sopenharmony_ci This function allows for partial sends, which can happen when the network 5357db96d56Sopenharmony_ci is busy. 5367db96d56Sopenharmony_ci 5377db96d56Sopenharmony_ci 5387db96d56Sopenharmony_ci .. method:: createSocket() 5397db96d56Sopenharmony_ci 5407db96d56Sopenharmony_ci Tries to create a socket; on failure, uses an exponential back-off 5417db96d56Sopenharmony_ci algorithm. On initial failure, the handler will drop the message it was 5427db96d56Sopenharmony_ci trying to send. When subsequent messages are handled by the same 5437db96d56Sopenharmony_ci instance, it will not try connecting until some time has passed. The 5447db96d56Sopenharmony_ci default parameters are such that the initial delay is one second, and if 5457db96d56Sopenharmony_ci after that delay the connection still can't be made, the handler will 5467db96d56Sopenharmony_ci double the delay each time up to a maximum of 30 seconds. 5477db96d56Sopenharmony_ci 5487db96d56Sopenharmony_ci This behaviour is controlled by the following handler attributes: 5497db96d56Sopenharmony_ci 5507db96d56Sopenharmony_ci * ``retryStart`` (initial delay, defaulting to 1.0 seconds). 5517db96d56Sopenharmony_ci * ``retryFactor`` (multiplier, defaulting to 2.0). 5527db96d56Sopenharmony_ci * ``retryMax`` (maximum delay, defaulting to 30.0 seconds). 5537db96d56Sopenharmony_ci 5547db96d56Sopenharmony_ci This means that if the remote listener starts up *after* the handler has 5557db96d56Sopenharmony_ci been used, you could lose messages (since the handler won't even attempt 5567db96d56Sopenharmony_ci a connection until the delay has elapsed, but just silently drop messages 5577db96d56Sopenharmony_ci during the delay period). 5587db96d56Sopenharmony_ci 5597db96d56Sopenharmony_ci 5607db96d56Sopenharmony_ci.. _datagram-handler: 5617db96d56Sopenharmony_ci 5627db96d56Sopenharmony_ciDatagramHandler 5637db96d56Sopenharmony_ci^^^^^^^^^^^^^^^ 5647db96d56Sopenharmony_ci 5657db96d56Sopenharmony_ciThe :class:`DatagramHandler` class, located in the :mod:`logging.handlers` 5667db96d56Sopenharmony_cimodule, inherits from :class:`SocketHandler` to support sending logging messages 5677db96d56Sopenharmony_ciover UDP sockets. 5687db96d56Sopenharmony_ci 5697db96d56Sopenharmony_ci 5707db96d56Sopenharmony_ci.. class:: DatagramHandler(host, port) 5717db96d56Sopenharmony_ci 5727db96d56Sopenharmony_ci Returns a new instance of the :class:`DatagramHandler` class intended to 5737db96d56Sopenharmony_ci communicate with a remote machine whose address is given by *host* and *port*. 5747db96d56Sopenharmony_ci 5757db96d56Sopenharmony_ci .. note:: As UDP is not a streaming protocol, there is no persistent connection 5767db96d56Sopenharmony_ci between an instance of this handler and *host*. For this reason, when using a 5777db96d56Sopenharmony_ci network socket, a DNS lookup might have to be made each time an event is 5787db96d56Sopenharmony_ci logged, which can introduce some latency into the system. If this affects you, 5797db96d56Sopenharmony_ci you can do a lookup yourself and initialize this handler using the looked-up IP 5807db96d56Sopenharmony_ci address rather than the hostname. 5817db96d56Sopenharmony_ci 5827db96d56Sopenharmony_ci .. versionchanged:: 3.4 5837db96d56Sopenharmony_ci If ``port`` is specified as ``None``, a Unix domain socket is created 5847db96d56Sopenharmony_ci using the value in ``host`` - otherwise, a UDP socket is created. 5857db96d56Sopenharmony_ci 5867db96d56Sopenharmony_ci .. method:: emit() 5877db96d56Sopenharmony_ci 5887db96d56Sopenharmony_ci Pickles the record's attribute dictionary and writes it to the socket in 5897db96d56Sopenharmony_ci binary format. If there is an error with the socket, silently drops the 5907db96d56Sopenharmony_ci packet. To unpickle the record at the receiving end into a 5917db96d56Sopenharmony_ci :class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord` 5927db96d56Sopenharmony_ci function. 5937db96d56Sopenharmony_ci 5947db96d56Sopenharmony_ci 5957db96d56Sopenharmony_ci .. method:: makeSocket() 5967db96d56Sopenharmony_ci 5977db96d56Sopenharmony_ci The factory method of :class:`SocketHandler` is here overridden to create 5987db96d56Sopenharmony_ci a UDP socket (:const:`socket.SOCK_DGRAM`). 5997db96d56Sopenharmony_ci 6007db96d56Sopenharmony_ci 6017db96d56Sopenharmony_ci .. method:: send(s) 6027db96d56Sopenharmony_ci 6037db96d56Sopenharmony_ci Send a pickled byte-string to a socket. The format of the sent byte-string 6047db96d56Sopenharmony_ci is as described in the documentation for :meth:`SocketHandler.makePickle`. 6057db96d56Sopenharmony_ci 6067db96d56Sopenharmony_ci 6077db96d56Sopenharmony_ci.. _syslog-handler: 6087db96d56Sopenharmony_ci 6097db96d56Sopenharmony_ciSysLogHandler 6107db96d56Sopenharmony_ci^^^^^^^^^^^^^ 6117db96d56Sopenharmony_ci 6127db96d56Sopenharmony_ciThe :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module, 6137db96d56Sopenharmony_cisupports sending logging messages to a remote or local Unix syslog. 6147db96d56Sopenharmony_ci 6157db96d56Sopenharmony_ci 6167db96d56Sopenharmony_ci.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM) 6177db96d56Sopenharmony_ci 6187db96d56Sopenharmony_ci Returns a new instance of the :class:`SysLogHandler` class intended to 6197db96d56Sopenharmony_ci communicate with a remote Unix machine whose address is given by *address* in 6207db96d56Sopenharmony_ci the form of a ``(host, port)`` tuple. If *address* is not specified, 6217db96d56Sopenharmony_ci ``('localhost', 514)`` is used. The address is used to open a socket. An 6227db96d56Sopenharmony_ci alternative to providing a ``(host, port)`` tuple is providing an address as a 6237db96d56Sopenharmony_ci string, for example '/dev/log'. In this case, a Unix domain socket is used to 6247db96d56Sopenharmony_ci send the message to the syslog. If *facility* is not specified, 6257db96d56Sopenharmony_ci :const:`LOG_USER` is used. The type of socket opened depends on the 6267db96d56Sopenharmony_ci *socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus 6277db96d56Sopenharmony_ci opens a UDP socket. To open a TCP socket (for use with the newer syslog 6287db96d56Sopenharmony_ci daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`. 6297db96d56Sopenharmony_ci 6307db96d56Sopenharmony_ci Note that if your server is not listening on UDP port 514, 6317db96d56Sopenharmony_ci :class:`SysLogHandler` may appear not to work. In that case, check what 6327db96d56Sopenharmony_ci address you should be using for a domain socket - it's system dependent. 6337db96d56Sopenharmony_ci For example, on Linux it's usually '/dev/log' but on OS/X it's 6347db96d56Sopenharmony_ci '/var/run/syslog'. You'll need to check your platform and use the 6357db96d56Sopenharmony_ci appropriate address (you may need to do this check at runtime if your 6367db96d56Sopenharmony_ci application needs to run on several platforms). On Windows, you pretty 6377db96d56Sopenharmony_ci much have to use the UDP option. 6387db96d56Sopenharmony_ci 6397db96d56Sopenharmony_ci .. note:: On macOS 12.x (Monterey), Apple has changed the behaviour of their 6407db96d56Sopenharmony_ci syslog daemon - it no longer listens on a domain socket. Therefore, you cannot 6417db96d56Sopenharmony_ci expect :class:`SysLogHandler` to work on this system. 6427db96d56Sopenharmony_ci 6437db96d56Sopenharmony_ci See :gh:`91070` for more information. 6447db96d56Sopenharmony_ci 6457db96d56Sopenharmony_ci .. versionchanged:: 3.2 6467db96d56Sopenharmony_ci *socktype* was added. 6477db96d56Sopenharmony_ci 6487db96d56Sopenharmony_ci 6497db96d56Sopenharmony_ci .. method:: close() 6507db96d56Sopenharmony_ci 6517db96d56Sopenharmony_ci Closes the socket to the remote host. 6527db96d56Sopenharmony_ci 6537db96d56Sopenharmony_ci .. method:: createSocket() 6547db96d56Sopenharmony_ci 6557db96d56Sopenharmony_ci Tries to create a socket and, if it's not a datagram socket, connect it 6567db96d56Sopenharmony_ci to the other end. This method is called during handler initialization, 6577db96d56Sopenharmony_ci but it's not regarded as an error if the other end isn't listening at 6587db96d56Sopenharmony_ci this point - the method will be called again when emitting an event, if 6597db96d56Sopenharmony_ci but it's not regarded as an error if the other end isn't listening yet 6607db96d56Sopenharmony_ci --- the method will be called again when emitting an event, 6617db96d56Sopenharmony_ci if there is no socket at that point. 6627db96d56Sopenharmony_ci 6637db96d56Sopenharmony_ci .. versionadded:: 3.11 6647db96d56Sopenharmony_ci 6657db96d56Sopenharmony_ci .. method:: emit(record) 6667db96d56Sopenharmony_ci 6677db96d56Sopenharmony_ci The record is formatted, and then sent to the syslog server. If exception 6687db96d56Sopenharmony_ci information is present, it is *not* sent to the server. 6697db96d56Sopenharmony_ci 6707db96d56Sopenharmony_ci .. versionchanged:: 3.2.1 6717db96d56Sopenharmony_ci (See: :issue:`12168`.) In earlier versions, the message sent to the 6727db96d56Sopenharmony_ci syslog daemons was always terminated with a NUL byte, because early 6737db96d56Sopenharmony_ci versions of these daemons expected a NUL terminated message - even 6747db96d56Sopenharmony_ci though it's not in the relevant specification (:rfc:`5424`). More recent 6757db96d56Sopenharmony_ci versions of these daemons don't expect the NUL byte but strip it off 6767db96d56Sopenharmony_ci if it's there, and even more recent daemons (which adhere more closely 6777db96d56Sopenharmony_ci to RFC 5424) pass the NUL byte on as part of the message. 6787db96d56Sopenharmony_ci 6797db96d56Sopenharmony_ci To enable easier handling of syslog messages in the face of all these 6807db96d56Sopenharmony_ci differing daemon behaviours, the appending of the NUL byte has been 6817db96d56Sopenharmony_ci made configurable, through the use of a class-level attribute, 6827db96d56Sopenharmony_ci ``append_nul``. This defaults to ``True`` (preserving the existing 6837db96d56Sopenharmony_ci behaviour) but can be set to ``False`` on a ``SysLogHandler`` instance 6847db96d56Sopenharmony_ci in order for that instance to *not* append the NUL terminator. 6857db96d56Sopenharmony_ci 6867db96d56Sopenharmony_ci .. versionchanged:: 3.3 6877db96d56Sopenharmony_ci (See: :issue:`12419`.) In earlier versions, there was no facility for 6887db96d56Sopenharmony_ci an "ident" or "tag" prefix to identify the source of the message. This 6897db96d56Sopenharmony_ci can now be specified using a class-level attribute, defaulting to 6907db96d56Sopenharmony_ci ``""`` to preserve existing behaviour, but which can be overridden on 6917db96d56Sopenharmony_ci a ``SysLogHandler`` instance in order for that instance to prepend 6927db96d56Sopenharmony_ci the ident to every message handled. Note that the provided ident must 6937db96d56Sopenharmony_ci be text, not bytes, and is prepended to the message exactly as is. 6947db96d56Sopenharmony_ci 6957db96d56Sopenharmony_ci .. method:: encodePriority(facility, priority) 6967db96d56Sopenharmony_ci 6977db96d56Sopenharmony_ci Encodes the facility and priority into an integer. You can pass in strings 6987db96d56Sopenharmony_ci or integers - if strings are passed, internal mapping dictionaries are 6997db96d56Sopenharmony_ci used to convert them to integers. 7007db96d56Sopenharmony_ci 7017db96d56Sopenharmony_ci The symbolic ``LOG_`` values are defined in :class:`SysLogHandler` and 7027db96d56Sopenharmony_ci mirror the values defined in the ``sys/syslog.h`` header file. 7037db96d56Sopenharmony_ci 7047db96d56Sopenharmony_ci **Priorities** 7057db96d56Sopenharmony_ci 7067db96d56Sopenharmony_ci +--------------------------+---------------+ 7077db96d56Sopenharmony_ci | Name (string) | Symbolic value| 7087db96d56Sopenharmony_ci +==========================+===============+ 7097db96d56Sopenharmony_ci | ``alert`` | LOG_ALERT | 7107db96d56Sopenharmony_ci +--------------------------+---------------+ 7117db96d56Sopenharmony_ci | ``crit`` or ``critical`` | LOG_CRIT | 7127db96d56Sopenharmony_ci +--------------------------+---------------+ 7137db96d56Sopenharmony_ci | ``debug`` | LOG_DEBUG | 7147db96d56Sopenharmony_ci +--------------------------+---------------+ 7157db96d56Sopenharmony_ci | ``emerg`` or ``panic`` | LOG_EMERG | 7167db96d56Sopenharmony_ci +--------------------------+---------------+ 7177db96d56Sopenharmony_ci | ``err`` or ``error`` | LOG_ERR | 7187db96d56Sopenharmony_ci +--------------------------+---------------+ 7197db96d56Sopenharmony_ci | ``info`` | LOG_INFO | 7207db96d56Sopenharmony_ci +--------------------------+---------------+ 7217db96d56Sopenharmony_ci | ``notice`` | LOG_NOTICE | 7227db96d56Sopenharmony_ci +--------------------------+---------------+ 7237db96d56Sopenharmony_ci | ``warn`` or ``warning`` | LOG_WARNING | 7247db96d56Sopenharmony_ci +--------------------------+---------------+ 7257db96d56Sopenharmony_ci 7267db96d56Sopenharmony_ci **Facilities** 7277db96d56Sopenharmony_ci 7287db96d56Sopenharmony_ci +---------------+---------------+ 7297db96d56Sopenharmony_ci | Name (string) | Symbolic value| 7307db96d56Sopenharmony_ci +===============+===============+ 7317db96d56Sopenharmony_ci | ``auth`` | LOG_AUTH | 7327db96d56Sopenharmony_ci +---------------+---------------+ 7337db96d56Sopenharmony_ci | ``authpriv`` | LOG_AUTHPRIV | 7347db96d56Sopenharmony_ci +---------------+---------------+ 7357db96d56Sopenharmony_ci | ``cron`` | LOG_CRON | 7367db96d56Sopenharmony_ci +---------------+---------------+ 7377db96d56Sopenharmony_ci | ``daemon`` | LOG_DAEMON | 7387db96d56Sopenharmony_ci +---------------+---------------+ 7397db96d56Sopenharmony_ci | ``ftp`` | LOG_FTP | 7407db96d56Sopenharmony_ci +---------------+---------------+ 7417db96d56Sopenharmony_ci | ``kern`` | LOG_KERN | 7427db96d56Sopenharmony_ci +---------------+---------------+ 7437db96d56Sopenharmony_ci | ``lpr`` | LOG_LPR | 7447db96d56Sopenharmony_ci +---------------+---------------+ 7457db96d56Sopenharmony_ci | ``mail`` | LOG_MAIL | 7467db96d56Sopenharmony_ci +---------------+---------------+ 7477db96d56Sopenharmony_ci | ``news`` | LOG_NEWS | 7487db96d56Sopenharmony_ci +---------------+---------------+ 7497db96d56Sopenharmony_ci | ``syslog`` | LOG_SYSLOG | 7507db96d56Sopenharmony_ci +---------------+---------------+ 7517db96d56Sopenharmony_ci | ``user`` | LOG_USER | 7527db96d56Sopenharmony_ci +---------------+---------------+ 7537db96d56Sopenharmony_ci | ``uucp`` | LOG_UUCP | 7547db96d56Sopenharmony_ci +---------------+---------------+ 7557db96d56Sopenharmony_ci | ``local0`` | LOG_LOCAL0 | 7567db96d56Sopenharmony_ci +---------------+---------------+ 7577db96d56Sopenharmony_ci | ``local1`` | LOG_LOCAL1 | 7587db96d56Sopenharmony_ci +---------------+---------------+ 7597db96d56Sopenharmony_ci | ``local2`` | LOG_LOCAL2 | 7607db96d56Sopenharmony_ci +---------------+---------------+ 7617db96d56Sopenharmony_ci | ``local3`` | LOG_LOCAL3 | 7627db96d56Sopenharmony_ci +---------------+---------------+ 7637db96d56Sopenharmony_ci | ``local4`` | LOG_LOCAL4 | 7647db96d56Sopenharmony_ci +---------------+---------------+ 7657db96d56Sopenharmony_ci | ``local5`` | LOG_LOCAL5 | 7667db96d56Sopenharmony_ci +---------------+---------------+ 7677db96d56Sopenharmony_ci | ``local6`` | LOG_LOCAL6 | 7687db96d56Sopenharmony_ci +---------------+---------------+ 7697db96d56Sopenharmony_ci | ``local7`` | LOG_LOCAL7 | 7707db96d56Sopenharmony_ci +---------------+---------------+ 7717db96d56Sopenharmony_ci 7727db96d56Sopenharmony_ci .. method:: mapPriority(levelname) 7737db96d56Sopenharmony_ci 7747db96d56Sopenharmony_ci Maps a logging level name to a syslog priority name. 7757db96d56Sopenharmony_ci You may need to override this if you are using custom levels, or 7767db96d56Sopenharmony_ci if the default algorithm is not suitable for your needs. The 7777db96d56Sopenharmony_ci default algorithm maps ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR`` and 7787db96d56Sopenharmony_ci ``CRITICAL`` to the equivalent syslog names, and all other level 7797db96d56Sopenharmony_ci names to 'warning'. 7807db96d56Sopenharmony_ci 7817db96d56Sopenharmony_ci.. _nt-eventlog-handler: 7827db96d56Sopenharmony_ci 7837db96d56Sopenharmony_ciNTEventLogHandler 7847db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^ 7857db96d56Sopenharmony_ci 7867db96d56Sopenharmony_ciThe :class:`NTEventLogHandler` class, located in the :mod:`logging.handlers` 7877db96d56Sopenharmony_cimodule, supports sending logging messages to a local Windows NT, Windows 2000 or 7887db96d56Sopenharmony_ciWindows XP event log. Before you can use it, you need Mark Hammond's Win32 7897db96d56Sopenharmony_ciextensions for Python installed. 7907db96d56Sopenharmony_ci 7917db96d56Sopenharmony_ci 7927db96d56Sopenharmony_ci.. class:: NTEventLogHandler(appname, dllname=None, logtype='Application') 7937db96d56Sopenharmony_ci 7947db96d56Sopenharmony_ci Returns a new instance of the :class:`NTEventLogHandler` class. The *appname* is 7957db96d56Sopenharmony_ci used to define the application name as it appears in the event log. An 7967db96d56Sopenharmony_ci appropriate registry entry is created using this name. The *dllname* should give 7977db96d56Sopenharmony_ci the fully qualified pathname of a .dll or .exe which contains message 7987db96d56Sopenharmony_ci definitions to hold in the log (if not specified, ``'win32service.pyd'`` is used 7997db96d56Sopenharmony_ci - this is installed with the Win32 extensions and contains some basic 8007db96d56Sopenharmony_ci placeholder message definitions. Note that use of these placeholders will make 8017db96d56Sopenharmony_ci your event logs big, as the entire message source is held in the log. If you 8027db96d56Sopenharmony_ci want slimmer logs, you have to pass in the name of your own .dll or .exe which 8037db96d56Sopenharmony_ci contains the message definitions you want to use in the event log). The 8047db96d56Sopenharmony_ci *logtype* is one of ``'Application'``, ``'System'`` or ``'Security'``, and 8057db96d56Sopenharmony_ci defaults to ``'Application'``. 8067db96d56Sopenharmony_ci 8077db96d56Sopenharmony_ci 8087db96d56Sopenharmony_ci .. method:: close() 8097db96d56Sopenharmony_ci 8107db96d56Sopenharmony_ci At this point, you can remove the application name from the registry as a 8117db96d56Sopenharmony_ci source of event log entries. However, if you do this, you will not be able 8127db96d56Sopenharmony_ci to see the events as you intended in the Event Log Viewer - it needs to be 8137db96d56Sopenharmony_ci able to access the registry to get the .dll name. The current version does 8147db96d56Sopenharmony_ci not do this. 8157db96d56Sopenharmony_ci 8167db96d56Sopenharmony_ci 8177db96d56Sopenharmony_ci .. method:: emit(record) 8187db96d56Sopenharmony_ci 8197db96d56Sopenharmony_ci Determines the message ID, event category and event type, and then logs 8207db96d56Sopenharmony_ci the message in the NT event log. 8217db96d56Sopenharmony_ci 8227db96d56Sopenharmony_ci 8237db96d56Sopenharmony_ci .. method:: getEventCategory(record) 8247db96d56Sopenharmony_ci 8257db96d56Sopenharmony_ci Returns the event category for the record. Override this if you want to 8267db96d56Sopenharmony_ci specify your own categories. This version returns 0. 8277db96d56Sopenharmony_ci 8287db96d56Sopenharmony_ci 8297db96d56Sopenharmony_ci .. method:: getEventType(record) 8307db96d56Sopenharmony_ci 8317db96d56Sopenharmony_ci Returns the event type for the record. Override this if you want to 8327db96d56Sopenharmony_ci specify your own types. This version does a mapping using the handler's 8337db96d56Sopenharmony_ci typemap attribute, which is set up in :meth:`__init__` to a dictionary 8347db96d56Sopenharmony_ci which contains mappings for :const:`DEBUG`, :const:`INFO`, 8357db96d56Sopenharmony_ci :const:`WARNING`, :const:`ERROR` and :const:`CRITICAL`. If you are using 8367db96d56Sopenharmony_ci your own levels, you will either need to override this method or place a 8377db96d56Sopenharmony_ci suitable dictionary in the handler's *typemap* attribute. 8387db96d56Sopenharmony_ci 8397db96d56Sopenharmony_ci 8407db96d56Sopenharmony_ci .. method:: getMessageID(record) 8417db96d56Sopenharmony_ci 8427db96d56Sopenharmony_ci Returns the message ID for the record. If you are using your own messages, 8437db96d56Sopenharmony_ci you could do this by having the *msg* passed to the logger being an ID 8447db96d56Sopenharmony_ci rather than a format string. Then, in here, you could use a dictionary 8457db96d56Sopenharmony_ci lookup to get the message ID. This version returns 1, which is the base 8467db96d56Sopenharmony_ci message ID in :file:`win32service.pyd`. 8477db96d56Sopenharmony_ci 8487db96d56Sopenharmony_ci.. _smtp-handler: 8497db96d56Sopenharmony_ci 8507db96d56Sopenharmony_ciSMTPHandler 8517db96d56Sopenharmony_ci^^^^^^^^^^^ 8527db96d56Sopenharmony_ci 8537db96d56Sopenharmony_ciThe :class:`SMTPHandler` class, located in the :mod:`logging.handlers` module, 8547db96d56Sopenharmony_cisupports sending logging messages to an email address via SMTP. 8557db96d56Sopenharmony_ci 8567db96d56Sopenharmony_ci 8577db96d56Sopenharmony_ci.. class:: SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0) 8587db96d56Sopenharmony_ci 8597db96d56Sopenharmony_ci Returns a new instance of the :class:`SMTPHandler` class. The instance is 8607db96d56Sopenharmony_ci initialized with the from and to addresses and subject line of the email. The 8617db96d56Sopenharmony_ci *toaddrs* should be a list of strings. To specify a non-standard SMTP port, use 8627db96d56Sopenharmony_ci the (host, port) tuple format for the *mailhost* argument. If you use a string, 8637db96d56Sopenharmony_ci the standard SMTP port is used. If your SMTP server requires authentication, you 8647db96d56Sopenharmony_ci can specify a (username, password) tuple for the *credentials* argument. 8657db96d56Sopenharmony_ci 8667db96d56Sopenharmony_ci To specify the use of a secure protocol (TLS), pass in a tuple to the 8677db96d56Sopenharmony_ci *secure* argument. This will only be used when authentication credentials are 8687db96d56Sopenharmony_ci supplied. The tuple should be either an empty tuple, or a single-value tuple 8697db96d56Sopenharmony_ci with the name of a keyfile, or a 2-value tuple with the names of the keyfile 8707db96d56Sopenharmony_ci and certificate file. (This tuple is passed to the 8717db96d56Sopenharmony_ci :meth:`smtplib.SMTP.starttls` method.) 8727db96d56Sopenharmony_ci 8737db96d56Sopenharmony_ci A timeout can be specified for communication with the SMTP server using the 8747db96d56Sopenharmony_ci *timeout* argument. 8757db96d56Sopenharmony_ci 8767db96d56Sopenharmony_ci .. versionadded:: 3.3 8777db96d56Sopenharmony_ci The *timeout* argument was added. 8787db96d56Sopenharmony_ci 8797db96d56Sopenharmony_ci .. method:: emit(record) 8807db96d56Sopenharmony_ci 8817db96d56Sopenharmony_ci Formats the record and sends it to the specified addressees. 8827db96d56Sopenharmony_ci 8837db96d56Sopenharmony_ci 8847db96d56Sopenharmony_ci .. method:: getSubject(record) 8857db96d56Sopenharmony_ci 8867db96d56Sopenharmony_ci If you want to specify a subject line which is record-dependent, override 8877db96d56Sopenharmony_ci this method. 8887db96d56Sopenharmony_ci 8897db96d56Sopenharmony_ci.. _memory-handler: 8907db96d56Sopenharmony_ci 8917db96d56Sopenharmony_ciMemoryHandler 8927db96d56Sopenharmony_ci^^^^^^^^^^^^^ 8937db96d56Sopenharmony_ci 8947db96d56Sopenharmony_ciThe :class:`MemoryHandler` class, located in the :mod:`logging.handlers` module, 8957db96d56Sopenharmony_cisupports buffering of logging records in memory, periodically flushing them to a 8967db96d56Sopenharmony_ci:dfn:`target` handler. Flushing occurs whenever the buffer is full, or when an 8977db96d56Sopenharmony_cievent of a certain severity or greater is seen. 8987db96d56Sopenharmony_ci 8997db96d56Sopenharmony_ci:class:`MemoryHandler` is a subclass of the more general 9007db96d56Sopenharmony_ci:class:`BufferingHandler`, which is an abstract class. This buffers logging 9017db96d56Sopenharmony_cirecords in memory. Whenever each record is added to the buffer, a check is made 9027db96d56Sopenharmony_ciby calling :meth:`shouldFlush` to see if the buffer should be flushed. If it 9037db96d56Sopenharmony_cishould, then :meth:`flush` is expected to do the flushing. 9047db96d56Sopenharmony_ci 9057db96d56Sopenharmony_ci 9067db96d56Sopenharmony_ci.. class:: BufferingHandler(capacity) 9077db96d56Sopenharmony_ci 9087db96d56Sopenharmony_ci Initializes the handler with a buffer of the specified capacity. Here, 9097db96d56Sopenharmony_ci *capacity* means the number of logging records buffered. 9107db96d56Sopenharmony_ci 9117db96d56Sopenharmony_ci 9127db96d56Sopenharmony_ci .. method:: emit(record) 9137db96d56Sopenharmony_ci 9147db96d56Sopenharmony_ci Append the record to the buffer. If :meth:`shouldFlush` returns true, 9157db96d56Sopenharmony_ci call :meth:`flush` to process the buffer. 9167db96d56Sopenharmony_ci 9177db96d56Sopenharmony_ci 9187db96d56Sopenharmony_ci .. method:: flush() 9197db96d56Sopenharmony_ci 9207db96d56Sopenharmony_ci You can override this to implement custom flushing behavior. This version 9217db96d56Sopenharmony_ci just zaps the buffer to empty. 9227db96d56Sopenharmony_ci 9237db96d56Sopenharmony_ci 9247db96d56Sopenharmony_ci .. method:: shouldFlush(record) 9257db96d56Sopenharmony_ci 9267db96d56Sopenharmony_ci Return ``True`` if the buffer is up to capacity. This method can be 9277db96d56Sopenharmony_ci overridden to implement custom flushing strategies. 9287db96d56Sopenharmony_ci 9297db96d56Sopenharmony_ci 9307db96d56Sopenharmony_ci.. class:: MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True) 9317db96d56Sopenharmony_ci 9327db96d56Sopenharmony_ci Returns a new instance of the :class:`MemoryHandler` class. The instance is 9337db96d56Sopenharmony_ci initialized with a buffer size of *capacity* (number of records buffered). 9347db96d56Sopenharmony_ci If *flushLevel* is not specified, :const:`ERROR` is used. If no *target* is 9357db96d56Sopenharmony_ci specified, the target will need to be set using :meth:`setTarget` before this 9367db96d56Sopenharmony_ci handler does anything useful. If *flushOnClose* is specified as ``False``, 9377db96d56Sopenharmony_ci then the buffer is *not* flushed when the handler is closed. If not specified 9387db96d56Sopenharmony_ci or specified as ``True``, the previous behaviour of flushing the buffer will 9397db96d56Sopenharmony_ci occur when the handler is closed. 9407db96d56Sopenharmony_ci 9417db96d56Sopenharmony_ci .. versionchanged:: 3.6 9427db96d56Sopenharmony_ci The *flushOnClose* parameter was added. 9437db96d56Sopenharmony_ci 9447db96d56Sopenharmony_ci 9457db96d56Sopenharmony_ci .. method:: close() 9467db96d56Sopenharmony_ci 9477db96d56Sopenharmony_ci Calls :meth:`flush`, sets the target to ``None`` and clears the 9487db96d56Sopenharmony_ci buffer. 9497db96d56Sopenharmony_ci 9507db96d56Sopenharmony_ci 9517db96d56Sopenharmony_ci .. method:: flush() 9527db96d56Sopenharmony_ci 9537db96d56Sopenharmony_ci For a :class:`MemoryHandler`, flushing means just sending the buffered 9547db96d56Sopenharmony_ci records to the target, if there is one. The buffer is also cleared when 9557db96d56Sopenharmony_ci this happens. Override if you want different behavior. 9567db96d56Sopenharmony_ci 9577db96d56Sopenharmony_ci 9587db96d56Sopenharmony_ci .. method:: setTarget(target) 9597db96d56Sopenharmony_ci 9607db96d56Sopenharmony_ci Sets the target handler for this handler. 9617db96d56Sopenharmony_ci 9627db96d56Sopenharmony_ci 9637db96d56Sopenharmony_ci .. method:: shouldFlush(record) 9647db96d56Sopenharmony_ci 9657db96d56Sopenharmony_ci Checks for buffer full or a record at the *flushLevel* or higher. 9667db96d56Sopenharmony_ci 9677db96d56Sopenharmony_ci 9687db96d56Sopenharmony_ci.. _http-handler: 9697db96d56Sopenharmony_ci 9707db96d56Sopenharmony_ciHTTPHandler 9717db96d56Sopenharmony_ci^^^^^^^^^^^ 9727db96d56Sopenharmony_ci 9737db96d56Sopenharmony_ciThe :class:`HTTPHandler` class, located in the :mod:`logging.handlers` module, 9747db96d56Sopenharmony_cisupports sending logging messages to a web server, using either ``GET`` or 9757db96d56Sopenharmony_ci``POST`` semantics. 9767db96d56Sopenharmony_ci 9777db96d56Sopenharmony_ci 9787db96d56Sopenharmony_ci.. class:: HTTPHandler(host, url, method='GET', secure=False, credentials=None, context=None) 9797db96d56Sopenharmony_ci 9807db96d56Sopenharmony_ci Returns a new instance of the :class:`HTTPHandler` class. The *host* can be 9817db96d56Sopenharmony_ci of the form ``host:port``, should you need to use a specific port number. If 9827db96d56Sopenharmony_ci no *method* is specified, ``GET`` is used. If *secure* is true, a HTTPS 9837db96d56Sopenharmony_ci connection will be used. The *context* parameter may be set to a 9847db96d56Sopenharmony_ci :class:`ssl.SSLContext` instance to configure the SSL settings used for the 9857db96d56Sopenharmony_ci HTTPS connection. If *credentials* is specified, it should be a 2-tuple 9867db96d56Sopenharmony_ci consisting of userid and password, which will be placed in a HTTP 9877db96d56Sopenharmony_ci 'Authorization' header using Basic authentication. If you specify 9887db96d56Sopenharmony_ci credentials, you should also specify secure=True so that your userid and 9897db96d56Sopenharmony_ci password are not passed in cleartext across the wire. 9907db96d56Sopenharmony_ci 9917db96d56Sopenharmony_ci .. versionchanged:: 3.5 9927db96d56Sopenharmony_ci The *context* parameter was added. 9937db96d56Sopenharmony_ci 9947db96d56Sopenharmony_ci .. method:: mapLogRecord(record) 9957db96d56Sopenharmony_ci 9967db96d56Sopenharmony_ci Provides a dictionary, based on ``record``, which is to be URL-encoded 9977db96d56Sopenharmony_ci and sent to the web server. The default implementation just returns 9987db96d56Sopenharmony_ci ``record.__dict__``. This method can be overridden if e.g. only a 9997db96d56Sopenharmony_ci subset of :class:`~logging.LogRecord` is to be sent to the web server, or 10007db96d56Sopenharmony_ci if more specific customization of what's sent to the server is required. 10017db96d56Sopenharmony_ci 10027db96d56Sopenharmony_ci .. method:: emit(record) 10037db96d56Sopenharmony_ci 10047db96d56Sopenharmony_ci Sends the record to the web server as a URL-encoded dictionary. The 10057db96d56Sopenharmony_ci :meth:`mapLogRecord` method is used to convert the record to the 10067db96d56Sopenharmony_ci dictionary to be sent. 10077db96d56Sopenharmony_ci 10087db96d56Sopenharmony_ci .. note:: Since preparing a record for sending it to a web server is not 10097db96d56Sopenharmony_ci the same as a generic formatting operation, using 10107db96d56Sopenharmony_ci :meth:`~logging.Handler.setFormatter` to specify a 10117db96d56Sopenharmony_ci :class:`~logging.Formatter` for a :class:`HTTPHandler` has no effect. 10127db96d56Sopenharmony_ci Instead of calling :meth:`~logging.Handler.format`, this handler calls 10137db96d56Sopenharmony_ci :meth:`mapLogRecord` and then :func:`urllib.parse.urlencode` to encode the 10147db96d56Sopenharmony_ci dictionary in a form suitable for sending to a web server. 10157db96d56Sopenharmony_ci 10167db96d56Sopenharmony_ci 10177db96d56Sopenharmony_ci.. _queue-handler: 10187db96d56Sopenharmony_ci 10197db96d56Sopenharmony_ci 10207db96d56Sopenharmony_ciQueueHandler 10217db96d56Sopenharmony_ci^^^^^^^^^^^^ 10227db96d56Sopenharmony_ci 10237db96d56Sopenharmony_ci.. versionadded:: 3.2 10247db96d56Sopenharmony_ci 10257db96d56Sopenharmony_ciThe :class:`QueueHandler` class, located in the :mod:`logging.handlers` module, 10267db96d56Sopenharmony_cisupports sending logging messages to a queue, such as those implemented in the 10277db96d56Sopenharmony_ci:mod:`queue` or :mod:`multiprocessing` modules. 10287db96d56Sopenharmony_ci 10297db96d56Sopenharmony_ciAlong with the :class:`QueueListener` class, :class:`QueueHandler` can be used 10307db96d56Sopenharmony_cito let handlers do their work on a separate thread from the one which does the 10317db96d56Sopenharmony_cilogging. This is important in web applications and also other service 10327db96d56Sopenharmony_ciapplications where threads servicing clients need to respond as quickly as 10337db96d56Sopenharmony_cipossible, while any potentially slow operations (such as sending an email via 10347db96d56Sopenharmony_ci:class:`SMTPHandler`) are done on a separate thread. 10357db96d56Sopenharmony_ci 10367db96d56Sopenharmony_ci.. class:: QueueHandler(queue) 10377db96d56Sopenharmony_ci 10387db96d56Sopenharmony_ci Returns a new instance of the :class:`QueueHandler` class. The instance is 10397db96d56Sopenharmony_ci initialized with the queue to send messages to. The *queue* can be any 10407db96d56Sopenharmony_ci queue-like object; it's used as-is by the :meth:`enqueue` method, which 10417db96d56Sopenharmony_ci needs to know how to send messages to it. The queue is not *required* to 10427db96d56Sopenharmony_ci have the task tracking API, which means that you can use 10437db96d56Sopenharmony_ci :class:`~queue.SimpleQueue` instances for *queue*. 10447db96d56Sopenharmony_ci 10457db96d56Sopenharmony_ci .. note:: If you are using :mod:`multiprocessing`, you should avoid using 10467db96d56Sopenharmony_ci :class:`~queue.SimpleQueue` and instead use :class:`multiprocessing.Queue`. 10477db96d56Sopenharmony_ci 10487db96d56Sopenharmony_ci .. method:: emit(record) 10497db96d56Sopenharmony_ci 10507db96d56Sopenharmony_ci Enqueues the result of preparing the LogRecord. Should an exception 10517db96d56Sopenharmony_ci occur (e.g. because a bounded queue has filled up), the 10527db96d56Sopenharmony_ci :meth:`~logging.Handler.handleError` method is called to handle the 10537db96d56Sopenharmony_ci error. This can result in the record silently being dropped (if 10547db96d56Sopenharmony_ci :attr:`logging.raiseExceptions` is ``False``) or a message printed to 10557db96d56Sopenharmony_ci ``sys.stderr`` (if :attr:`logging.raiseExceptions` is ``True``). 10567db96d56Sopenharmony_ci 10577db96d56Sopenharmony_ci .. method:: prepare(record) 10587db96d56Sopenharmony_ci 10597db96d56Sopenharmony_ci Prepares a record for queuing. The object returned by this 10607db96d56Sopenharmony_ci method is enqueued. 10617db96d56Sopenharmony_ci 10627db96d56Sopenharmony_ci The base implementation formats the record to merge the message, 10637db96d56Sopenharmony_ci arguments, exception and stack information, if present. It also removes 10647db96d56Sopenharmony_ci unpickleable items from the record in-place. Specifically, it overwrites 10657db96d56Sopenharmony_ci the record's :attr:`msg` and :attr:`message` attributes with the merged 10667db96d56Sopenharmony_ci message (obtained by calling the handler's :meth:`format` method), and 10677db96d56Sopenharmony_ci sets the :attr:`args`, :attr:`exc_info` and :attr:`exc_text` attributes 10687db96d56Sopenharmony_ci to ``None``. 10697db96d56Sopenharmony_ci 10707db96d56Sopenharmony_ci You might want to override this method if you want to convert 10717db96d56Sopenharmony_ci the record to a dict or JSON string, or send a modified copy 10727db96d56Sopenharmony_ci of the record while leaving the original intact. 10737db96d56Sopenharmony_ci 10747db96d56Sopenharmony_ci .. note:: The base implementation formats the message with arguments, sets 10757db96d56Sopenharmony_ci the ``message`` and ``msg`` attributes to the formatted message and 10767db96d56Sopenharmony_ci sets the ``args`` and ``exc_text`` attributes to ``None`` to allow 10777db96d56Sopenharmony_ci pickling and to prevent further attempts at formatting. This means 10787db96d56Sopenharmony_ci that a handler on the :class:`QueueListener` side won't have the 10797db96d56Sopenharmony_ci information to do custom formatting, e.g. of exceptions. You may wish 10807db96d56Sopenharmony_ci to subclass ``QueueHandler`` and override this method to e.g. avoid 10817db96d56Sopenharmony_ci setting ``exc_text`` to ``None``. Note that the ``message`` / ``msg`` 10827db96d56Sopenharmony_ci / ``args`` changes are related to ensuring the record is pickleable, 10837db96d56Sopenharmony_ci and you might or might not be able to avoid doing that depending on 10847db96d56Sopenharmony_ci whether your ``args`` are pickleable. (Note that you may have to 10857db96d56Sopenharmony_ci consider not only your own code but also code in any libraries that 10867db96d56Sopenharmony_ci you use.) 10877db96d56Sopenharmony_ci 10887db96d56Sopenharmony_ci .. method:: enqueue(record) 10897db96d56Sopenharmony_ci 10907db96d56Sopenharmony_ci Enqueues the record on the queue using ``put_nowait()``; you may 10917db96d56Sopenharmony_ci want to override this if you want to use blocking behaviour, or a 10927db96d56Sopenharmony_ci timeout, or a customized queue implementation. 10937db96d56Sopenharmony_ci 10947db96d56Sopenharmony_ci 10957db96d56Sopenharmony_ci 10967db96d56Sopenharmony_ci.. _queue-listener: 10977db96d56Sopenharmony_ci 10987db96d56Sopenharmony_ciQueueListener 10997db96d56Sopenharmony_ci^^^^^^^^^^^^^ 11007db96d56Sopenharmony_ci 11017db96d56Sopenharmony_ci.. versionadded:: 3.2 11027db96d56Sopenharmony_ci 11037db96d56Sopenharmony_ciThe :class:`QueueListener` class, located in the :mod:`logging.handlers` 11047db96d56Sopenharmony_cimodule, supports receiving logging messages from a queue, such as those 11057db96d56Sopenharmony_ciimplemented in the :mod:`queue` or :mod:`multiprocessing` modules. The 11067db96d56Sopenharmony_cimessages are received from a queue in an internal thread and passed, on 11077db96d56Sopenharmony_cithe same thread, to one or more handlers for processing. While 11087db96d56Sopenharmony_ci:class:`QueueListener` is not itself a handler, it is documented here 11097db96d56Sopenharmony_cibecause it works hand-in-hand with :class:`QueueHandler`. 11107db96d56Sopenharmony_ci 11117db96d56Sopenharmony_ciAlong with the :class:`QueueHandler` class, :class:`QueueListener` can be used 11127db96d56Sopenharmony_cito let handlers do their work on a separate thread from the one which does the 11137db96d56Sopenharmony_cilogging. This is important in web applications and also other service 11147db96d56Sopenharmony_ciapplications where threads servicing clients need to respond as quickly as 11157db96d56Sopenharmony_cipossible, while any potentially slow operations (such as sending an email via 11167db96d56Sopenharmony_ci:class:`SMTPHandler`) are done on a separate thread. 11177db96d56Sopenharmony_ci 11187db96d56Sopenharmony_ci.. class:: QueueListener(queue, *handlers, respect_handler_level=False) 11197db96d56Sopenharmony_ci 11207db96d56Sopenharmony_ci Returns a new instance of the :class:`QueueListener` class. The instance is 11217db96d56Sopenharmony_ci initialized with the queue to send messages to and a list of handlers which 11227db96d56Sopenharmony_ci will handle entries placed on the queue. The queue can be any queue-like 11237db96d56Sopenharmony_ci object; it's passed as-is to the :meth:`dequeue` method, which needs 11247db96d56Sopenharmony_ci to know how to get messages from it. The queue is not *required* to have the 11257db96d56Sopenharmony_ci task tracking API (though it's used if available), which means that you can 11267db96d56Sopenharmony_ci use :class:`~queue.SimpleQueue` instances for *queue*. 11277db96d56Sopenharmony_ci 11287db96d56Sopenharmony_ci .. note:: If you are using :mod:`multiprocessing`, you should avoid using 11297db96d56Sopenharmony_ci :class:`~queue.SimpleQueue` and instead use :class:`multiprocessing.Queue`. 11307db96d56Sopenharmony_ci 11317db96d56Sopenharmony_ci If ``respect_handler_level`` is ``True``, a handler's level is respected 11327db96d56Sopenharmony_ci (compared with the level for the message) when deciding whether to pass 11337db96d56Sopenharmony_ci messages to that handler; otherwise, the behaviour is as in previous Python 11347db96d56Sopenharmony_ci versions - to always pass each message to each handler. 11357db96d56Sopenharmony_ci 11367db96d56Sopenharmony_ci .. versionchanged:: 3.5 11377db96d56Sopenharmony_ci The ``respect_handler_level`` argument was added. 11387db96d56Sopenharmony_ci 11397db96d56Sopenharmony_ci .. method:: dequeue(block) 11407db96d56Sopenharmony_ci 11417db96d56Sopenharmony_ci Dequeues a record and return it, optionally blocking. 11427db96d56Sopenharmony_ci 11437db96d56Sopenharmony_ci The base implementation uses ``get()``. You may want to override this 11447db96d56Sopenharmony_ci method if you want to use timeouts or work with custom queue 11457db96d56Sopenharmony_ci implementations. 11467db96d56Sopenharmony_ci 11477db96d56Sopenharmony_ci .. method:: prepare(record) 11487db96d56Sopenharmony_ci 11497db96d56Sopenharmony_ci Prepare a record for handling. 11507db96d56Sopenharmony_ci 11517db96d56Sopenharmony_ci This implementation just returns the passed-in record. You may want to 11527db96d56Sopenharmony_ci override this method if you need to do any custom marshalling or 11537db96d56Sopenharmony_ci manipulation of the record before passing it to the handlers. 11547db96d56Sopenharmony_ci 11557db96d56Sopenharmony_ci .. method:: handle(record) 11567db96d56Sopenharmony_ci 11577db96d56Sopenharmony_ci Handle a record. 11587db96d56Sopenharmony_ci 11597db96d56Sopenharmony_ci This just loops through the handlers offering them the record 11607db96d56Sopenharmony_ci to handle. The actual object passed to the handlers is that which 11617db96d56Sopenharmony_ci is returned from :meth:`prepare`. 11627db96d56Sopenharmony_ci 11637db96d56Sopenharmony_ci .. method:: start() 11647db96d56Sopenharmony_ci 11657db96d56Sopenharmony_ci Starts the listener. 11667db96d56Sopenharmony_ci 11677db96d56Sopenharmony_ci This starts up a background thread to monitor the queue for 11687db96d56Sopenharmony_ci LogRecords to process. 11697db96d56Sopenharmony_ci 11707db96d56Sopenharmony_ci .. method:: stop() 11717db96d56Sopenharmony_ci 11727db96d56Sopenharmony_ci Stops the listener. 11737db96d56Sopenharmony_ci 11747db96d56Sopenharmony_ci This asks the thread to terminate, and then waits for it to do so. 11757db96d56Sopenharmony_ci Note that if you don't call this before your application exits, there 11767db96d56Sopenharmony_ci may be some records still left on the queue, which won't be processed. 11777db96d56Sopenharmony_ci 11787db96d56Sopenharmony_ci .. method:: enqueue_sentinel() 11797db96d56Sopenharmony_ci 11807db96d56Sopenharmony_ci Writes a sentinel to the queue to tell the listener to quit. This 11817db96d56Sopenharmony_ci implementation uses ``put_nowait()``. You may want to override this 11827db96d56Sopenharmony_ci method if you want to use timeouts or work with custom queue 11837db96d56Sopenharmony_ci implementations. 11847db96d56Sopenharmony_ci 11857db96d56Sopenharmony_ci .. versionadded:: 3.3 11867db96d56Sopenharmony_ci 11877db96d56Sopenharmony_ci 11887db96d56Sopenharmony_ci.. seealso:: 11897db96d56Sopenharmony_ci 11907db96d56Sopenharmony_ci Module :mod:`logging` 11917db96d56Sopenharmony_ci API reference for the logging module. 11927db96d56Sopenharmony_ci 11937db96d56Sopenharmony_ci Module :mod:`logging.config` 11947db96d56Sopenharmony_ci Configuration API for the logging module. 11957db96d56Sopenharmony_ci 11967db96d56Sopenharmony_ci 1197