17db96d56Sopenharmony_ci:mod:`logging.config` --- Logging configuration
27db96d56Sopenharmony_ci===============================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: logging.config
57db96d56Sopenharmony_ci   :synopsis: Configuration of 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/config.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_ciThis section describes the API for configuring the logging module.
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ci.. _logging-config-api:
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ciConfiguration functions
287db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ciThe following functions configure the logging module. They are located in the
317db96d56Sopenharmony_ci:mod:`logging.config` module.  Their use is optional --- you can configure the
327db96d56Sopenharmony_cilogging module using these functions or by making calls to the main API (defined
337db96d56Sopenharmony_ciin :mod:`logging` itself) and defining handlers which are declared either in
347db96d56Sopenharmony_ci:mod:`logging` or :mod:`logging.handlers`.
357db96d56Sopenharmony_ci
367db96d56Sopenharmony_ci.. function:: dictConfig(config)
377db96d56Sopenharmony_ci
387db96d56Sopenharmony_ci   Takes the logging configuration from a dictionary.  The contents of
397db96d56Sopenharmony_ci   this dictionary are described in :ref:`logging-config-dictschema`
407db96d56Sopenharmony_ci   below.
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci   If an error is encountered during configuration, this function will
437db96d56Sopenharmony_ci   raise a :exc:`ValueError`, :exc:`TypeError`, :exc:`AttributeError`
447db96d56Sopenharmony_ci   or :exc:`ImportError` with a suitably descriptive message.  The
457db96d56Sopenharmony_ci   following is a (possibly incomplete) list of conditions which will
467db96d56Sopenharmony_ci   raise an error:
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ci   * A ``level`` which is not a string or which is a string not
497db96d56Sopenharmony_ci     corresponding to an actual logging level.
507db96d56Sopenharmony_ci   * A ``propagate`` value which is not a boolean.
517db96d56Sopenharmony_ci   * An id which does not have a corresponding destination.
527db96d56Sopenharmony_ci   * A non-existent handler id found during an incremental call.
537db96d56Sopenharmony_ci   * An invalid logger name.
547db96d56Sopenharmony_ci   * Inability to resolve to an internal or external object.
557db96d56Sopenharmony_ci
567db96d56Sopenharmony_ci   Parsing is performed by the :class:`DictConfigurator` class, whose
577db96d56Sopenharmony_ci   constructor is passed the dictionary used for configuration, and
587db96d56Sopenharmony_ci   has a :meth:`configure` method.  The :mod:`logging.config` module
597db96d56Sopenharmony_ci   has a callable attribute :attr:`dictConfigClass`
607db96d56Sopenharmony_ci   which is initially set to :class:`DictConfigurator`.
617db96d56Sopenharmony_ci   You can replace the value of :attr:`dictConfigClass` with a
627db96d56Sopenharmony_ci   suitable implementation of your own.
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci   :func:`dictConfig` calls :attr:`dictConfigClass` passing
657db96d56Sopenharmony_ci   the specified dictionary, and then calls the :meth:`configure` method on
667db96d56Sopenharmony_ci   the returned object to put the configuration into effect::
677db96d56Sopenharmony_ci
687db96d56Sopenharmony_ci         def dictConfig(config):
697db96d56Sopenharmony_ci             dictConfigClass(config).configure()
707db96d56Sopenharmony_ci
717db96d56Sopenharmony_ci   For example, a subclass of :class:`DictConfigurator` could call
727db96d56Sopenharmony_ci   ``DictConfigurator.__init__()`` in its own :meth:`__init__()`, then
737db96d56Sopenharmony_ci   set up custom prefixes which would be usable in the subsequent
747db96d56Sopenharmony_ci   :meth:`configure` call. :attr:`dictConfigClass` would be bound to
757db96d56Sopenharmony_ci   this new subclass, and then :func:`dictConfig` could be called exactly as
767db96d56Sopenharmony_ci   in the default, uncustomized state.
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci   .. versionadded:: 3.2
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci.. function:: fileConfig(fname, defaults=None, disable_existing_loggers=True, encoding=None)
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ci   Reads the logging configuration from a :mod:`configparser`\-format file. The
837db96d56Sopenharmony_ci   format of the file should be as described in
847db96d56Sopenharmony_ci   :ref:`logging-config-fileformat`.
857db96d56Sopenharmony_ci   This function can be called several times from an application, allowing an
867db96d56Sopenharmony_ci   end user to select from various pre-canned configurations (if the developer
877db96d56Sopenharmony_ci   provides a mechanism to present the choices and load the chosen
887db96d56Sopenharmony_ci   configuration).
897db96d56Sopenharmony_ci
907db96d56Sopenharmony_ci   It will raise :exc:`FileNotFoundError` if the file
917db96d56Sopenharmony_ci   doesn't exist and :exc:`RuntimeError` if the file is invalid or
927db96d56Sopenharmony_ci   empty.
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci   :param fname: A filename, or a file-like object, or an instance derived
957db96d56Sopenharmony_ci                 from :class:`~configparser.RawConfigParser`. If a
967db96d56Sopenharmony_ci                 ``RawConfigParser``-derived instance is passed, it is used as
977db96d56Sopenharmony_ci                 is. Otherwise, a :class:`~configparser.Configparser` is
987db96d56Sopenharmony_ci                 instantiated, and the configuration read by it from the
997db96d56Sopenharmony_ci                 object passed in ``fname``. If that has a :meth:`readline`
1007db96d56Sopenharmony_ci                 method, it is assumed to be a file-like object and read using
1017db96d56Sopenharmony_ci                 :meth:`~configparser.ConfigParser.read_file`; otherwise,
1027db96d56Sopenharmony_ci                 it is assumed to be a filename and passed to
1037db96d56Sopenharmony_ci                 :meth:`~configparser.ConfigParser.read`.
1047db96d56Sopenharmony_ci
1057db96d56Sopenharmony_ci
1067db96d56Sopenharmony_ci   :param defaults: Defaults to be passed to the ConfigParser can be specified
1077db96d56Sopenharmony_ci                    in this argument.
1087db96d56Sopenharmony_ci
1097db96d56Sopenharmony_ci   :param disable_existing_loggers: If specified as ``False``, loggers which
1107db96d56Sopenharmony_ci                                    exist when this call is made are left
1117db96d56Sopenharmony_ci                                    enabled. The default is ``True`` because this
1127db96d56Sopenharmony_ci                                    enables old behaviour in a
1137db96d56Sopenharmony_ci                                    backward-compatible way. This behaviour is to
1147db96d56Sopenharmony_ci                                    disable any existing non-root loggers unless
1157db96d56Sopenharmony_ci                                    they or their ancestors are explicitly named
1167db96d56Sopenharmony_ci                                    in the logging configuration.
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ci   :param encoding: The encoding used to open file when *fname* is filename.
1197db96d56Sopenharmony_ci
1207db96d56Sopenharmony_ci   .. versionchanged:: 3.4
1217db96d56Sopenharmony_ci      An instance of a subclass of :class:`~configparser.RawConfigParser` is
1227db96d56Sopenharmony_ci      now accepted as a value for ``fname``. This facilitates:
1237db96d56Sopenharmony_ci
1247db96d56Sopenharmony_ci      * Use of a configuration file where logging configuration is just part
1257db96d56Sopenharmony_ci        of the overall application configuration.
1267db96d56Sopenharmony_ci      * Use of a configuration read from a file, and then modified by the using
1277db96d56Sopenharmony_ci        application (e.g. based on command-line parameters or other aspects
1287db96d56Sopenharmony_ci        of the runtime environment) before being passed to ``fileConfig``.
1297db96d56Sopenharmony_ci
1307db96d56Sopenharmony_ci    .. versionadded:: 3.10
1317db96d56Sopenharmony_ci       The *encoding* parameter is added.
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci    .. versionchanged:: 3.11.4
1347db96d56Sopenharmony_ci       An exception will be thrown if the provided file
1357db96d56Sopenharmony_ci       doesn't exist or is invalid or empty.
1367db96d56Sopenharmony_ci
1377db96d56Sopenharmony_ci.. function:: listen(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None)
1387db96d56Sopenharmony_ci
1397db96d56Sopenharmony_ci   Starts up a socket server on the specified port, and listens for new
1407db96d56Sopenharmony_ci   configurations. If no port is specified, the module's default
1417db96d56Sopenharmony_ci   :const:`DEFAULT_LOGGING_CONFIG_PORT` is used. Logging configurations will be
1427db96d56Sopenharmony_ci   sent as a file suitable for processing by :func:`dictConfig` or
1437db96d56Sopenharmony_ci   :func:`fileConfig`. Returns a :class:`~threading.Thread` instance on which
1447db96d56Sopenharmony_ci   you can call :meth:`~threading.Thread.start` to start the server, and which
1457db96d56Sopenharmony_ci   you can :meth:`~threading.Thread.join` when appropriate. To stop the server,
1467db96d56Sopenharmony_ci   call :func:`stopListening`.
1477db96d56Sopenharmony_ci
1487db96d56Sopenharmony_ci   The ``verify`` argument, if specified, should be a callable which should
1497db96d56Sopenharmony_ci   verify whether bytes received across the socket are valid and should be
1507db96d56Sopenharmony_ci   processed. This could be done by encrypting and/or signing what is sent
1517db96d56Sopenharmony_ci   across the socket, such that the ``verify`` callable can perform
1527db96d56Sopenharmony_ci   signature verification and/or decryption. The ``verify`` callable is called
1537db96d56Sopenharmony_ci   with a single argument - the bytes received across the socket - and should
1547db96d56Sopenharmony_ci   return the bytes to be processed, or ``None`` to indicate that the bytes should
1557db96d56Sopenharmony_ci   be discarded. The returned bytes could be the same as the passed in bytes
1567db96d56Sopenharmony_ci   (e.g. when only verification is done), or they could be completely different
1577db96d56Sopenharmony_ci   (perhaps if decryption were performed).
1587db96d56Sopenharmony_ci
1597db96d56Sopenharmony_ci   To send a configuration to the socket, read in the configuration file and
1607db96d56Sopenharmony_ci   send it to the socket as a sequence of bytes preceded by a four-byte length
1617db96d56Sopenharmony_ci   string packed in binary using ``struct.pack('>L', n)``.
1627db96d56Sopenharmony_ci
1637db96d56Sopenharmony_ci   .. _logging-eval-security:
1647db96d56Sopenharmony_ci
1657db96d56Sopenharmony_ci   .. note::
1667db96d56Sopenharmony_ci
1677db96d56Sopenharmony_ci      Because portions of the configuration are passed through
1687db96d56Sopenharmony_ci      :func:`eval`, use of this function may open its users to a security risk.
1697db96d56Sopenharmony_ci      While the function only binds to a socket on ``localhost``, and so does
1707db96d56Sopenharmony_ci      not accept connections from remote machines, there are scenarios where
1717db96d56Sopenharmony_ci      untrusted code could be run under the account of the process which calls
1727db96d56Sopenharmony_ci      :func:`listen`. Specifically, if the process calling :func:`listen` runs
1737db96d56Sopenharmony_ci      on a multi-user machine where users cannot trust each other, then a
1747db96d56Sopenharmony_ci      malicious user could arrange to run essentially arbitrary code in a
1757db96d56Sopenharmony_ci      victim user's process, simply by connecting to the victim's
1767db96d56Sopenharmony_ci      :func:`listen` socket and sending a configuration which runs whatever
1777db96d56Sopenharmony_ci      code the attacker wants to have executed in the victim's process. This is
1787db96d56Sopenharmony_ci      especially easy to do if the default port is used, but not hard even if a
1797db96d56Sopenharmony_ci      different port is used. To avoid the risk of this happening, use the
1807db96d56Sopenharmony_ci      ``verify`` argument to :func:`listen` to prevent unrecognised
1817db96d56Sopenharmony_ci      configurations from being applied.
1827db96d56Sopenharmony_ci
1837db96d56Sopenharmony_ci   .. versionchanged:: 3.4
1847db96d56Sopenharmony_ci      The ``verify`` argument was added.
1857db96d56Sopenharmony_ci
1867db96d56Sopenharmony_ci   .. note::
1877db96d56Sopenharmony_ci
1887db96d56Sopenharmony_ci      If you want to send configurations to the listener which don't
1897db96d56Sopenharmony_ci      disable existing loggers, you will need to use a JSON format for
1907db96d56Sopenharmony_ci      the configuration, which will use :func:`dictConfig` for configuration.
1917db96d56Sopenharmony_ci      This method allows you to specify ``disable_existing_loggers`` as
1927db96d56Sopenharmony_ci      ``False`` in the configuration you send.
1937db96d56Sopenharmony_ci
1947db96d56Sopenharmony_ci
1957db96d56Sopenharmony_ci.. function:: stopListening()
1967db96d56Sopenharmony_ci
1977db96d56Sopenharmony_ci   Stops the listening server which was created with a call to :func:`listen`.
1987db96d56Sopenharmony_ci   This is typically called before calling :meth:`join` on the return value from
1997db96d56Sopenharmony_ci   :func:`listen`.
2007db96d56Sopenharmony_ci
2017db96d56Sopenharmony_ci
2027db96d56Sopenharmony_ciSecurity considerations
2037db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^
2047db96d56Sopenharmony_ci
2057db96d56Sopenharmony_ciThe logging configuration functionality tries to offer convenience, and in part this
2067db96d56Sopenharmony_ciis done by offering the ability to convert text in configuration files into Python
2077db96d56Sopenharmony_ciobjects used in logging configuration - for example, as described in
2087db96d56Sopenharmony_ci:ref:`logging-config-dict-userdef`. However, these same mechanisms (importing
2097db96d56Sopenharmony_cicallables from user-defined modules and calling them with parameters from the
2107db96d56Sopenharmony_ciconfiguration) could be used to invoke any code you like, and for this reason you
2117db96d56Sopenharmony_cishould treat configuration files from untrusted sources with *extreme caution* and
2127db96d56Sopenharmony_cisatisfy yourself that nothing bad can happen if you load them, before actually loading
2137db96d56Sopenharmony_cithem.
2147db96d56Sopenharmony_ci
2157db96d56Sopenharmony_ci
2167db96d56Sopenharmony_ci.. _logging-config-dictschema:
2177db96d56Sopenharmony_ci
2187db96d56Sopenharmony_ciConfiguration dictionary schema
2197db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2207db96d56Sopenharmony_ci
2217db96d56Sopenharmony_ciDescribing a logging configuration requires listing the various
2227db96d56Sopenharmony_ciobjects to create and the connections between them; for example, you
2237db96d56Sopenharmony_cimay create a handler named 'console' and then say that the logger
2247db96d56Sopenharmony_cinamed 'startup' will send its messages to the 'console' handler.
2257db96d56Sopenharmony_ciThese objects aren't limited to those provided by the :mod:`logging`
2267db96d56Sopenharmony_cimodule because you might write your own formatter or handler class.
2277db96d56Sopenharmony_ciThe parameters to these classes may also need to include external
2287db96d56Sopenharmony_ciobjects such as ``sys.stderr``.  The syntax for describing these
2297db96d56Sopenharmony_ciobjects and connections is defined in :ref:`logging-config-dict-connections`
2307db96d56Sopenharmony_cibelow.
2317db96d56Sopenharmony_ci
2327db96d56Sopenharmony_ciDictionary Schema Details
2337db96d56Sopenharmony_ci"""""""""""""""""""""""""
2347db96d56Sopenharmony_ci
2357db96d56Sopenharmony_ciThe dictionary passed to :func:`dictConfig` must contain the following
2367db96d56Sopenharmony_cikeys:
2377db96d56Sopenharmony_ci
2387db96d56Sopenharmony_ci* *version* - to be set to an integer value representing the schema
2397db96d56Sopenharmony_ci  version.  The only valid value at present is 1, but having this key
2407db96d56Sopenharmony_ci  allows the schema to evolve while still preserving backwards
2417db96d56Sopenharmony_ci  compatibility.
2427db96d56Sopenharmony_ci
2437db96d56Sopenharmony_ciAll other keys are optional, but if present they will be interpreted
2447db96d56Sopenharmony_cias described below.  In all cases below where a 'configuring dict' is
2457db96d56Sopenharmony_cimentioned, it will be checked for the special ``'()'`` key to see if a
2467db96d56Sopenharmony_cicustom instantiation is required.  If so, the mechanism described in
2477db96d56Sopenharmony_ci:ref:`logging-config-dict-userdef` below is used to create an instance;
2487db96d56Sopenharmony_ciotherwise, the context is used to determine what to instantiate.
2497db96d56Sopenharmony_ci
2507db96d56Sopenharmony_ci.. _logging-config-dictschema-formatters:
2517db96d56Sopenharmony_ci
2527db96d56Sopenharmony_ci* *formatters* - the corresponding value will be a dict in which each
2537db96d56Sopenharmony_ci  key is a formatter id and each value is a dict describing how to
2547db96d56Sopenharmony_ci  configure the corresponding :class:`~logging.Formatter` instance.
2557db96d56Sopenharmony_ci
2567db96d56Sopenharmony_ci  The configuring dict is searched for the following optional keys
2577db96d56Sopenharmony_ci  which correspond to the arguments passed to create a
2587db96d56Sopenharmony_ci  :class:`~logging.Formatter` object:
2597db96d56Sopenharmony_ci
2607db96d56Sopenharmony_ci   * ``format``
2617db96d56Sopenharmony_ci   * ``datefmt``
2627db96d56Sopenharmony_ci   * ``style``
2637db96d56Sopenharmony_ci   * ``validate`` (since version >=3.8)
2647db96d56Sopenharmony_ci
2657db96d56Sopenharmony_ci  An optional ``class`` key indicates the name of the formatter's
2667db96d56Sopenharmony_ci  class (as a dotted module and class name).  The instantiation
2677db96d56Sopenharmony_ci  arguments are as for :class:`~logging.Formatter`, thus this key is
2687db96d56Sopenharmony_ci  most useful for instantiating a customised subclass of
2697db96d56Sopenharmony_ci  :class:`~logging.Formatter`.  For example, the alternative class
2707db96d56Sopenharmony_ci  might present exception tracebacks in an expanded or condensed
2717db96d56Sopenharmony_ci  format.  If your formatter requires different or extra configuration
2727db96d56Sopenharmony_ci  keys, you should use :ref:`logging-config-dict-userdef`.
2737db96d56Sopenharmony_ci
2747db96d56Sopenharmony_ci* *filters* - the corresponding value will be a dict in which each key
2757db96d56Sopenharmony_ci  is a filter id and each value is a dict describing how to configure
2767db96d56Sopenharmony_ci  the corresponding Filter instance.
2777db96d56Sopenharmony_ci
2787db96d56Sopenharmony_ci  The configuring dict is searched for the key ``name`` (defaulting to the
2797db96d56Sopenharmony_ci  empty string) and this is used to construct a :class:`logging.Filter`
2807db96d56Sopenharmony_ci  instance.
2817db96d56Sopenharmony_ci
2827db96d56Sopenharmony_ci* *handlers* - the corresponding value will be a dict in which each
2837db96d56Sopenharmony_ci  key is a handler id and each value is a dict describing how to
2847db96d56Sopenharmony_ci  configure the corresponding Handler instance.
2857db96d56Sopenharmony_ci
2867db96d56Sopenharmony_ci  The configuring dict is searched for the following keys:
2877db96d56Sopenharmony_ci
2887db96d56Sopenharmony_ci  * ``class`` (mandatory).  This is the fully qualified name of the
2897db96d56Sopenharmony_ci    handler class.
2907db96d56Sopenharmony_ci
2917db96d56Sopenharmony_ci  * ``level`` (optional).  The level of the handler.
2927db96d56Sopenharmony_ci
2937db96d56Sopenharmony_ci  * ``formatter`` (optional).  The id of the formatter for this
2947db96d56Sopenharmony_ci    handler.
2957db96d56Sopenharmony_ci
2967db96d56Sopenharmony_ci  * ``filters`` (optional).  A list of ids of the filters for this
2977db96d56Sopenharmony_ci    handler.
2987db96d56Sopenharmony_ci
2997db96d56Sopenharmony_ci    .. versionchanged:: 3.11
3007db96d56Sopenharmony_ci       ``filters`` can take filter instances in addition to ids.
3017db96d56Sopenharmony_ci
3027db96d56Sopenharmony_ci  All *other* keys are passed through as keyword arguments to the
3037db96d56Sopenharmony_ci  handler's constructor.  For example, given the snippet:
3047db96d56Sopenharmony_ci
3057db96d56Sopenharmony_ci  .. code-block:: yaml
3067db96d56Sopenharmony_ci
3077db96d56Sopenharmony_ci      handlers:
3087db96d56Sopenharmony_ci        console:
3097db96d56Sopenharmony_ci          class : logging.StreamHandler
3107db96d56Sopenharmony_ci          formatter: brief
3117db96d56Sopenharmony_ci          level   : INFO
3127db96d56Sopenharmony_ci          filters: [allow_foo]
3137db96d56Sopenharmony_ci          stream  : ext://sys.stdout
3147db96d56Sopenharmony_ci        file:
3157db96d56Sopenharmony_ci          class : logging.handlers.RotatingFileHandler
3167db96d56Sopenharmony_ci          formatter: precise
3177db96d56Sopenharmony_ci          filename: logconfig.log
3187db96d56Sopenharmony_ci          maxBytes: 1024
3197db96d56Sopenharmony_ci          backupCount: 3
3207db96d56Sopenharmony_ci
3217db96d56Sopenharmony_ci  the handler with id ``console`` is instantiated as a
3227db96d56Sopenharmony_ci  :class:`logging.StreamHandler`, using ``sys.stdout`` as the underlying
3237db96d56Sopenharmony_ci  stream.  The handler with id ``file`` is instantiated as a
3247db96d56Sopenharmony_ci  :class:`logging.handlers.RotatingFileHandler` with the keyword arguments
3257db96d56Sopenharmony_ci  ``filename='logconfig.log', maxBytes=1024, backupCount=3``.
3267db96d56Sopenharmony_ci
3277db96d56Sopenharmony_ci* *loggers* - the corresponding value will be a dict in which each key
3287db96d56Sopenharmony_ci  is a logger name and each value is a dict describing how to
3297db96d56Sopenharmony_ci  configure the corresponding Logger instance.
3307db96d56Sopenharmony_ci
3317db96d56Sopenharmony_ci  The configuring dict is searched for the following keys:
3327db96d56Sopenharmony_ci
3337db96d56Sopenharmony_ci  * ``level`` (optional).  The level of the logger.
3347db96d56Sopenharmony_ci
3357db96d56Sopenharmony_ci  * ``propagate`` (optional).  The propagation setting of the logger.
3367db96d56Sopenharmony_ci
3377db96d56Sopenharmony_ci  * ``filters`` (optional).  A list of ids of the filters for this
3387db96d56Sopenharmony_ci    logger.
3397db96d56Sopenharmony_ci
3407db96d56Sopenharmony_ci    .. versionchanged:: 3.11
3417db96d56Sopenharmony_ci       ``filters`` can take filter instances in addition to ids.
3427db96d56Sopenharmony_ci
3437db96d56Sopenharmony_ci  * ``handlers`` (optional).  A list of ids of the handlers for this
3447db96d56Sopenharmony_ci    logger.
3457db96d56Sopenharmony_ci
3467db96d56Sopenharmony_ci  The specified loggers will be configured according to the level,
3477db96d56Sopenharmony_ci  propagation, filters and handlers specified.
3487db96d56Sopenharmony_ci
3497db96d56Sopenharmony_ci* *root* - this will be the configuration for the root logger.
3507db96d56Sopenharmony_ci  Processing of the configuration will be as for any logger, except
3517db96d56Sopenharmony_ci  that the ``propagate`` setting will not be applicable.
3527db96d56Sopenharmony_ci
3537db96d56Sopenharmony_ci* *incremental* - whether the configuration is to be interpreted as
3547db96d56Sopenharmony_ci  incremental to the existing configuration.  This value defaults to
3557db96d56Sopenharmony_ci  ``False``, which means that the specified configuration replaces the
3567db96d56Sopenharmony_ci  existing configuration with the same semantics as used by the
3577db96d56Sopenharmony_ci  existing :func:`fileConfig` API.
3587db96d56Sopenharmony_ci
3597db96d56Sopenharmony_ci  If the specified value is ``True``, the configuration is processed
3607db96d56Sopenharmony_ci  as described in the section on :ref:`logging-config-dict-incremental`.
3617db96d56Sopenharmony_ci
3627db96d56Sopenharmony_ci* *disable_existing_loggers* - whether any existing non-root loggers are
3637db96d56Sopenharmony_ci  to be disabled. This setting mirrors the parameter of the same name in
3647db96d56Sopenharmony_ci  :func:`fileConfig`. If absent, this parameter defaults to ``True``.
3657db96d56Sopenharmony_ci  This value is ignored if *incremental* is ``True``.
3667db96d56Sopenharmony_ci
3677db96d56Sopenharmony_ci.. _logging-config-dict-incremental:
3687db96d56Sopenharmony_ci
3697db96d56Sopenharmony_ciIncremental Configuration
3707db96d56Sopenharmony_ci"""""""""""""""""""""""""
3717db96d56Sopenharmony_ci
3727db96d56Sopenharmony_ciIt is difficult to provide complete flexibility for incremental
3737db96d56Sopenharmony_ciconfiguration.  For example, because objects such as filters
3747db96d56Sopenharmony_ciand formatters are anonymous, once a configuration is set up, it is
3757db96d56Sopenharmony_cinot possible to refer to such anonymous objects when augmenting a
3767db96d56Sopenharmony_ciconfiguration.
3777db96d56Sopenharmony_ci
3787db96d56Sopenharmony_ciFurthermore, there is not a compelling case for arbitrarily altering
3797db96d56Sopenharmony_cithe object graph of loggers, handlers, filters, formatters at
3807db96d56Sopenharmony_cirun-time, once a configuration is set up; the verbosity of loggers and
3817db96d56Sopenharmony_cihandlers can be controlled just by setting levels (and, in the case of
3827db96d56Sopenharmony_ciloggers, propagation flags).  Changing the object graph arbitrarily in
3837db96d56Sopenharmony_cia safe way is problematic in a multi-threaded environment; while not
3847db96d56Sopenharmony_ciimpossible, the benefits are not worth the complexity it adds to the
3857db96d56Sopenharmony_ciimplementation.
3867db96d56Sopenharmony_ci
3877db96d56Sopenharmony_ciThus, when the ``incremental`` key of a configuration dict is present
3887db96d56Sopenharmony_ciand is ``True``, the system will completely ignore any ``formatters`` and
3897db96d56Sopenharmony_ci``filters`` entries, and process only the ``level``
3907db96d56Sopenharmony_cisettings in the ``handlers`` entries, and the ``level`` and
3917db96d56Sopenharmony_ci``propagate`` settings in the ``loggers`` and ``root`` entries.
3927db96d56Sopenharmony_ci
3937db96d56Sopenharmony_ciUsing a value in the configuration dict lets configurations to be sent
3947db96d56Sopenharmony_ciover the wire as pickled dicts to a socket listener. Thus, the logging
3957db96d56Sopenharmony_civerbosity of a long-running application can be altered over time with
3967db96d56Sopenharmony_cino need to stop and restart the application.
3977db96d56Sopenharmony_ci
3987db96d56Sopenharmony_ci.. _logging-config-dict-connections:
3997db96d56Sopenharmony_ci
4007db96d56Sopenharmony_ciObject connections
4017db96d56Sopenharmony_ci""""""""""""""""""
4027db96d56Sopenharmony_ci
4037db96d56Sopenharmony_ciThe schema describes a set of logging objects - loggers,
4047db96d56Sopenharmony_cihandlers, formatters, filters - which are connected to each other in
4057db96d56Sopenharmony_cian object graph.  Thus, the schema needs to represent connections
4067db96d56Sopenharmony_cibetween the objects.  For example, say that, once configured, a
4077db96d56Sopenharmony_ciparticular logger has attached to it a particular handler.  For the
4087db96d56Sopenharmony_cipurposes of this discussion, we can say that the logger represents the
4097db96d56Sopenharmony_cisource, and the handler the destination, of a connection between the
4107db96d56Sopenharmony_citwo.  Of course in the configured objects this is represented by the
4117db96d56Sopenharmony_cilogger holding a reference to the handler.  In the configuration dict,
4127db96d56Sopenharmony_cithis is done by giving each destination object an id which identifies
4137db96d56Sopenharmony_ciit unambiguously, and then using the id in the source object's
4147db96d56Sopenharmony_ciconfiguration to indicate that a connection exists between the source
4157db96d56Sopenharmony_ciand the destination object with that id.
4167db96d56Sopenharmony_ci
4177db96d56Sopenharmony_ciSo, for example, consider the following YAML snippet:
4187db96d56Sopenharmony_ci
4197db96d56Sopenharmony_ci.. code-block:: yaml
4207db96d56Sopenharmony_ci
4217db96d56Sopenharmony_ci    formatters:
4227db96d56Sopenharmony_ci      brief:
4237db96d56Sopenharmony_ci        # configuration for formatter with id 'brief' goes here
4247db96d56Sopenharmony_ci      precise:
4257db96d56Sopenharmony_ci        # configuration for formatter with id 'precise' goes here
4267db96d56Sopenharmony_ci    handlers:
4277db96d56Sopenharmony_ci      h1: #This is an id
4287db96d56Sopenharmony_ci       # configuration of handler with id 'h1' goes here
4297db96d56Sopenharmony_ci       formatter: brief
4307db96d56Sopenharmony_ci      h2: #This is another id
4317db96d56Sopenharmony_ci       # configuration of handler with id 'h2' goes here
4327db96d56Sopenharmony_ci       formatter: precise
4337db96d56Sopenharmony_ci    loggers:
4347db96d56Sopenharmony_ci      foo.bar.baz:
4357db96d56Sopenharmony_ci        # other configuration for logger 'foo.bar.baz'
4367db96d56Sopenharmony_ci        handlers: [h1, h2]
4377db96d56Sopenharmony_ci
4387db96d56Sopenharmony_ci(Note: YAML used here because it's a little more readable than the
4397db96d56Sopenharmony_ciequivalent Python source form for the dictionary.)
4407db96d56Sopenharmony_ci
4417db96d56Sopenharmony_ciThe ids for loggers are the logger names which would be used
4427db96d56Sopenharmony_ciprogrammatically to obtain a reference to those loggers, e.g.
4437db96d56Sopenharmony_ci``foo.bar.baz``.  The ids for Formatters and Filters can be any string
4447db96d56Sopenharmony_civalue (such as ``brief``, ``precise`` above) and they are transient,
4457db96d56Sopenharmony_ciin that they are only meaningful for processing the configuration
4467db96d56Sopenharmony_cidictionary and used to determine connections between objects, and are
4477db96d56Sopenharmony_cinot persisted anywhere when the configuration call is complete.
4487db96d56Sopenharmony_ci
4497db96d56Sopenharmony_ciThe above snippet indicates that logger named ``foo.bar.baz`` should
4507db96d56Sopenharmony_cihave two handlers attached to it, which are described by the handler
4517db96d56Sopenharmony_ciids ``h1`` and ``h2``. The formatter for ``h1`` is that described by id
4527db96d56Sopenharmony_ci``brief``, and the formatter for ``h2`` is that described by id
4537db96d56Sopenharmony_ci``precise``.
4547db96d56Sopenharmony_ci
4557db96d56Sopenharmony_ci
4567db96d56Sopenharmony_ci.. _logging-config-dict-userdef:
4577db96d56Sopenharmony_ci
4587db96d56Sopenharmony_ciUser-defined objects
4597db96d56Sopenharmony_ci""""""""""""""""""""
4607db96d56Sopenharmony_ci
4617db96d56Sopenharmony_ciThe schema supports user-defined objects for handlers, filters and
4627db96d56Sopenharmony_ciformatters.  (Loggers do not need to have different types for
4637db96d56Sopenharmony_cidifferent instances, so there is no support in this configuration
4647db96d56Sopenharmony_cischema for user-defined logger classes.)
4657db96d56Sopenharmony_ci
4667db96d56Sopenharmony_ciObjects to be configured are described by dictionaries
4677db96d56Sopenharmony_ciwhich detail their configuration.  In some places, the logging system
4687db96d56Sopenharmony_ciwill be able to infer from the context how an object is to be
4697db96d56Sopenharmony_ciinstantiated, but when a user-defined object is to be instantiated,
4707db96d56Sopenharmony_cithe system will not know how to do this.  In order to provide complete
4717db96d56Sopenharmony_ciflexibility for user-defined object instantiation, the user needs
4727db96d56Sopenharmony_cito provide a 'factory' - a callable which is called with a
4737db96d56Sopenharmony_ciconfiguration dictionary and which returns the instantiated object.
4747db96d56Sopenharmony_ciThis is signalled by an absolute import path to the factory being
4757db96d56Sopenharmony_cimade available under the special key ``'()'``.  Here's a concrete
4767db96d56Sopenharmony_ciexample:
4777db96d56Sopenharmony_ci
4787db96d56Sopenharmony_ci.. code-block:: yaml
4797db96d56Sopenharmony_ci
4807db96d56Sopenharmony_ci    formatters:
4817db96d56Sopenharmony_ci      brief:
4827db96d56Sopenharmony_ci        format: '%(message)s'
4837db96d56Sopenharmony_ci      default:
4847db96d56Sopenharmony_ci        format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
4857db96d56Sopenharmony_ci        datefmt: '%Y-%m-%d %H:%M:%S'
4867db96d56Sopenharmony_ci      custom:
4877db96d56Sopenharmony_ci          (): my.package.customFormatterFactory
4887db96d56Sopenharmony_ci          bar: baz
4897db96d56Sopenharmony_ci          spam: 99.9
4907db96d56Sopenharmony_ci          answer: 42
4917db96d56Sopenharmony_ci
4927db96d56Sopenharmony_ciThe above YAML snippet defines three formatters.  The first, with id
4937db96d56Sopenharmony_ci``brief``, is a standard :class:`logging.Formatter` instance with the
4947db96d56Sopenharmony_cispecified format string.  The second, with id ``default``, has a
4957db96d56Sopenharmony_cilonger format and also defines the time format explicitly, and will
4967db96d56Sopenharmony_ciresult in a :class:`logging.Formatter` initialized with those two format
4977db96d56Sopenharmony_cistrings.  Shown in Python source form, the ``brief`` and ``default``
4987db96d56Sopenharmony_ciformatters have configuration sub-dictionaries::
4997db96d56Sopenharmony_ci
5007db96d56Sopenharmony_ci    {
5017db96d56Sopenharmony_ci      'format' : '%(message)s'
5027db96d56Sopenharmony_ci    }
5037db96d56Sopenharmony_ci
5047db96d56Sopenharmony_ciand::
5057db96d56Sopenharmony_ci
5067db96d56Sopenharmony_ci    {
5077db96d56Sopenharmony_ci      'format' : '%(asctime)s %(levelname)-8s %(name)-15s %(message)s',
5087db96d56Sopenharmony_ci      'datefmt' : '%Y-%m-%d %H:%M:%S'
5097db96d56Sopenharmony_ci    }
5107db96d56Sopenharmony_ci
5117db96d56Sopenharmony_cirespectively, and as these dictionaries do not contain the special key
5127db96d56Sopenharmony_ci``'()'``, the instantiation is inferred from the context: as a result,
5137db96d56Sopenharmony_cistandard :class:`logging.Formatter` instances are created.  The
5147db96d56Sopenharmony_ciconfiguration sub-dictionary for the third formatter, with id
5157db96d56Sopenharmony_ci``custom``, is::
5167db96d56Sopenharmony_ci
5177db96d56Sopenharmony_ci  {
5187db96d56Sopenharmony_ci    '()' : 'my.package.customFormatterFactory',
5197db96d56Sopenharmony_ci    'bar' : 'baz',
5207db96d56Sopenharmony_ci    'spam' : 99.9,
5217db96d56Sopenharmony_ci    'answer' : 42
5227db96d56Sopenharmony_ci  }
5237db96d56Sopenharmony_ci
5247db96d56Sopenharmony_ciand this contains the special key ``'()'``, which means that
5257db96d56Sopenharmony_ciuser-defined instantiation is wanted.  In this case, the specified
5267db96d56Sopenharmony_cifactory callable will be used. If it is an actual callable it will be
5277db96d56Sopenharmony_ciused directly - otherwise, if you specify a string (as in the example)
5287db96d56Sopenharmony_cithe actual callable will be located using normal import mechanisms.
5297db96d56Sopenharmony_ciThe callable will be called with the **remaining** items in the
5307db96d56Sopenharmony_ciconfiguration sub-dictionary as keyword arguments.  In the above
5317db96d56Sopenharmony_ciexample, the formatter with id ``custom`` will be assumed to be
5327db96d56Sopenharmony_cireturned by the call::
5337db96d56Sopenharmony_ci
5347db96d56Sopenharmony_ci    my.package.customFormatterFactory(bar='baz', spam=99.9, answer=42)
5357db96d56Sopenharmony_ci
5367db96d56Sopenharmony_ci.. warning:: The values for keys such as ``bar``, ``spam`` and ``answer`` in
5377db96d56Sopenharmony_ci   the above example should not be configuration dictionaries or references such
5387db96d56Sopenharmony_ci   as ``cfg://foo`` or ``ext://bar``, because they will not be processed by the
5397db96d56Sopenharmony_ci   configuration machinery, but passed to the callable as-is.
5407db96d56Sopenharmony_ci
5417db96d56Sopenharmony_ciThe key ``'()'`` has been used as the special key because it is not a
5427db96d56Sopenharmony_civalid keyword parameter name, and so will not clash with the names of
5437db96d56Sopenharmony_cithe keyword arguments used in the call.  The ``'()'`` also serves as a
5447db96d56Sopenharmony_cimnemonic that the corresponding value is a callable.
5457db96d56Sopenharmony_ci
5467db96d56Sopenharmony_ci    .. versionchanged:: 3.11
5477db96d56Sopenharmony_ci       The ``filters`` member of ``handlers`` and ``loggers`` can take
5487db96d56Sopenharmony_ci       filter instances in addition to ids.
5497db96d56Sopenharmony_ci
5507db96d56Sopenharmony_ciYou can also specify a special key ``'.'`` whose value is a dictionary is a
5517db96d56Sopenharmony_cimapping of attribute names to values. If found, the specified attributes will
5527db96d56Sopenharmony_cibe set on the user-defined object before it is returned. Thus, with the
5537db96d56Sopenharmony_cifollowing configuration::
5547db96d56Sopenharmony_ci
5557db96d56Sopenharmony_ci    {
5567db96d56Sopenharmony_ci      '()' : 'my.package.customFormatterFactory',
5577db96d56Sopenharmony_ci      'bar' : 'baz',
5587db96d56Sopenharmony_ci      'spam' : 99.9,
5597db96d56Sopenharmony_ci      'answer' : 42,
5607db96d56Sopenharmony_ci      '.' {
5617db96d56Sopenharmony_ci        'foo': 'bar',
5627db96d56Sopenharmony_ci        'baz': 'bozz'
5637db96d56Sopenharmony_ci      }
5647db96d56Sopenharmony_ci    }
5657db96d56Sopenharmony_ci
5667db96d56Sopenharmony_cithe returned formatter will have attribute ``foo`` set to ``'bar'`` and
5677db96d56Sopenharmony_ciattribute ``baz`` set to ``'bozz'``.
5687db96d56Sopenharmony_ci
5697db96d56Sopenharmony_ci.. warning:: The values for attributes such as ``foo`` and ``baz`` in
5707db96d56Sopenharmony_ci   the above example should not be configuration dictionaries or references such
5717db96d56Sopenharmony_ci   as ``cfg://foo`` or ``ext://bar``, because they will not be processed by the
5727db96d56Sopenharmony_ci   configuration machinery, but set as attribute values as-is.
5737db96d56Sopenharmony_ci
5747db96d56Sopenharmony_ci
5757db96d56Sopenharmony_ci.. _handler-config-dict-order:
5767db96d56Sopenharmony_ci
5777db96d56Sopenharmony_ciHandler configuration order
5787db96d56Sopenharmony_ci"""""""""""""""""""""""""""
5797db96d56Sopenharmony_ci
5807db96d56Sopenharmony_ciHandlers are configured in alphabetical order of their keys, and a configured
5817db96d56Sopenharmony_cihandler replaces the configuration dictionary in (a working copy of) the
5827db96d56Sopenharmony_ci``handlers`` dictionary in the schema. If you use a construct such as
5837db96d56Sopenharmony_ci``cfg://handlers.foo``, then initially ``handlers['foo']`` points to the
5847db96d56Sopenharmony_ciconfiguration dictionary for the handler named ``foo``, and later (once that
5857db96d56Sopenharmony_cihandler has been configured) it points to the configured handler instance.
5867db96d56Sopenharmony_ciThus, ``cfg://handlers.foo`` could resolve to either a dictionary or a handler
5877db96d56Sopenharmony_ciinstance. In general, it is wise to name handlers in a way such that dependent
5887db96d56Sopenharmony_cihandlers are configured _after_ any handlers they depend on; that allows
5897db96d56Sopenharmony_cisomething like ``cfg://handlers.foo`` to be used in configuring a handler that
5907db96d56Sopenharmony_cidepends on handler ``foo``. If that dependent handler were named ``bar``,
5917db96d56Sopenharmony_ciproblems would result, because the configuration of ``bar`` would be attempted
5927db96d56Sopenharmony_cibefore that of ``foo``, and ``foo`` would not yet have been configured.
5937db96d56Sopenharmony_ciHowever, if the dependent handler were named ``foobar``, it would be configured
5947db96d56Sopenharmony_ciafter ``foo``, with the result that ``cfg://handlers.foo`` would resolve to
5957db96d56Sopenharmony_ciconfigured handler ``foo``, and not its configuration dictionary.
5967db96d56Sopenharmony_ci
5977db96d56Sopenharmony_ci
5987db96d56Sopenharmony_ci.. _logging-config-dict-externalobj:
5997db96d56Sopenharmony_ci
6007db96d56Sopenharmony_ciAccess to external objects
6017db96d56Sopenharmony_ci""""""""""""""""""""""""""
6027db96d56Sopenharmony_ci
6037db96d56Sopenharmony_ciThere are times where a configuration needs to refer to objects
6047db96d56Sopenharmony_ciexternal to the configuration, for example ``sys.stderr``.  If the
6057db96d56Sopenharmony_ciconfiguration dict is constructed using Python code, this is
6067db96d56Sopenharmony_cistraightforward, but a problem arises when the configuration is
6077db96d56Sopenharmony_ciprovided via a text file (e.g. JSON, YAML).  In a text file, there is
6087db96d56Sopenharmony_cino standard way to distinguish ``sys.stderr`` from the literal string
6097db96d56Sopenharmony_ci``'sys.stderr'``.  To facilitate this distinction, the configuration
6107db96d56Sopenharmony_cisystem looks for certain special prefixes in string values and
6117db96d56Sopenharmony_citreat them specially.  For example, if the literal string
6127db96d56Sopenharmony_ci``'ext://sys.stderr'`` is provided as a value in the configuration,
6137db96d56Sopenharmony_cithen the ``ext://`` will be stripped off and the remainder of the
6147db96d56Sopenharmony_civalue processed using normal import mechanisms.
6157db96d56Sopenharmony_ci
6167db96d56Sopenharmony_ciThe handling of such prefixes is done in a way analogous to protocol
6177db96d56Sopenharmony_cihandling: there is a generic mechanism to look for prefixes which
6187db96d56Sopenharmony_cimatch the regular expression ``^(?P<prefix>[a-z]+)://(?P<suffix>.*)$``
6197db96d56Sopenharmony_ciwhereby, if the ``prefix`` is recognised, the ``suffix`` is processed
6207db96d56Sopenharmony_ciin a prefix-dependent manner and the result of the processing replaces
6217db96d56Sopenharmony_cithe string value.  If the prefix is not recognised, then the string
6227db96d56Sopenharmony_civalue will be left as-is.
6237db96d56Sopenharmony_ci
6247db96d56Sopenharmony_ci
6257db96d56Sopenharmony_ci.. _logging-config-dict-internalobj:
6267db96d56Sopenharmony_ci
6277db96d56Sopenharmony_ciAccess to internal objects
6287db96d56Sopenharmony_ci""""""""""""""""""""""""""
6297db96d56Sopenharmony_ci
6307db96d56Sopenharmony_ciAs well as external objects, there is sometimes also a need to refer
6317db96d56Sopenharmony_cito objects in the configuration.  This will be done implicitly by the
6327db96d56Sopenharmony_ciconfiguration system for things that it knows about.  For example, the
6337db96d56Sopenharmony_cistring value ``'DEBUG'`` for a ``level`` in a logger or handler will
6347db96d56Sopenharmony_ciautomatically be converted to the value ``logging.DEBUG``, and the
6357db96d56Sopenharmony_ci``handlers``, ``filters`` and ``formatter`` entries will take an
6367db96d56Sopenharmony_ciobject id and resolve to the appropriate destination object.
6377db96d56Sopenharmony_ci
6387db96d56Sopenharmony_ciHowever, a more generic mechanism is needed for user-defined
6397db96d56Sopenharmony_ciobjects which are not known to the :mod:`logging` module.  For
6407db96d56Sopenharmony_ciexample, consider :class:`logging.handlers.MemoryHandler`, which takes
6417db96d56Sopenharmony_cia ``target`` argument which is another handler to delegate to. Since
6427db96d56Sopenharmony_cithe system already knows about this class, then in the configuration,
6437db96d56Sopenharmony_cithe given ``target`` just needs to be the object id of the relevant
6447db96d56Sopenharmony_citarget handler, and the system will resolve to the handler from the
6457db96d56Sopenharmony_ciid.  If, however, a user defines a ``my.package.MyHandler`` which has
6467db96d56Sopenharmony_cian ``alternate`` handler, the configuration system would not know that
6477db96d56Sopenharmony_cithe ``alternate`` referred to a handler.  To cater for this, a generic
6487db96d56Sopenharmony_ciresolution system allows the user to specify:
6497db96d56Sopenharmony_ci
6507db96d56Sopenharmony_ci.. code-block:: yaml
6517db96d56Sopenharmony_ci
6527db96d56Sopenharmony_ci    handlers:
6537db96d56Sopenharmony_ci      file:
6547db96d56Sopenharmony_ci        # configuration of file handler goes here
6557db96d56Sopenharmony_ci
6567db96d56Sopenharmony_ci      custom:
6577db96d56Sopenharmony_ci        (): my.package.MyHandler
6587db96d56Sopenharmony_ci        alternate: cfg://handlers.file
6597db96d56Sopenharmony_ci
6607db96d56Sopenharmony_ciThe literal string ``'cfg://handlers.file'`` will be resolved in an
6617db96d56Sopenharmony_cianalogous way to strings with the ``ext://`` prefix, but looking
6627db96d56Sopenharmony_ciin the configuration itself rather than the import namespace.  The
6637db96d56Sopenharmony_cimechanism allows access by dot or by index, in a similar way to
6647db96d56Sopenharmony_cithat provided by ``str.format``.  Thus, given the following snippet:
6657db96d56Sopenharmony_ci
6667db96d56Sopenharmony_ci.. code-block:: yaml
6677db96d56Sopenharmony_ci
6687db96d56Sopenharmony_ci    handlers:
6697db96d56Sopenharmony_ci      email:
6707db96d56Sopenharmony_ci        class: logging.handlers.SMTPHandler
6717db96d56Sopenharmony_ci        mailhost: localhost
6727db96d56Sopenharmony_ci        fromaddr: my_app@domain.tld
6737db96d56Sopenharmony_ci        toaddrs:
6747db96d56Sopenharmony_ci          - support_team@domain.tld
6757db96d56Sopenharmony_ci          - dev_team@domain.tld
6767db96d56Sopenharmony_ci        subject: Houston, we have a problem.
6777db96d56Sopenharmony_ci
6787db96d56Sopenharmony_ciin the configuration, the string ``'cfg://handlers'`` would resolve to
6797db96d56Sopenharmony_cithe dict with key ``handlers``, the string ``'cfg://handlers.email``
6807db96d56Sopenharmony_ciwould resolve to the dict with key ``email`` in the ``handlers`` dict,
6817db96d56Sopenharmony_ciand so on.  The string ``'cfg://handlers.email.toaddrs[1]`` would
6827db96d56Sopenharmony_ciresolve to ``'dev_team@domain.tld'`` and the string
6837db96d56Sopenharmony_ci``'cfg://handlers.email.toaddrs[0]'`` would resolve to the value
6847db96d56Sopenharmony_ci``'support_team@domain.tld'``. The ``subject`` value could be accessed
6857db96d56Sopenharmony_ciusing either ``'cfg://handlers.email.subject'`` or, equivalently,
6867db96d56Sopenharmony_ci``'cfg://handlers.email[subject]'``.  The latter form only needs to be
6877db96d56Sopenharmony_ciused if the key contains spaces or non-alphanumeric characters.  If an
6887db96d56Sopenharmony_ciindex value consists only of decimal digits, access will be attempted
6897db96d56Sopenharmony_ciusing the corresponding integer value, falling back to the string
6907db96d56Sopenharmony_civalue if needed.
6917db96d56Sopenharmony_ci
6927db96d56Sopenharmony_ciGiven a string ``cfg://handlers.myhandler.mykey.123``, this will
6937db96d56Sopenharmony_ciresolve to ``config_dict['handlers']['myhandler']['mykey']['123']``.
6947db96d56Sopenharmony_ciIf the string is specified as ``cfg://handlers.myhandler.mykey[123]``,
6957db96d56Sopenharmony_cithe system will attempt to retrieve the value from
6967db96d56Sopenharmony_ci``config_dict['handlers']['myhandler']['mykey'][123]``, and fall back
6977db96d56Sopenharmony_cito ``config_dict['handlers']['myhandler']['mykey']['123']`` if that
6987db96d56Sopenharmony_cifails.
6997db96d56Sopenharmony_ci
7007db96d56Sopenharmony_ci
7017db96d56Sopenharmony_ci.. _logging-import-resolution:
7027db96d56Sopenharmony_ci
7037db96d56Sopenharmony_ciImport resolution and custom importers
7047db96d56Sopenharmony_ci""""""""""""""""""""""""""""""""""""""
7057db96d56Sopenharmony_ci
7067db96d56Sopenharmony_ciImport resolution, by default, uses the builtin :func:`__import__` function
7077db96d56Sopenharmony_cito do its importing. You may want to replace this with your own importing
7087db96d56Sopenharmony_cimechanism: if so, you can replace the :attr:`importer` attribute of the
7097db96d56Sopenharmony_ci:class:`DictConfigurator` or its superclass, the
7107db96d56Sopenharmony_ci:class:`BaseConfigurator` class. However, you need to be
7117db96d56Sopenharmony_cicareful because of the way functions are accessed from classes via
7127db96d56Sopenharmony_cidescriptors. If you are using a Python callable to do your imports, and you
7137db96d56Sopenharmony_ciwant to define it at class level rather than instance level, you need to wrap
7147db96d56Sopenharmony_ciit with :func:`staticmethod`. For example::
7157db96d56Sopenharmony_ci
7167db96d56Sopenharmony_ci   from importlib import import_module
7177db96d56Sopenharmony_ci   from logging.config import BaseConfigurator
7187db96d56Sopenharmony_ci
7197db96d56Sopenharmony_ci   BaseConfigurator.importer = staticmethod(import_module)
7207db96d56Sopenharmony_ci
7217db96d56Sopenharmony_ciYou don't need to wrap with :func:`staticmethod` if you're setting the import
7227db96d56Sopenharmony_cicallable on a configurator *instance*.
7237db96d56Sopenharmony_ci
7247db96d56Sopenharmony_ci
7257db96d56Sopenharmony_ci.. _logging-config-fileformat:
7267db96d56Sopenharmony_ci
7277db96d56Sopenharmony_ciConfiguration file format
7287db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^
7297db96d56Sopenharmony_ci
7307db96d56Sopenharmony_ciThe configuration file format understood by :func:`fileConfig` is based on
7317db96d56Sopenharmony_ci:mod:`configparser` functionality. The file must contain sections called
7327db96d56Sopenharmony_ci``[loggers]``, ``[handlers]`` and ``[formatters]`` which identify by name the
7337db96d56Sopenharmony_cientities of each type which are defined in the file. For each such entity, there
7347db96d56Sopenharmony_ciis a separate section which identifies how that entity is configured.  Thus, for
7357db96d56Sopenharmony_cia logger named ``log01`` in the ``[loggers]`` section, the relevant
7367db96d56Sopenharmony_ciconfiguration details are held in a section ``[logger_log01]``. Similarly, a
7377db96d56Sopenharmony_cihandler called ``hand01`` in the ``[handlers]`` section will have its
7387db96d56Sopenharmony_ciconfiguration held in a section called ``[handler_hand01]``, while a formatter
7397db96d56Sopenharmony_cicalled ``form01`` in the ``[formatters]`` section will have its configuration
7407db96d56Sopenharmony_cispecified in a section called ``[formatter_form01]``. The root logger
7417db96d56Sopenharmony_ciconfiguration must be specified in a section called ``[logger_root]``.
7427db96d56Sopenharmony_ci
7437db96d56Sopenharmony_ci.. note::
7447db96d56Sopenharmony_ci
7457db96d56Sopenharmony_ci   The :func:`fileConfig` API is older than the :func:`dictConfig` API and does
7467db96d56Sopenharmony_ci   not provide functionality to cover certain aspects of logging. For example,
7477db96d56Sopenharmony_ci   you cannot configure :class:`~logging.Filter` objects, which provide for
7487db96d56Sopenharmony_ci   filtering of messages beyond simple integer levels, using :func:`fileConfig`.
7497db96d56Sopenharmony_ci   If you need to have instances of :class:`~logging.Filter` in your logging
7507db96d56Sopenharmony_ci   configuration, you will need to use :func:`dictConfig`. Note that future
7517db96d56Sopenharmony_ci   enhancements to configuration functionality will be added to
7527db96d56Sopenharmony_ci   :func:`dictConfig`, so it's worth considering transitioning to this newer
7537db96d56Sopenharmony_ci   API when it's convenient to do so.
7547db96d56Sopenharmony_ci
7557db96d56Sopenharmony_ciExamples of these sections in the file are given below.
7567db96d56Sopenharmony_ci
7577db96d56Sopenharmony_ci.. code-block:: ini
7587db96d56Sopenharmony_ci
7597db96d56Sopenharmony_ci   [loggers]
7607db96d56Sopenharmony_ci   keys=root,log02,log03,log04,log05,log06,log07
7617db96d56Sopenharmony_ci
7627db96d56Sopenharmony_ci   [handlers]
7637db96d56Sopenharmony_ci   keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09
7647db96d56Sopenharmony_ci
7657db96d56Sopenharmony_ci   [formatters]
7667db96d56Sopenharmony_ci   keys=form01,form02,form03,form04,form05,form06,form07,form08,form09
7677db96d56Sopenharmony_ci
7687db96d56Sopenharmony_ciThe root logger must specify a level and a list of handlers. An example of a
7697db96d56Sopenharmony_ciroot logger section is given below.
7707db96d56Sopenharmony_ci
7717db96d56Sopenharmony_ci.. code-block:: ini
7727db96d56Sopenharmony_ci
7737db96d56Sopenharmony_ci   [logger_root]
7747db96d56Sopenharmony_ci   level=NOTSET
7757db96d56Sopenharmony_ci   handlers=hand01
7767db96d56Sopenharmony_ci
7777db96d56Sopenharmony_ciThe ``level`` entry can be one of ``DEBUG, INFO, WARNING, ERROR, CRITICAL`` or
7787db96d56Sopenharmony_ci``NOTSET``. For the root logger only, ``NOTSET`` means that all messages will be
7797db96d56Sopenharmony_cilogged. Level values are :ref:`evaluated <func-eval>` in the context of the ``logging``
7807db96d56Sopenharmony_cipackage's namespace.
7817db96d56Sopenharmony_ci
7827db96d56Sopenharmony_ciThe ``handlers`` entry is a comma-separated list of handler names, which must
7837db96d56Sopenharmony_ciappear in the ``[handlers]`` section. These names must appear in the
7847db96d56Sopenharmony_ci``[handlers]`` section and have corresponding sections in the configuration
7857db96d56Sopenharmony_cifile.
7867db96d56Sopenharmony_ci
7877db96d56Sopenharmony_ciFor loggers other than the root logger, some additional information is required.
7887db96d56Sopenharmony_ciThis is illustrated by the following example.
7897db96d56Sopenharmony_ci
7907db96d56Sopenharmony_ci.. code-block:: ini
7917db96d56Sopenharmony_ci
7927db96d56Sopenharmony_ci   [logger_parser]
7937db96d56Sopenharmony_ci   level=DEBUG
7947db96d56Sopenharmony_ci   handlers=hand01
7957db96d56Sopenharmony_ci   propagate=1
7967db96d56Sopenharmony_ci   qualname=compiler.parser
7977db96d56Sopenharmony_ci
7987db96d56Sopenharmony_ciThe ``level`` and ``handlers`` entries are interpreted as for the root logger,
7997db96d56Sopenharmony_ciexcept that if a non-root logger's level is specified as ``NOTSET``, the system
8007db96d56Sopenharmony_ciconsults loggers higher up the hierarchy to determine the effective level of the
8017db96d56Sopenharmony_cilogger. The ``propagate`` entry is set to 1 to indicate that messages must
8027db96d56Sopenharmony_cipropagate to handlers higher up the logger hierarchy from this logger, or 0 to
8037db96d56Sopenharmony_ciindicate that messages are **not** propagated to handlers up the hierarchy. The
8047db96d56Sopenharmony_ci``qualname`` entry is the hierarchical channel name of the logger, that is to
8057db96d56Sopenharmony_cisay the name used by the application to get the logger.
8067db96d56Sopenharmony_ci
8077db96d56Sopenharmony_ciSections which specify handler configuration are exemplified by the following.
8087db96d56Sopenharmony_ci
8097db96d56Sopenharmony_ci.. code-block:: ini
8107db96d56Sopenharmony_ci
8117db96d56Sopenharmony_ci   [handler_hand01]
8127db96d56Sopenharmony_ci   class=StreamHandler
8137db96d56Sopenharmony_ci   level=NOTSET
8147db96d56Sopenharmony_ci   formatter=form01
8157db96d56Sopenharmony_ci   args=(sys.stdout,)
8167db96d56Sopenharmony_ci
8177db96d56Sopenharmony_ciThe ``class`` entry indicates the handler's class (as determined by :func:`eval`
8187db96d56Sopenharmony_ciin the ``logging`` package's namespace). The ``level`` is interpreted as for
8197db96d56Sopenharmony_ciloggers, and ``NOTSET`` is taken to mean 'log everything'.
8207db96d56Sopenharmony_ci
8217db96d56Sopenharmony_ciThe ``formatter`` entry indicates the key name of the formatter for this
8227db96d56Sopenharmony_cihandler. If blank, a default formatter (``logging._defaultFormatter``) is used.
8237db96d56Sopenharmony_ciIf a name is specified, it must appear in the ``[formatters]`` section and have
8247db96d56Sopenharmony_cia corresponding section in the configuration file.
8257db96d56Sopenharmony_ci
8267db96d56Sopenharmony_ciThe ``args`` entry, when :ref:`evaluated <func-eval>` in the context of the ``logging``
8277db96d56Sopenharmony_cipackage's namespace, is the list of arguments to the constructor for the handler
8287db96d56Sopenharmony_ciclass. Refer to the constructors for the relevant handlers, or to the examples
8297db96d56Sopenharmony_cibelow, to see how typical entries are constructed. If not provided, it defaults
8307db96d56Sopenharmony_cito ``()``.
8317db96d56Sopenharmony_ci
8327db96d56Sopenharmony_ciThe optional ``kwargs`` entry, when :ref:`evaluated <func-eval>` in the context of the
8337db96d56Sopenharmony_ci``logging`` package's namespace, is the keyword argument dict to the constructor
8347db96d56Sopenharmony_cifor the handler class. If not provided, it defaults to ``{}``.
8357db96d56Sopenharmony_ci
8367db96d56Sopenharmony_ci.. code-block:: ini
8377db96d56Sopenharmony_ci
8387db96d56Sopenharmony_ci   [handler_hand02]
8397db96d56Sopenharmony_ci   class=FileHandler
8407db96d56Sopenharmony_ci   level=DEBUG
8417db96d56Sopenharmony_ci   formatter=form02
8427db96d56Sopenharmony_ci   args=('python.log', 'w')
8437db96d56Sopenharmony_ci
8447db96d56Sopenharmony_ci   [handler_hand03]
8457db96d56Sopenharmony_ci   class=handlers.SocketHandler
8467db96d56Sopenharmony_ci   level=INFO
8477db96d56Sopenharmony_ci   formatter=form03
8487db96d56Sopenharmony_ci   args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)
8497db96d56Sopenharmony_ci
8507db96d56Sopenharmony_ci   [handler_hand04]
8517db96d56Sopenharmony_ci   class=handlers.DatagramHandler
8527db96d56Sopenharmony_ci   level=WARN
8537db96d56Sopenharmony_ci   formatter=form04
8547db96d56Sopenharmony_ci   args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT)
8557db96d56Sopenharmony_ci
8567db96d56Sopenharmony_ci   [handler_hand05]
8577db96d56Sopenharmony_ci   class=handlers.SysLogHandler
8587db96d56Sopenharmony_ci   level=ERROR
8597db96d56Sopenharmony_ci   formatter=form05
8607db96d56Sopenharmony_ci   args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
8617db96d56Sopenharmony_ci
8627db96d56Sopenharmony_ci   [handler_hand06]
8637db96d56Sopenharmony_ci   class=handlers.NTEventLogHandler
8647db96d56Sopenharmony_ci   level=CRITICAL
8657db96d56Sopenharmony_ci   formatter=form06
8667db96d56Sopenharmony_ci   args=('Python Application', '', 'Application')
8677db96d56Sopenharmony_ci
8687db96d56Sopenharmony_ci   [handler_hand07]
8697db96d56Sopenharmony_ci   class=handlers.SMTPHandler
8707db96d56Sopenharmony_ci   level=WARN
8717db96d56Sopenharmony_ci   formatter=form07
8727db96d56Sopenharmony_ci   args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')
8737db96d56Sopenharmony_ci   kwargs={'timeout': 10.0}
8747db96d56Sopenharmony_ci
8757db96d56Sopenharmony_ci   [handler_hand08]
8767db96d56Sopenharmony_ci   class=handlers.MemoryHandler
8777db96d56Sopenharmony_ci   level=NOTSET
8787db96d56Sopenharmony_ci   formatter=form08
8797db96d56Sopenharmony_ci   target=
8807db96d56Sopenharmony_ci   args=(10, ERROR)
8817db96d56Sopenharmony_ci
8827db96d56Sopenharmony_ci   [handler_hand09]
8837db96d56Sopenharmony_ci   class=handlers.HTTPHandler
8847db96d56Sopenharmony_ci   level=NOTSET
8857db96d56Sopenharmony_ci   formatter=form09
8867db96d56Sopenharmony_ci   args=('localhost:9022', '/log', 'GET')
8877db96d56Sopenharmony_ci   kwargs={'secure': True}
8887db96d56Sopenharmony_ci
8897db96d56Sopenharmony_ciSections which specify formatter configuration are typified by the following.
8907db96d56Sopenharmony_ci
8917db96d56Sopenharmony_ci.. code-block:: ini
8927db96d56Sopenharmony_ci
8937db96d56Sopenharmony_ci   [formatter_form01]
8947db96d56Sopenharmony_ci   format=F1 %(asctime)s %(levelname)s %(message)s
8957db96d56Sopenharmony_ci   datefmt=
8967db96d56Sopenharmony_ci   style=%
8977db96d56Sopenharmony_ci   validate=True
8987db96d56Sopenharmony_ci   class=logging.Formatter
8997db96d56Sopenharmony_ci
9007db96d56Sopenharmony_ciThe arguments for the formatter configuration are the same as the keys
9017db96d56Sopenharmony_ciin the dictionary schema :ref:`formatters section
9027db96d56Sopenharmony_ci<logging-config-dictschema-formatters>`.
9037db96d56Sopenharmony_ci
9047db96d56Sopenharmony_ci.. note::
9057db96d56Sopenharmony_ci
9067db96d56Sopenharmony_ci   Due to the use of :func:`eval` as described above, there are
9077db96d56Sopenharmony_ci   potential security risks which result from using the :func:`listen` to send
9087db96d56Sopenharmony_ci   and receive configurations via sockets. The risks are limited to where
9097db96d56Sopenharmony_ci   multiple users with no mutual trust run code on the same machine; see the
9107db96d56Sopenharmony_ci   :func:`listen` documentation for more information.
9117db96d56Sopenharmony_ci
9127db96d56Sopenharmony_ci.. seealso::
9137db96d56Sopenharmony_ci
9147db96d56Sopenharmony_ci   Module :mod:`logging`
9157db96d56Sopenharmony_ci      API reference for the logging module.
9167db96d56Sopenharmony_ci
9177db96d56Sopenharmony_ci   Module :mod:`logging.handlers`
9187db96d56Sopenharmony_ci      Useful handlers included with the logging module.
919