17db96d56Sopenharmony_ci:mod:`logging` --- Logging facility for Python 27db96d56Sopenharmony_ci============================================== 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: logging 57db96d56Sopenharmony_ci :synopsis: Flexible event logging system for applications. 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/__init__.py` 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci.. index:: pair: Errors; logging 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci.. sidebar:: Important 157db96d56Sopenharmony_ci 167db96d56Sopenharmony_ci This page contains the API reference information. For tutorial 177db96d56Sopenharmony_ci information and discussion of more advanced topics, see 187db96d56Sopenharmony_ci 197db96d56Sopenharmony_ci * :ref:`Basic Tutorial <logging-basic-tutorial>` 207db96d56Sopenharmony_ci * :ref:`Advanced Tutorial <logging-advanced-tutorial>` 217db96d56Sopenharmony_ci * :ref:`Logging Cookbook <logging-cookbook>` 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ci-------------- 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ciThis module defines functions and classes which implement a flexible event 267db96d56Sopenharmony_cilogging system for applications and libraries. 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ciThe key benefit of having the logging API provided by a standard library module 297db96d56Sopenharmony_ciis that all Python modules can participate in logging, so your application log 307db96d56Sopenharmony_cican include your own messages integrated with messages from third-party 317db96d56Sopenharmony_cimodules. 327db96d56Sopenharmony_ci 337db96d56Sopenharmony_ciThe simplest example: 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ci.. code-block:: none 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ci >>> import logging 387db96d56Sopenharmony_ci >>> logging.warning('Watch out!') 397db96d56Sopenharmony_ci WARNING:root:Watch out! 407db96d56Sopenharmony_ci 417db96d56Sopenharmony_ciThe module provides a lot of functionality and flexibility. If you are 427db96d56Sopenharmony_ciunfamiliar with logging, the best way to get to grips with it is to view the 437db96d56Sopenharmony_citutorials (**see the links above and on the right**). 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ciThe basic classes defined by the module, together with their functions, are 467db96d56Sopenharmony_cilisted below. 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ci* Loggers expose the interface that application code directly uses. 497db96d56Sopenharmony_ci* Handlers send the log records (created by loggers) to the appropriate 507db96d56Sopenharmony_ci destination. 517db96d56Sopenharmony_ci* Filters provide a finer grained facility for determining which log records 527db96d56Sopenharmony_ci to output. 537db96d56Sopenharmony_ci* Formatters specify the layout of log records in the final output. 547db96d56Sopenharmony_ci 557db96d56Sopenharmony_ci 567db96d56Sopenharmony_ci.. _logger: 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ciLogger Objects 597db96d56Sopenharmony_ci-------------- 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ciLoggers have the following attributes and methods. Note that Loggers should 627db96d56Sopenharmony_ci*NEVER* be instantiated directly, but always through the module-level function 637db96d56Sopenharmony_ci``logging.getLogger(name)``. Multiple calls to :func:`getLogger` with the same 647db96d56Sopenharmony_ciname will always return a reference to the same Logger object. 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ciThe ``name`` is potentially a period-separated hierarchical value, like 677db96d56Sopenharmony_ci``foo.bar.baz`` (though it could also be just plain ``foo``, for example). 687db96d56Sopenharmony_ciLoggers that are further down in the hierarchical list are children of loggers 697db96d56Sopenharmony_cihigher up in the list. For example, given a logger with a name of ``foo``, 707db96d56Sopenharmony_ciloggers with names of ``foo.bar``, ``foo.bar.baz``, and ``foo.bam`` are all 717db96d56Sopenharmony_cidescendants of ``foo``. The logger name hierarchy is analogous to the Python 727db96d56Sopenharmony_cipackage hierarchy, and identical to it if you organise your loggers on a 737db96d56Sopenharmony_ciper-module basis using the recommended construction 747db96d56Sopenharmony_ci``logging.getLogger(__name__)``. That's because in a module, ``__name__`` 757db96d56Sopenharmony_ciis the module's name in the Python package namespace. 767db96d56Sopenharmony_ci 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci.. class:: Logger 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ci .. attribute:: Logger.propagate 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ci If this attribute evaluates to true, events logged to this logger will be 837db96d56Sopenharmony_ci passed to the handlers of higher level (ancestor) loggers, in addition to 847db96d56Sopenharmony_ci any handlers attached to this logger. Messages are passed directly to the 857db96d56Sopenharmony_ci ancestor loggers' handlers - neither the level nor filters of the ancestor 867db96d56Sopenharmony_ci loggers in question are considered. 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ci If this evaluates to false, logging messages are not passed to the handlers 897db96d56Sopenharmony_ci of ancestor loggers. 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci Spelling it out with an example: If the propagate attribute of the logger named 927db96d56Sopenharmony_ci ``A.B.C`` evaluates to true, any event logged to ``A.B.C`` via a method call such as 937db96d56Sopenharmony_ci ``logging.getLogger('A.B.C').error(...)`` will [subject to passing that logger's 947db96d56Sopenharmony_ci level and filter settings] be passed in turn to any handlers attached to loggers 957db96d56Sopenharmony_ci named ``A.B``, ``A`` and the root logger, after first being passed to any handlers 967db96d56Sopenharmony_ci attached to ``A.B.C``. If any logger in the chain ``A.B.C``, ``A.B``, ``A`` has its 977db96d56Sopenharmony_ci ``propagate`` attribute set to false, then that is the last logger whose handlers 987db96d56Sopenharmony_ci are offered the event to handle, and propagation stops at that point. 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ci The constructor sets this attribute to ``True``. 1017db96d56Sopenharmony_ci 1027db96d56Sopenharmony_ci .. note:: If you attach a handler to a logger *and* one or more of its 1037db96d56Sopenharmony_ci ancestors, it may emit the same record multiple times. In general, you 1047db96d56Sopenharmony_ci should not need to attach a handler to more than one logger - if you just 1057db96d56Sopenharmony_ci attach it to the appropriate logger which is highest in the logger 1067db96d56Sopenharmony_ci hierarchy, then it will see all events logged by all descendant loggers, 1077db96d56Sopenharmony_ci provided that their propagate setting is left set to ``True``. A common 1087db96d56Sopenharmony_ci scenario is to attach handlers only to the root logger, and to let 1097db96d56Sopenharmony_ci propagation take care of the rest. 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ci .. method:: Logger.setLevel(level) 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci Sets the threshold for this logger to *level*. Logging messages which are less 1147db96d56Sopenharmony_ci severe than *level* will be ignored; logging messages which have severity *level* 1157db96d56Sopenharmony_ci or higher will be emitted by whichever handler or handlers service this logger, 1167db96d56Sopenharmony_ci unless a handler's level has been set to a higher severity level than *level*. 1177db96d56Sopenharmony_ci 1187db96d56Sopenharmony_ci When a logger is created, the level is set to :const:`NOTSET` (which causes 1197db96d56Sopenharmony_ci all messages to be processed when the logger is the root logger, or delegation 1207db96d56Sopenharmony_ci to the parent when the logger is a non-root logger). Note that the root logger 1217db96d56Sopenharmony_ci is created with level :const:`WARNING`. 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_ci The term 'delegation to the parent' means that if a logger has a level of 1247db96d56Sopenharmony_ci NOTSET, its chain of ancestor loggers is traversed until either an ancestor with 1257db96d56Sopenharmony_ci a level other than NOTSET is found, or the root is reached. 1267db96d56Sopenharmony_ci 1277db96d56Sopenharmony_ci If an ancestor is found with a level other than NOTSET, then that ancestor's 1287db96d56Sopenharmony_ci level is treated as the effective level of the logger where the ancestor search 1297db96d56Sopenharmony_ci began, and is used to determine how a logging event is handled. 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ci If the root is reached, and it has a level of NOTSET, then all messages will be 1327db96d56Sopenharmony_ci processed. Otherwise, the root's level will be used as the effective level. 1337db96d56Sopenharmony_ci 1347db96d56Sopenharmony_ci See :ref:`levels` for a list of levels. 1357db96d56Sopenharmony_ci 1367db96d56Sopenharmony_ci .. versionchanged:: 3.2 1377db96d56Sopenharmony_ci The *level* parameter now accepts a string representation of the 1387db96d56Sopenharmony_ci level such as 'INFO' as an alternative to the integer constants 1397db96d56Sopenharmony_ci such as :const:`INFO`. Note, however, that levels are internally stored 1407db96d56Sopenharmony_ci as integers, and methods such as e.g. :meth:`getEffectiveLevel` and 1417db96d56Sopenharmony_ci :meth:`isEnabledFor` will return/expect to be passed integers. 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci .. method:: Logger.isEnabledFor(level) 1457db96d56Sopenharmony_ci 1467db96d56Sopenharmony_ci Indicates if a message of severity *level* would be processed by this logger. 1477db96d56Sopenharmony_ci This method checks first the module-level level set by 1487db96d56Sopenharmony_ci ``logging.disable(level)`` and then the logger's effective level as determined 1497db96d56Sopenharmony_ci by :meth:`getEffectiveLevel`. 1507db96d56Sopenharmony_ci 1517db96d56Sopenharmony_ci 1527db96d56Sopenharmony_ci .. method:: Logger.getEffectiveLevel() 1537db96d56Sopenharmony_ci 1547db96d56Sopenharmony_ci Indicates the effective level for this logger. If a value other than 1557db96d56Sopenharmony_ci :const:`NOTSET` has been set using :meth:`setLevel`, it is returned. Otherwise, 1567db96d56Sopenharmony_ci the hierarchy is traversed towards the root until a value other than 1577db96d56Sopenharmony_ci :const:`NOTSET` is found, and that value is returned. The value returned is 1587db96d56Sopenharmony_ci an integer, typically one of :const:`logging.DEBUG`, :const:`logging.INFO` 1597db96d56Sopenharmony_ci etc. 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ci 1627db96d56Sopenharmony_ci .. method:: Logger.getChild(suffix) 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci Returns a logger which is a descendant to this logger, as determined by the suffix. 1657db96d56Sopenharmony_ci Thus, ``logging.getLogger('abc').getChild('def.ghi')`` would return the same 1667db96d56Sopenharmony_ci logger as would be returned by ``logging.getLogger('abc.def.ghi')``. This is a 1677db96d56Sopenharmony_ci convenience method, useful when the parent logger is named using e.g. ``__name__`` 1687db96d56Sopenharmony_ci rather than a literal string. 1697db96d56Sopenharmony_ci 1707db96d56Sopenharmony_ci .. versionadded:: 3.2 1717db96d56Sopenharmony_ci 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci .. method:: Logger.debug(msg, *args, **kwargs) 1747db96d56Sopenharmony_ci 1757db96d56Sopenharmony_ci Logs a message with level :const:`DEBUG` on this logger. The *msg* is the 1767db96d56Sopenharmony_ci message format string, and the *args* are the arguments which are merged into 1777db96d56Sopenharmony_ci *msg* using the string formatting operator. (Note that this means that you can 1787db96d56Sopenharmony_ci use keywords in the format string, together with a single dictionary argument.) 1797db96d56Sopenharmony_ci No % formatting operation is performed on *msg* when no *args* are supplied. 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ci There are four keyword arguments in *kwargs* which are inspected: 1827db96d56Sopenharmony_ci *exc_info*, *stack_info*, *stacklevel* and *extra*. 1837db96d56Sopenharmony_ci 1847db96d56Sopenharmony_ci If *exc_info* does not evaluate as false, it causes exception information to be 1857db96d56Sopenharmony_ci added to the logging message. If an exception tuple (in the format returned by 1867db96d56Sopenharmony_ci :func:`sys.exc_info`) or an exception instance is provided, it is used; 1877db96d56Sopenharmony_ci otherwise, :func:`sys.exc_info` is called to get the exception information. 1887db96d56Sopenharmony_ci 1897db96d56Sopenharmony_ci The second optional keyword argument is *stack_info*, which defaults to 1907db96d56Sopenharmony_ci ``False``. If true, stack information is added to the logging 1917db96d56Sopenharmony_ci message, including the actual logging call. Note that this is not the same 1927db96d56Sopenharmony_ci stack information as that displayed through specifying *exc_info*: The 1937db96d56Sopenharmony_ci former is stack frames from the bottom of the stack up to the logging call 1947db96d56Sopenharmony_ci in the current thread, whereas the latter is information about stack frames 1957db96d56Sopenharmony_ci which have been unwound, following an exception, while searching for 1967db96d56Sopenharmony_ci exception handlers. 1977db96d56Sopenharmony_ci 1987db96d56Sopenharmony_ci You can specify *stack_info* independently of *exc_info*, e.g. to just show 1997db96d56Sopenharmony_ci how you got to a certain point in your code, even when no exceptions were 2007db96d56Sopenharmony_ci raised. The stack frames are printed following a header line which says: 2017db96d56Sopenharmony_ci 2027db96d56Sopenharmony_ci .. code-block:: none 2037db96d56Sopenharmony_ci 2047db96d56Sopenharmony_ci Stack (most recent call last): 2057db96d56Sopenharmony_ci 2067db96d56Sopenharmony_ci This mimics the ``Traceback (most recent call last):`` which is used when 2077db96d56Sopenharmony_ci displaying exception frames. 2087db96d56Sopenharmony_ci 2097db96d56Sopenharmony_ci The third optional keyword argument is *stacklevel*, which defaults to ``1``. 2107db96d56Sopenharmony_ci If greater than 1, the corresponding number of stack frames are skipped 2117db96d56Sopenharmony_ci when computing the line number and function name set in the :class:`LogRecord` 2127db96d56Sopenharmony_ci created for the logging event. This can be used in logging helpers so that 2137db96d56Sopenharmony_ci the function name, filename and line number recorded are not the information 2147db96d56Sopenharmony_ci for the helper function/method, but rather its caller. The name of this 2157db96d56Sopenharmony_ci parameter mirrors the equivalent one in the :mod:`warnings` module. 2167db96d56Sopenharmony_ci 2177db96d56Sopenharmony_ci The fourth keyword argument is *extra* which can be used to pass a 2187db96d56Sopenharmony_ci dictionary which is used to populate the __dict__ of the :class:`LogRecord` 2197db96d56Sopenharmony_ci created for the logging event with user-defined attributes. These custom 2207db96d56Sopenharmony_ci attributes can then be used as you like. For example, they could be 2217db96d56Sopenharmony_ci incorporated into logged messages. For example:: 2227db96d56Sopenharmony_ci 2237db96d56Sopenharmony_ci FORMAT = '%(asctime)s %(clientip)-15s %(user)-8s %(message)s' 2247db96d56Sopenharmony_ci logging.basicConfig(format=FORMAT) 2257db96d56Sopenharmony_ci d = {'clientip': '192.168.0.1', 'user': 'fbloggs'} 2267db96d56Sopenharmony_ci logger = logging.getLogger('tcpserver') 2277db96d56Sopenharmony_ci logger.warning('Protocol problem: %s', 'connection reset', extra=d) 2287db96d56Sopenharmony_ci 2297db96d56Sopenharmony_ci would print something like 2307db96d56Sopenharmony_ci 2317db96d56Sopenharmony_ci .. code-block:: none 2327db96d56Sopenharmony_ci 2337db96d56Sopenharmony_ci 2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset 2347db96d56Sopenharmony_ci 2357db96d56Sopenharmony_ci The keys in the dictionary passed in *extra* should not clash with the keys used 2367db96d56Sopenharmony_ci by the logging system. (See the section on :ref:`logrecord-attributes` for more 2377db96d56Sopenharmony_ci information on which keys are used by the logging system.) 2387db96d56Sopenharmony_ci 2397db96d56Sopenharmony_ci If you choose to use these attributes in logged messages, you need to exercise 2407db96d56Sopenharmony_ci some care. In the above example, for instance, the :class:`Formatter` has been 2417db96d56Sopenharmony_ci set up with a format string which expects 'clientip' and 'user' in the attribute 2427db96d56Sopenharmony_ci dictionary of the :class:`LogRecord`. If these are missing, the message will 2437db96d56Sopenharmony_ci not be logged because a string formatting exception will occur. So in this case, 2447db96d56Sopenharmony_ci you always need to pass the *extra* dictionary with these keys. 2457db96d56Sopenharmony_ci 2467db96d56Sopenharmony_ci While this might be annoying, this feature is intended for use in specialized 2477db96d56Sopenharmony_ci circumstances, such as multi-threaded servers where the same code executes in 2487db96d56Sopenharmony_ci many contexts, and interesting conditions which arise are dependent on this 2497db96d56Sopenharmony_ci context (such as remote client IP address and authenticated user name, in the 2507db96d56Sopenharmony_ci above example). In such circumstances, it is likely that specialized 2517db96d56Sopenharmony_ci :class:`Formatter`\ s would be used with particular :class:`Handler`\ s. 2527db96d56Sopenharmony_ci 2537db96d56Sopenharmony_ci If no handler is attached to this logger (or any of its ancestors, 2547db96d56Sopenharmony_ci taking into account the relevant :attr:`Logger.propagate` attributes), 2557db96d56Sopenharmony_ci the message will be sent to the handler set on :attr:`lastResort`. 2567db96d56Sopenharmony_ci 2577db96d56Sopenharmony_ci .. versionchanged:: 3.2 2587db96d56Sopenharmony_ci The *stack_info* parameter was added. 2597db96d56Sopenharmony_ci 2607db96d56Sopenharmony_ci .. versionchanged:: 3.5 2617db96d56Sopenharmony_ci The *exc_info* parameter can now accept exception instances. 2627db96d56Sopenharmony_ci 2637db96d56Sopenharmony_ci .. versionchanged:: 3.8 2647db96d56Sopenharmony_ci The *stacklevel* parameter was added. 2657db96d56Sopenharmony_ci 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ci .. method:: Logger.info(msg, *args, **kwargs) 2687db96d56Sopenharmony_ci 2697db96d56Sopenharmony_ci Logs a message with level :const:`INFO` on this logger. The arguments are 2707db96d56Sopenharmony_ci interpreted as for :meth:`debug`. 2717db96d56Sopenharmony_ci 2727db96d56Sopenharmony_ci 2737db96d56Sopenharmony_ci .. method:: Logger.warning(msg, *args, **kwargs) 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ci Logs a message with level :const:`WARNING` on this logger. The arguments are 2767db96d56Sopenharmony_ci interpreted as for :meth:`debug`. 2777db96d56Sopenharmony_ci 2787db96d56Sopenharmony_ci .. note:: There is an obsolete method ``warn`` which is functionally 2797db96d56Sopenharmony_ci identical to ``warning``. As ``warn`` is deprecated, please do not use 2807db96d56Sopenharmony_ci it - use ``warning`` instead. 2817db96d56Sopenharmony_ci 2827db96d56Sopenharmony_ci .. method:: Logger.error(msg, *args, **kwargs) 2837db96d56Sopenharmony_ci 2847db96d56Sopenharmony_ci Logs a message with level :const:`ERROR` on this logger. The arguments are 2857db96d56Sopenharmony_ci interpreted as for :meth:`debug`. 2867db96d56Sopenharmony_ci 2877db96d56Sopenharmony_ci 2887db96d56Sopenharmony_ci .. method:: Logger.critical(msg, *args, **kwargs) 2897db96d56Sopenharmony_ci 2907db96d56Sopenharmony_ci Logs a message with level :const:`CRITICAL` on this logger. The arguments are 2917db96d56Sopenharmony_ci interpreted as for :meth:`debug`. 2927db96d56Sopenharmony_ci 2937db96d56Sopenharmony_ci 2947db96d56Sopenharmony_ci .. method:: Logger.log(level, msg, *args, **kwargs) 2957db96d56Sopenharmony_ci 2967db96d56Sopenharmony_ci Logs a message with integer level *level* on this logger. The other arguments are 2977db96d56Sopenharmony_ci interpreted as for :meth:`debug`. 2987db96d56Sopenharmony_ci 2997db96d56Sopenharmony_ci 3007db96d56Sopenharmony_ci .. method:: Logger.exception(msg, *args, **kwargs) 3017db96d56Sopenharmony_ci 3027db96d56Sopenharmony_ci Logs a message with level :const:`ERROR` on this logger. The arguments are 3037db96d56Sopenharmony_ci interpreted as for :meth:`debug`. Exception info is added to the logging 3047db96d56Sopenharmony_ci message. This method should only be called from an exception handler. 3057db96d56Sopenharmony_ci 3067db96d56Sopenharmony_ci 3077db96d56Sopenharmony_ci .. method:: Logger.addFilter(filter) 3087db96d56Sopenharmony_ci 3097db96d56Sopenharmony_ci Adds the specified filter *filter* to this logger. 3107db96d56Sopenharmony_ci 3117db96d56Sopenharmony_ci 3127db96d56Sopenharmony_ci .. method:: Logger.removeFilter(filter) 3137db96d56Sopenharmony_ci 3147db96d56Sopenharmony_ci Removes the specified filter *filter* from this logger. 3157db96d56Sopenharmony_ci 3167db96d56Sopenharmony_ci 3177db96d56Sopenharmony_ci .. method:: Logger.filter(record) 3187db96d56Sopenharmony_ci 3197db96d56Sopenharmony_ci Apply this logger's filters to the record and return ``True`` if the 3207db96d56Sopenharmony_ci record is to be processed. The filters are consulted in turn, until one of 3217db96d56Sopenharmony_ci them returns a false value. If none of them return a false value, the record 3227db96d56Sopenharmony_ci will be processed (passed to handlers). If one returns a false value, no 3237db96d56Sopenharmony_ci further processing of the record occurs. 3247db96d56Sopenharmony_ci 3257db96d56Sopenharmony_ci 3267db96d56Sopenharmony_ci .. method:: Logger.addHandler(hdlr) 3277db96d56Sopenharmony_ci 3287db96d56Sopenharmony_ci Adds the specified handler *hdlr* to this logger. 3297db96d56Sopenharmony_ci 3307db96d56Sopenharmony_ci 3317db96d56Sopenharmony_ci .. method:: Logger.removeHandler(hdlr) 3327db96d56Sopenharmony_ci 3337db96d56Sopenharmony_ci Removes the specified handler *hdlr* from this logger. 3347db96d56Sopenharmony_ci 3357db96d56Sopenharmony_ci 3367db96d56Sopenharmony_ci .. method:: Logger.findCaller(stack_info=False, stacklevel=1) 3377db96d56Sopenharmony_ci 3387db96d56Sopenharmony_ci Finds the caller's source filename and line number. Returns the filename, line 3397db96d56Sopenharmony_ci number, function name and stack information as a 4-element tuple. The stack 3407db96d56Sopenharmony_ci information is returned as ``None`` unless *stack_info* is ``True``. 3417db96d56Sopenharmony_ci 3427db96d56Sopenharmony_ci The *stacklevel* parameter is passed from code calling the :meth:`debug` 3437db96d56Sopenharmony_ci and other APIs. If greater than 1, the excess is used to skip stack frames 3447db96d56Sopenharmony_ci before determining the values to be returned. This will generally be useful 3457db96d56Sopenharmony_ci when calling logging APIs from helper/wrapper code, so that the information 3467db96d56Sopenharmony_ci in the event log refers not to the helper/wrapper code, but to the code that 3477db96d56Sopenharmony_ci calls it. 3487db96d56Sopenharmony_ci 3497db96d56Sopenharmony_ci 3507db96d56Sopenharmony_ci .. method:: Logger.handle(record) 3517db96d56Sopenharmony_ci 3527db96d56Sopenharmony_ci Handles a record by passing it to all handlers associated with this logger and 3537db96d56Sopenharmony_ci its ancestors (until a false value of *propagate* is found). This method is used 3547db96d56Sopenharmony_ci for unpickled records received from a socket, as well as those created locally. 3557db96d56Sopenharmony_ci Logger-level filtering is applied using :meth:`~Logger.filter`. 3567db96d56Sopenharmony_ci 3577db96d56Sopenharmony_ci 3587db96d56Sopenharmony_ci .. method:: Logger.makeRecord(name, level, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None) 3597db96d56Sopenharmony_ci 3607db96d56Sopenharmony_ci This is a factory method which can be overridden in subclasses to create 3617db96d56Sopenharmony_ci specialized :class:`LogRecord` instances. 3627db96d56Sopenharmony_ci 3637db96d56Sopenharmony_ci .. method:: Logger.hasHandlers() 3647db96d56Sopenharmony_ci 3657db96d56Sopenharmony_ci Checks to see if this logger has any handlers configured. This is done by 3667db96d56Sopenharmony_ci looking for handlers in this logger and its parents in the logger hierarchy. 3677db96d56Sopenharmony_ci Returns ``True`` if a handler was found, else ``False``. The method stops searching 3687db96d56Sopenharmony_ci up the hierarchy whenever a logger with the 'propagate' attribute set to 3697db96d56Sopenharmony_ci false is found - that will be the last logger which is checked for the 3707db96d56Sopenharmony_ci existence of handlers. 3717db96d56Sopenharmony_ci 3727db96d56Sopenharmony_ci .. versionadded:: 3.2 3737db96d56Sopenharmony_ci 3747db96d56Sopenharmony_ci .. versionchanged:: 3.7 3757db96d56Sopenharmony_ci Loggers can now be pickled and unpickled. 3767db96d56Sopenharmony_ci 3777db96d56Sopenharmony_ci.. _levels: 3787db96d56Sopenharmony_ci 3797db96d56Sopenharmony_ciLogging Levels 3807db96d56Sopenharmony_ci-------------- 3817db96d56Sopenharmony_ci 3827db96d56Sopenharmony_ciThe numeric values of logging levels are given in the following table. These are 3837db96d56Sopenharmony_ciprimarily of interest if you want to define your own levels, and need them to 3847db96d56Sopenharmony_cihave specific values relative to the predefined levels. If you define a level 3857db96d56Sopenharmony_ciwith the same numeric value, it overwrites the predefined value; the predefined 3867db96d56Sopenharmony_ciname is lost. 3877db96d56Sopenharmony_ci 3887db96d56Sopenharmony_ci+--------------+---------------+ 3897db96d56Sopenharmony_ci| Level | Numeric value | 3907db96d56Sopenharmony_ci+==============+===============+ 3917db96d56Sopenharmony_ci| ``CRITICAL`` | 50 | 3927db96d56Sopenharmony_ci+--------------+---------------+ 3937db96d56Sopenharmony_ci| ``ERROR`` | 40 | 3947db96d56Sopenharmony_ci+--------------+---------------+ 3957db96d56Sopenharmony_ci| ``WARNING`` | 30 | 3967db96d56Sopenharmony_ci+--------------+---------------+ 3977db96d56Sopenharmony_ci| ``INFO`` | 20 | 3987db96d56Sopenharmony_ci+--------------+---------------+ 3997db96d56Sopenharmony_ci| ``DEBUG`` | 10 | 4007db96d56Sopenharmony_ci+--------------+---------------+ 4017db96d56Sopenharmony_ci| ``NOTSET`` | 0 | 4027db96d56Sopenharmony_ci+--------------+---------------+ 4037db96d56Sopenharmony_ci 4047db96d56Sopenharmony_ci 4057db96d56Sopenharmony_ci.. _handler: 4067db96d56Sopenharmony_ci 4077db96d56Sopenharmony_ciHandler Objects 4087db96d56Sopenharmony_ci--------------- 4097db96d56Sopenharmony_ci 4107db96d56Sopenharmony_ciHandlers have the following attributes and methods. Note that :class:`Handler` 4117db96d56Sopenharmony_ciis never instantiated directly; this class acts as a base for more useful 4127db96d56Sopenharmony_cisubclasses. However, the :meth:`__init__` method in subclasses needs to call 4137db96d56Sopenharmony_ci:meth:`Handler.__init__`. 4147db96d56Sopenharmony_ci 4157db96d56Sopenharmony_ci.. class:: Handler 4167db96d56Sopenharmony_ci 4177db96d56Sopenharmony_ci .. method:: Handler.__init__(level=NOTSET) 4187db96d56Sopenharmony_ci 4197db96d56Sopenharmony_ci Initializes the :class:`Handler` instance by setting its level, setting the list 4207db96d56Sopenharmony_ci of filters to the empty list and creating a lock (using :meth:`createLock`) for 4217db96d56Sopenharmony_ci serializing access to an I/O mechanism. 4227db96d56Sopenharmony_ci 4237db96d56Sopenharmony_ci 4247db96d56Sopenharmony_ci .. method:: Handler.createLock() 4257db96d56Sopenharmony_ci 4267db96d56Sopenharmony_ci Initializes a thread lock which can be used to serialize access to underlying 4277db96d56Sopenharmony_ci I/O functionality which may not be threadsafe. 4287db96d56Sopenharmony_ci 4297db96d56Sopenharmony_ci 4307db96d56Sopenharmony_ci .. method:: Handler.acquire() 4317db96d56Sopenharmony_ci 4327db96d56Sopenharmony_ci Acquires the thread lock created with :meth:`createLock`. 4337db96d56Sopenharmony_ci 4347db96d56Sopenharmony_ci 4357db96d56Sopenharmony_ci .. method:: Handler.release() 4367db96d56Sopenharmony_ci 4377db96d56Sopenharmony_ci Releases the thread lock acquired with :meth:`acquire`. 4387db96d56Sopenharmony_ci 4397db96d56Sopenharmony_ci 4407db96d56Sopenharmony_ci .. method:: Handler.setLevel(level) 4417db96d56Sopenharmony_ci 4427db96d56Sopenharmony_ci Sets the threshold for this handler to *level*. Logging messages which are 4437db96d56Sopenharmony_ci less severe than *level* will be ignored. When a handler is created, the 4447db96d56Sopenharmony_ci level is set to :const:`NOTSET` (which causes all messages to be 4457db96d56Sopenharmony_ci processed). 4467db96d56Sopenharmony_ci 4477db96d56Sopenharmony_ci See :ref:`levels` for a list of levels. 4487db96d56Sopenharmony_ci 4497db96d56Sopenharmony_ci .. versionchanged:: 3.2 4507db96d56Sopenharmony_ci The *level* parameter now accepts a string representation of the 4517db96d56Sopenharmony_ci level such as 'INFO' as an alternative to the integer constants 4527db96d56Sopenharmony_ci such as :const:`INFO`. 4537db96d56Sopenharmony_ci 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ci .. method:: Handler.setFormatter(fmt) 4567db96d56Sopenharmony_ci 4577db96d56Sopenharmony_ci Sets the :class:`Formatter` for this handler to *fmt*. 4587db96d56Sopenharmony_ci 4597db96d56Sopenharmony_ci 4607db96d56Sopenharmony_ci .. method:: Handler.addFilter(filter) 4617db96d56Sopenharmony_ci 4627db96d56Sopenharmony_ci Adds the specified filter *filter* to this handler. 4637db96d56Sopenharmony_ci 4647db96d56Sopenharmony_ci 4657db96d56Sopenharmony_ci .. method:: Handler.removeFilter(filter) 4667db96d56Sopenharmony_ci 4677db96d56Sopenharmony_ci Removes the specified filter *filter* from this handler. 4687db96d56Sopenharmony_ci 4697db96d56Sopenharmony_ci 4707db96d56Sopenharmony_ci .. method:: Handler.filter(record) 4717db96d56Sopenharmony_ci 4727db96d56Sopenharmony_ci Apply this handler's filters to the record and return ``True`` if the 4737db96d56Sopenharmony_ci record is to be processed. The filters are consulted in turn, until one of 4747db96d56Sopenharmony_ci them returns a false value. If none of them return a false value, the record 4757db96d56Sopenharmony_ci will be emitted. If one returns a false value, the handler will not emit the 4767db96d56Sopenharmony_ci record. 4777db96d56Sopenharmony_ci 4787db96d56Sopenharmony_ci 4797db96d56Sopenharmony_ci .. method:: Handler.flush() 4807db96d56Sopenharmony_ci 4817db96d56Sopenharmony_ci Ensure all logging output has been flushed. This version does nothing and is 4827db96d56Sopenharmony_ci intended to be implemented by subclasses. 4837db96d56Sopenharmony_ci 4847db96d56Sopenharmony_ci 4857db96d56Sopenharmony_ci .. method:: Handler.close() 4867db96d56Sopenharmony_ci 4877db96d56Sopenharmony_ci Tidy up any resources used by the handler. This version does no output but 4887db96d56Sopenharmony_ci removes the handler from an internal list of handlers which is closed when 4897db96d56Sopenharmony_ci :func:`shutdown` is called. Subclasses should ensure that this gets called 4907db96d56Sopenharmony_ci from overridden :meth:`close` methods. 4917db96d56Sopenharmony_ci 4927db96d56Sopenharmony_ci 4937db96d56Sopenharmony_ci .. method:: Handler.handle(record) 4947db96d56Sopenharmony_ci 4957db96d56Sopenharmony_ci Conditionally emits the specified logging record, depending on filters which may 4967db96d56Sopenharmony_ci have been added to the handler. Wraps the actual emission of the record with 4977db96d56Sopenharmony_ci acquisition/release of the I/O thread lock. 4987db96d56Sopenharmony_ci 4997db96d56Sopenharmony_ci 5007db96d56Sopenharmony_ci .. method:: Handler.handleError(record) 5017db96d56Sopenharmony_ci 5027db96d56Sopenharmony_ci This method should be called from handlers when an exception is encountered 5037db96d56Sopenharmony_ci during an :meth:`emit` call. If the module-level attribute 5047db96d56Sopenharmony_ci ``raiseExceptions`` is ``False``, exceptions get silently ignored. This is 5057db96d56Sopenharmony_ci what is mostly wanted for a logging system - most users will not care about 5067db96d56Sopenharmony_ci errors in the logging system, they are more interested in application 5077db96d56Sopenharmony_ci errors. You could, however, replace this with a custom handler if you wish. 5087db96d56Sopenharmony_ci The specified record is the one which was being processed when the exception 5097db96d56Sopenharmony_ci occurred. (The default value of ``raiseExceptions`` is ``True``, as that is 5107db96d56Sopenharmony_ci more useful during development). 5117db96d56Sopenharmony_ci 5127db96d56Sopenharmony_ci 5137db96d56Sopenharmony_ci .. method:: Handler.format(record) 5147db96d56Sopenharmony_ci 5157db96d56Sopenharmony_ci Do formatting for a record - if a formatter is set, use it. Otherwise, use the 5167db96d56Sopenharmony_ci default formatter for the module. 5177db96d56Sopenharmony_ci 5187db96d56Sopenharmony_ci 5197db96d56Sopenharmony_ci .. method:: Handler.emit(record) 5207db96d56Sopenharmony_ci 5217db96d56Sopenharmony_ci Do whatever it takes to actually log the specified logging record. This version 5227db96d56Sopenharmony_ci is intended to be implemented by subclasses and so raises a 5237db96d56Sopenharmony_ci :exc:`NotImplementedError`. 5247db96d56Sopenharmony_ci 5257db96d56Sopenharmony_ci .. warning:: This method is called after a handler-level lock is acquired, which 5267db96d56Sopenharmony_ci is released after this method returns. When you override this method, note 5277db96d56Sopenharmony_ci that you should be careful when calling anything that invokes other parts of 5287db96d56Sopenharmony_ci the logging API which might do locking, because that might result in a 5297db96d56Sopenharmony_ci deadlock. Specifically: 5307db96d56Sopenharmony_ci 5317db96d56Sopenharmony_ci * Logging configuration APIs acquire the module-level lock, and then 5327db96d56Sopenharmony_ci individual handler-level locks as those handlers are configured. 5337db96d56Sopenharmony_ci 5347db96d56Sopenharmony_ci * Many logging APIs lock the module-level lock. If such an API is called 5357db96d56Sopenharmony_ci from this method, it could cause a deadlock if a configuration call is 5367db96d56Sopenharmony_ci made on another thread, because that thread will try to acquire the 5377db96d56Sopenharmony_ci module-level lock *before* the handler-level lock, whereas this thread 5387db96d56Sopenharmony_ci tries to acquire the module-level lock *after* the handler-level lock 5397db96d56Sopenharmony_ci (because in this method, the handler-level lock has already been acquired). 5407db96d56Sopenharmony_ci 5417db96d56Sopenharmony_ciFor a list of handlers included as standard, see :mod:`logging.handlers`. 5427db96d56Sopenharmony_ci 5437db96d56Sopenharmony_ci.. _formatter-objects: 5447db96d56Sopenharmony_ci 5457db96d56Sopenharmony_ciFormatter Objects 5467db96d56Sopenharmony_ci----------------- 5477db96d56Sopenharmony_ci 5487db96d56Sopenharmony_ci.. currentmodule:: logging 5497db96d56Sopenharmony_ci 5507db96d56Sopenharmony_ci:class:`Formatter` objects have the following attributes and methods. They are 5517db96d56Sopenharmony_ciresponsible for converting a :class:`LogRecord` to (usually) a string which can 5527db96d56Sopenharmony_cibe interpreted by either a human or an external system. The base 5537db96d56Sopenharmony_ci:class:`Formatter` allows a formatting string to be specified. If none is 5547db96d56Sopenharmony_cisupplied, the default value of ``'%(message)s'`` is used, which just includes 5557db96d56Sopenharmony_cithe message in the logging call. To have additional items of information in the 5567db96d56Sopenharmony_ciformatted output (such as a timestamp), keep reading. 5577db96d56Sopenharmony_ci 5587db96d56Sopenharmony_ciA Formatter can be initialized with a format string which makes use of knowledge 5597db96d56Sopenharmony_ciof the :class:`LogRecord` attributes - such as the default value mentioned above 5607db96d56Sopenharmony_cimaking use of the fact that the user's message and arguments are pre-formatted 5617db96d56Sopenharmony_ciinto a :class:`LogRecord`'s *message* attribute. This format string contains 5627db96d56Sopenharmony_cistandard Python %-style mapping keys. See section :ref:`old-string-formatting` 5637db96d56Sopenharmony_cifor more information on string formatting. 5647db96d56Sopenharmony_ci 5657db96d56Sopenharmony_ciThe useful mapping keys in a :class:`LogRecord` are given in the section on 5667db96d56Sopenharmony_ci:ref:`logrecord-attributes`. 5677db96d56Sopenharmony_ci 5687db96d56Sopenharmony_ci 5697db96d56Sopenharmony_ci.. class:: Formatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None) 5707db96d56Sopenharmony_ci 5717db96d56Sopenharmony_ci Returns a new instance of the :class:`Formatter` class. The instance is 5727db96d56Sopenharmony_ci initialized with a format string for the message as a whole, as well as a 5737db96d56Sopenharmony_ci format string for the date/time portion of a message. If no *fmt* is 5747db96d56Sopenharmony_ci specified, ``'%(message)s'`` is used. If no *datefmt* is specified, a format 5757db96d56Sopenharmony_ci is used which is described in the :meth:`formatTime` documentation. 5767db96d56Sopenharmony_ci 5777db96d56Sopenharmony_ci The *style* parameter can be one of '%', '{' or '$' and determines how 5787db96d56Sopenharmony_ci the format string will be merged with its data: using one of %-formatting, 5797db96d56Sopenharmony_ci :meth:`str.format` or :class:`string.Template`. This only applies to the 5807db96d56Sopenharmony_ci format string *fmt* (e.g. ``'%(message)s'`` or ``{message}``), not to the 5817db96d56Sopenharmony_ci actual log messages passed to ``Logger.debug`` etc; see 5827db96d56Sopenharmony_ci :ref:`formatting-styles` for more information on using {- and $-formatting 5837db96d56Sopenharmony_ci for log messages. 5847db96d56Sopenharmony_ci 5857db96d56Sopenharmony_ci The *defaults* parameter can be a dictionary with default values to use in 5867db96d56Sopenharmony_ci custom fields. For example: 5877db96d56Sopenharmony_ci ``logging.Formatter('%(ip)s %(message)s', defaults={"ip": None})`` 5887db96d56Sopenharmony_ci 5897db96d56Sopenharmony_ci .. versionchanged:: 3.2 5907db96d56Sopenharmony_ci The *style* parameter was added. 5917db96d56Sopenharmony_ci 5927db96d56Sopenharmony_ci .. versionchanged:: 3.8 5937db96d56Sopenharmony_ci The *validate* parameter was added. Incorrect or mismatched style and fmt 5947db96d56Sopenharmony_ci will raise a ``ValueError``. 5957db96d56Sopenharmony_ci For example: ``logging.Formatter('%(asctime)s - %(message)s', style='{')``. 5967db96d56Sopenharmony_ci 5977db96d56Sopenharmony_ci .. versionchanged:: 3.10 5987db96d56Sopenharmony_ci The *defaults* parameter was added. 5997db96d56Sopenharmony_ci 6007db96d56Sopenharmony_ci .. method:: format(record) 6017db96d56Sopenharmony_ci 6027db96d56Sopenharmony_ci The record's attribute dictionary is used as the operand to a string 6037db96d56Sopenharmony_ci formatting operation. Returns the resulting string. Before formatting the 6047db96d56Sopenharmony_ci dictionary, a couple of preparatory steps are carried out. The *message* 6057db96d56Sopenharmony_ci attribute of the record is computed using *msg* % *args*. If the 6067db96d56Sopenharmony_ci formatting string contains ``'(asctime)'``, :meth:`formatTime` is called 6077db96d56Sopenharmony_ci to format the event time. If there is exception information, it is 6087db96d56Sopenharmony_ci formatted using :meth:`formatException` and appended to the message. Note 6097db96d56Sopenharmony_ci that the formatted exception information is cached in attribute 6107db96d56Sopenharmony_ci *exc_text*. This is useful because the exception information can be 6117db96d56Sopenharmony_ci pickled and sent across the wire, but you should be careful if you have 6127db96d56Sopenharmony_ci more than one :class:`Formatter` subclass which customizes the formatting 6137db96d56Sopenharmony_ci of exception information. In this case, you will have to clear the cached 6147db96d56Sopenharmony_ci value (by setting the *exc_text* attribute to ``None``) after a formatter 6157db96d56Sopenharmony_ci has done its formatting, so that the next formatter to handle the event 6167db96d56Sopenharmony_ci doesn't use the cached value, but recalculates it afresh. 6177db96d56Sopenharmony_ci 6187db96d56Sopenharmony_ci If stack information is available, it's appended after the exception 6197db96d56Sopenharmony_ci information, using :meth:`formatStack` to transform it if necessary. 6207db96d56Sopenharmony_ci 6217db96d56Sopenharmony_ci 6227db96d56Sopenharmony_ci .. method:: formatTime(record, datefmt=None) 6237db96d56Sopenharmony_ci 6247db96d56Sopenharmony_ci This method should be called from :meth:`format` by a formatter which 6257db96d56Sopenharmony_ci wants to make use of a formatted time. This method can be overridden in 6267db96d56Sopenharmony_ci formatters to provide for any specific requirement, but the basic behavior 6277db96d56Sopenharmony_ci is as follows: if *datefmt* (a string) is specified, it is used with 6287db96d56Sopenharmony_ci :func:`time.strftime` to format the creation time of the 6297db96d56Sopenharmony_ci record. Otherwise, the format '%Y-%m-%d %H:%M:%S,uuu' is used, where the 6307db96d56Sopenharmony_ci uuu part is a millisecond value and the other letters are as per the 6317db96d56Sopenharmony_ci :func:`time.strftime` documentation. An example time in this format is 6327db96d56Sopenharmony_ci ``2003-01-23 00:29:50,411``. The resulting string is returned. 6337db96d56Sopenharmony_ci 6347db96d56Sopenharmony_ci This function uses a user-configurable function to convert the creation 6357db96d56Sopenharmony_ci time to a tuple. By default, :func:`time.localtime` is used; to change 6367db96d56Sopenharmony_ci this for a particular formatter instance, set the ``converter`` attribute 6377db96d56Sopenharmony_ci to a function with the same signature as :func:`time.localtime` or 6387db96d56Sopenharmony_ci :func:`time.gmtime`. To change it for all formatters, for example if you 6397db96d56Sopenharmony_ci want all logging times to be shown in GMT, set the ``converter`` 6407db96d56Sopenharmony_ci attribute in the ``Formatter`` class. 6417db96d56Sopenharmony_ci 6427db96d56Sopenharmony_ci .. versionchanged:: 3.3 6437db96d56Sopenharmony_ci Previously, the default format was hard-coded as in this example: 6447db96d56Sopenharmony_ci ``2010-09-06 22:38:15,292`` where the part before the comma is 6457db96d56Sopenharmony_ci handled by a strptime format string (``'%Y-%m-%d %H:%M:%S'``), and the 6467db96d56Sopenharmony_ci part after the comma is a millisecond value. Because strptime does not 6477db96d56Sopenharmony_ci have a format placeholder for milliseconds, the millisecond value is 6487db96d56Sopenharmony_ci appended using another format string, ``'%s,%03d'`` --- and both of these 6497db96d56Sopenharmony_ci format strings have been hardcoded into this method. With the change, 6507db96d56Sopenharmony_ci these strings are defined as class-level attributes which can be 6517db96d56Sopenharmony_ci overridden at the instance level when desired. The names of the 6527db96d56Sopenharmony_ci attributes are ``default_time_format`` (for the strptime format string) 6537db96d56Sopenharmony_ci and ``default_msec_format`` (for appending the millisecond value). 6547db96d56Sopenharmony_ci 6557db96d56Sopenharmony_ci .. versionchanged:: 3.9 6567db96d56Sopenharmony_ci The ``default_msec_format`` can be ``None``. 6577db96d56Sopenharmony_ci 6587db96d56Sopenharmony_ci .. method:: formatException(exc_info) 6597db96d56Sopenharmony_ci 6607db96d56Sopenharmony_ci Formats the specified exception information (a standard exception tuple as 6617db96d56Sopenharmony_ci returned by :func:`sys.exc_info`) as a string. This default implementation 6627db96d56Sopenharmony_ci just uses :func:`traceback.print_exception`. The resulting string is 6637db96d56Sopenharmony_ci returned. 6647db96d56Sopenharmony_ci 6657db96d56Sopenharmony_ci .. method:: formatStack(stack_info) 6667db96d56Sopenharmony_ci 6677db96d56Sopenharmony_ci Formats the specified stack information (a string as returned by 6687db96d56Sopenharmony_ci :func:`traceback.print_stack`, but with the last newline removed) as a 6697db96d56Sopenharmony_ci string. This default implementation just returns the input value. 6707db96d56Sopenharmony_ci 6717db96d56Sopenharmony_ci.. class:: BufferingFormatter(linefmt=None) 6727db96d56Sopenharmony_ci 6737db96d56Sopenharmony_ci A base formatter class suitable for subclassing when you want to format a 6747db96d56Sopenharmony_ci number of records. You can pass a :class:`Formatter` instance which you want 6757db96d56Sopenharmony_ci to use to format each line (that corresponds to a single record). If not 6767db96d56Sopenharmony_ci specified, the default formatter (which just outputs the event message) is 6777db96d56Sopenharmony_ci used as the line formatter. 6787db96d56Sopenharmony_ci 6797db96d56Sopenharmony_ci .. method:: formatHeader(records) 6807db96d56Sopenharmony_ci 6817db96d56Sopenharmony_ci Return a header for a list of *records*. The base implementation just 6827db96d56Sopenharmony_ci returns the empty string. You will need to override this method if you 6837db96d56Sopenharmony_ci want specific behaviour, e.g. to show the count of records, a title or a 6847db96d56Sopenharmony_ci separator line. 6857db96d56Sopenharmony_ci 6867db96d56Sopenharmony_ci .. method:: formatFooter(records) 6877db96d56Sopenharmony_ci 6887db96d56Sopenharmony_ci Return a footer for a list of *records*. The base implementation just 6897db96d56Sopenharmony_ci returns the empty string. You will need to override this method if you 6907db96d56Sopenharmony_ci want specific behaviour, e.g. to show the count of records or a separator 6917db96d56Sopenharmony_ci line. 6927db96d56Sopenharmony_ci 6937db96d56Sopenharmony_ci .. method:: format(records) 6947db96d56Sopenharmony_ci 6957db96d56Sopenharmony_ci Return formatted text for a list of *records*. The base implementation 6967db96d56Sopenharmony_ci just returns the empty string if there are no records; otherwise, it 6977db96d56Sopenharmony_ci returns the concatenation of the header, each record formatted with the 6987db96d56Sopenharmony_ci line formatter, and the footer. 6997db96d56Sopenharmony_ci 7007db96d56Sopenharmony_ci.. _filter: 7017db96d56Sopenharmony_ci 7027db96d56Sopenharmony_ciFilter Objects 7037db96d56Sopenharmony_ci-------------- 7047db96d56Sopenharmony_ci 7057db96d56Sopenharmony_ci``Filters`` can be used by ``Handlers`` and ``Loggers`` for more sophisticated 7067db96d56Sopenharmony_cifiltering than is provided by levels. The base filter class only allows events 7077db96d56Sopenharmony_ciwhich are below a certain point in the logger hierarchy. For example, a filter 7087db96d56Sopenharmony_ciinitialized with 'A.B' will allow events logged by loggers 'A.B', 'A.B.C', 7097db96d56Sopenharmony_ci'A.B.C.D', 'A.B.D' etc. but not 'A.BB', 'B.A.B' etc. If initialized with the 7107db96d56Sopenharmony_ciempty string, all events are passed. 7117db96d56Sopenharmony_ci 7127db96d56Sopenharmony_ci 7137db96d56Sopenharmony_ci.. class:: Filter(name='') 7147db96d56Sopenharmony_ci 7157db96d56Sopenharmony_ci Returns an instance of the :class:`Filter` class. If *name* is specified, it 7167db96d56Sopenharmony_ci names a logger which, together with its children, will have its events allowed 7177db96d56Sopenharmony_ci through the filter. If *name* is the empty string, allows every event. 7187db96d56Sopenharmony_ci 7197db96d56Sopenharmony_ci 7207db96d56Sopenharmony_ci .. method:: filter(record) 7217db96d56Sopenharmony_ci 7227db96d56Sopenharmony_ci Is the specified record to be logged? Returns zero for no, nonzero for 7237db96d56Sopenharmony_ci yes. If deemed appropriate, the record may be modified in-place by this 7247db96d56Sopenharmony_ci method. 7257db96d56Sopenharmony_ci 7267db96d56Sopenharmony_ciNote that filters attached to handlers are consulted before an event is 7277db96d56Sopenharmony_ciemitted by the handler, whereas filters attached to loggers are consulted 7287db96d56Sopenharmony_ciwhenever an event is logged (using :meth:`debug`, :meth:`info`, 7297db96d56Sopenharmony_cietc.), before sending an event to handlers. This means that events which have 7307db96d56Sopenharmony_cibeen generated by descendant loggers will not be filtered by a logger's filter 7317db96d56Sopenharmony_cisetting, unless the filter has also been applied to those descendant loggers. 7327db96d56Sopenharmony_ci 7337db96d56Sopenharmony_ciYou don't actually need to subclass ``Filter``: you can pass any instance 7347db96d56Sopenharmony_ciwhich has a ``filter`` method with the same semantics. 7357db96d56Sopenharmony_ci 7367db96d56Sopenharmony_ci.. versionchanged:: 3.2 7377db96d56Sopenharmony_ci You don't need to create specialized ``Filter`` classes, or use other 7387db96d56Sopenharmony_ci classes with a ``filter`` method: you can use a function (or other 7397db96d56Sopenharmony_ci callable) as a filter. The filtering logic will check to see if the filter 7407db96d56Sopenharmony_ci object has a ``filter`` attribute: if it does, it's assumed to be a 7417db96d56Sopenharmony_ci ``Filter`` and its :meth:`~Filter.filter` method is called. Otherwise, it's 7427db96d56Sopenharmony_ci assumed to be a callable and called with the record as the single 7437db96d56Sopenharmony_ci parameter. The returned value should conform to that returned by 7447db96d56Sopenharmony_ci :meth:`~Filter.filter`. 7457db96d56Sopenharmony_ci 7467db96d56Sopenharmony_ciAlthough filters are used primarily to filter records based on more 7477db96d56Sopenharmony_cisophisticated criteria than levels, they get to see every record which is 7487db96d56Sopenharmony_ciprocessed by the handler or logger they're attached to: this can be useful if 7497db96d56Sopenharmony_ciyou want to do things like counting how many records were processed by a 7507db96d56Sopenharmony_ciparticular logger or handler, or adding, changing or removing attributes in 7517db96d56Sopenharmony_cithe :class:`LogRecord` being processed. Obviously changing the LogRecord needs 7527db96d56Sopenharmony_cito be done with some care, but it does allow the injection of contextual 7537db96d56Sopenharmony_ciinformation into logs (see :ref:`filters-contextual`). 7547db96d56Sopenharmony_ci 7557db96d56Sopenharmony_ci 7567db96d56Sopenharmony_ci.. _log-record: 7577db96d56Sopenharmony_ci 7587db96d56Sopenharmony_ciLogRecord Objects 7597db96d56Sopenharmony_ci----------------- 7607db96d56Sopenharmony_ci 7617db96d56Sopenharmony_ci:class:`LogRecord` instances are created automatically by the :class:`Logger` 7627db96d56Sopenharmony_cievery time something is logged, and can be created manually via 7637db96d56Sopenharmony_ci:func:`makeLogRecord` (for example, from a pickled event received over the 7647db96d56Sopenharmony_ciwire). 7657db96d56Sopenharmony_ci 7667db96d56Sopenharmony_ci 7677db96d56Sopenharmony_ci.. class:: LogRecord(name, level, pathname, lineno, msg, args, exc_info, func=None, sinfo=None) 7687db96d56Sopenharmony_ci 7697db96d56Sopenharmony_ci Contains all the information pertinent to the event being logged. 7707db96d56Sopenharmony_ci 7717db96d56Sopenharmony_ci The primary information is passed in *msg* and *args*, 7727db96d56Sopenharmony_ci which are combined using ``msg % args`` to create 7737db96d56Sopenharmony_ci the :attr:`!message` attribute of the record. 7747db96d56Sopenharmony_ci 7757db96d56Sopenharmony_ci :param name: The name of the logger used to log the event 7767db96d56Sopenharmony_ci represented by this :class:`!LogRecord`. 7777db96d56Sopenharmony_ci Note that the logger name in the :class:`!LogRecord` 7787db96d56Sopenharmony_ci will always have this value, 7797db96d56Sopenharmony_ci even though it may be emitted by a handler 7807db96d56Sopenharmony_ci attached to a different (ancestor) logger. 7817db96d56Sopenharmony_ci :type name: str 7827db96d56Sopenharmony_ci 7837db96d56Sopenharmony_ci :param level: The :ref:`numeric level <levels>` of the logging event 7847db96d56Sopenharmony_ci (such as ``10`` for ``DEBUG``, ``20`` for ``INFO``, etc). 7857db96d56Sopenharmony_ci Note that this is converted to *two* attributes of the LogRecord: 7867db96d56Sopenharmony_ci :attr:`!levelno` for the numeric value 7877db96d56Sopenharmony_ci and :attr:`!levelname` for the corresponding level name. 7887db96d56Sopenharmony_ci :type level: int 7897db96d56Sopenharmony_ci 7907db96d56Sopenharmony_ci :param pathname: The full string path of the source file 7917db96d56Sopenharmony_ci where the logging call was made. 7927db96d56Sopenharmony_ci :type pathname: str 7937db96d56Sopenharmony_ci 7947db96d56Sopenharmony_ci :param lineno: The line number in the source file 7957db96d56Sopenharmony_ci where the logging call was made. 7967db96d56Sopenharmony_ci :type lineno: int 7977db96d56Sopenharmony_ci 7987db96d56Sopenharmony_ci :param msg: The event description message, 7997db96d56Sopenharmony_ci which can be a %-format string with placeholders for variable data, 8007db96d56Sopenharmony_ci or an arbitrary object (see :ref:`arbitrary-object-messages`). 8017db96d56Sopenharmony_ci :type msg: typing.Any 8027db96d56Sopenharmony_ci 8037db96d56Sopenharmony_ci :param args: Variable data to merge into the *msg* argument 8047db96d56Sopenharmony_ci to obtain the event description. 8057db96d56Sopenharmony_ci :type args: tuple | dict[str, typing.Any] 8067db96d56Sopenharmony_ci 8077db96d56Sopenharmony_ci :param exc_info: An exception tuple with the current exception information, 8087db96d56Sopenharmony_ci as returned by :func:`sys.exc_info`, 8097db96d56Sopenharmony_ci or ``None`` if no exception information is available. 8107db96d56Sopenharmony_ci :type exc_info: tuple[type[BaseException], BaseException, types.TracebackType] | None 8117db96d56Sopenharmony_ci 8127db96d56Sopenharmony_ci :param func: The name of the function or method 8137db96d56Sopenharmony_ci from which the logging call was invoked. 8147db96d56Sopenharmony_ci :type func: str | None 8157db96d56Sopenharmony_ci 8167db96d56Sopenharmony_ci :param sinfo: A text string representing stack information 8177db96d56Sopenharmony_ci from the base of the stack in the current thread, 8187db96d56Sopenharmony_ci up to the logging call. 8197db96d56Sopenharmony_ci :type sinfo: str | None 8207db96d56Sopenharmony_ci 8217db96d56Sopenharmony_ci .. method:: getMessage() 8227db96d56Sopenharmony_ci 8237db96d56Sopenharmony_ci Returns the message for this :class:`LogRecord` instance after merging any 8247db96d56Sopenharmony_ci user-supplied arguments with the message. If the user-supplied message 8257db96d56Sopenharmony_ci argument to the logging call is not a string, :func:`str` is called on it to 8267db96d56Sopenharmony_ci convert it to a string. This allows use of user-defined classes as 8277db96d56Sopenharmony_ci messages, whose ``__str__`` method can return the actual format string to 8287db96d56Sopenharmony_ci be used. 8297db96d56Sopenharmony_ci 8307db96d56Sopenharmony_ci .. versionchanged:: 3.2 8317db96d56Sopenharmony_ci The creation of a :class:`LogRecord` has been made more configurable by 8327db96d56Sopenharmony_ci providing a factory which is used to create the record. The factory can be 8337db96d56Sopenharmony_ci set using :func:`getLogRecordFactory` and :func:`setLogRecordFactory` 8347db96d56Sopenharmony_ci (see this for the factory's signature). 8357db96d56Sopenharmony_ci 8367db96d56Sopenharmony_ci This functionality can be used to inject your own values into a 8377db96d56Sopenharmony_ci :class:`LogRecord` at creation time. You can use the following pattern:: 8387db96d56Sopenharmony_ci 8397db96d56Sopenharmony_ci old_factory = logging.getLogRecordFactory() 8407db96d56Sopenharmony_ci 8417db96d56Sopenharmony_ci def record_factory(*args, **kwargs): 8427db96d56Sopenharmony_ci record = old_factory(*args, **kwargs) 8437db96d56Sopenharmony_ci record.custom_attribute = 0xdecafbad 8447db96d56Sopenharmony_ci return record 8457db96d56Sopenharmony_ci 8467db96d56Sopenharmony_ci logging.setLogRecordFactory(record_factory) 8477db96d56Sopenharmony_ci 8487db96d56Sopenharmony_ci With this pattern, multiple factories could be chained, and as long 8497db96d56Sopenharmony_ci as they don't overwrite each other's attributes or unintentionally 8507db96d56Sopenharmony_ci overwrite the standard attributes listed above, there should be no 8517db96d56Sopenharmony_ci surprises. 8527db96d56Sopenharmony_ci 8537db96d56Sopenharmony_ci 8547db96d56Sopenharmony_ci.. _logrecord-attributes: 8557db96d56Sopenharmony_ci 8567db96d56Sopenharmony_ciLogRecord attributes 8577db96d56Sopenharmony_ci-------------------- 8587db96d56Sopenharmony_ci 8597db96d56Sopenharmony_ciThe LogRecord has a number of attributes, most of which are derived from the 8607db96d56Sopenharmony_ciparameters to the constructor. (Note that the names do not always correspond 8617db96d56Sopenharmony_ciexactly between the LogRecord constructor parameters and the LogRecord 8627db96d56Sopenharmony_ciattributes.) These attributes can be used to merge data from the record into 8637db96d56Sopenharmony_cithe format string. The following table lists (in alphabetical order) the 8647db96d56Sopenharmony_ciattribute names, their meanings and the corresponding placeholder in a %-style 8657db96d56Sopenharmony_ciformat string. 8667db96d56Sopenharmony_ci 8677db96d56Sopenharmony_ciIf you are using {}-formatting (:func:`str.format`), you can use 8687db96d56Sopenharmony_ci``{attrname}`` as the placeholder in the format string. If you are using 8697db96d56Sopenharmony_ci$-formatting (:class:`string.Template`), use the form ``${attrname}``. In 8707db96d56Sopenharmony_ciboth cases, of course, replace ``attrname`` with the actual attribute name 8717db96d56Sopenharmony_ciyou want to use. 8727db96d56Sopenharmony_ci 8737db96d56Sopenharmony_ciIn the case of {}-formatting, you can specify formatting flags by placing them 8747db96d56Sopenharmony_ciafter the attribute name, separated from it with a colon. For example: a 8757db96d56Sopenharmony_ciplaceholder of ``{msecs:03d}`` would format a millisecond value of ``4`` as 8767db96d56Sopenharmony_ci``004``. Refer to the :meth:`str.format` documentation for full details on 8777db96d56Sopenharmony_cithe options available to you. 8787db96d56Sopenharmony_ci 8797db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 8807db96d56Sopenharmony_ci| Attribute name | Format | Description | 8817db96d56Sopenharmony_ci+================+=========================+===============================================+ 8827db96d56Sopenharmony_ci| args | You shouldn't need to | The tuple of arguments merged into ``msg`` to | 8837db96d56Sopenharmony_ci| | format this yourself. | produce ``message``, or a dict whose values | 8847db96d56Sopenharmony_ci| | | are used for the merge (when there is only one| 8857db96d56Sopenharmony_ci| | | argument, and it is a dictionary). | 8867db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 8877db96d56Sopenharmony_ci| asctime | ``%(asctime)s`` | Human-readable time when the | 8887db96d56Sopenharmony_ci| | | :class:`LogRecord` was created. By default | 8897db96d56Sopenharmony_ci| | | this is of the form '2003-07-08 16:49:45,896' | 8907db96d56Sopenharmony_ci| | | (the numbers after the comma are millisecond | 8917db96d56Sopenharmony_ci| | | portion of the time). | 8927db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 8937db96d56Sopenharmony_ci| created | ``%(created)f`` | Time when the :class:`LogRecord` was created | 8947db96d56Sopenharmony_ci| | | (as returned by :func:`time.time`). | 8957db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 8967db96d56Sopenharmony_ci| exc_info | You shouldn't need to | Exception tuple (à la ``sys.exc_info``) or, | 8977db96d56Sopenharmony_ci| | format this yourself. | if no exception has occurred, ``None``. | 8987db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 8997db96d56Sopenharmony_ci| filename | ``%(filename)s`` | Filename portion of ``pathname``. | 9007db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9017db96d56Sopenharmony_ci| funcName | ``%(funcName)s`` | Name of function containing the logging call. | 9027db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9037db96d56Sopenharmony_ci| levelname | ``%(levelname)s`` | Text logging level for the message | 9047db96d56Sopenharmony_ci| | | (``'DEBUG'``, ``'INFO'``, ``'WARNING'``, | 9057db96d56Sopenharmony_ci| | | ``'ERROR'``, ``'CRITICAL'``). | 9067db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9077db96d56Sopenharmony_ci| levelno | ``%(levelno)s`` | Numeric logging level for the message | 9087db96d56Sopenharmony_ci| | | (:const:`DEBUG`, :const:`INFO`, | 9097db96d56Sopenharmony_ci| | | :const:`WARNING`, :const:`ERROR`, | 9107db96d56Sopenharmony_ci| | | :const:`CRITICAL`). | 9117db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9127db96d56Sopenharmony_ci| lineno | ``%(lineno)d`` | Source line number where the logging call was | 9137db96d56Sopenharmony_ci| | | issued (if available). | 9147db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9157db96d56Sopenharmony_ci| message | ``%(message)s`` | The logged message, computed as ``msg % | 9167db96d56Sopenharmony_ci| | | args``. This is set when | 9177db96d56Sopenharmony_ci| | | :meth:`Formatter.format` is invoked. | 9187db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9197db96d56Sopenharmony_ci| module | ``%(module)s`` | Module (name portion of ``filename``). | 9207db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9217db96d56Sopenharmony_ci| msecs | ``%(msecs)d`` | Millisecond portion of the time when the | 9227db96d56Sopenharmony_ci| | | :class:`LogRecord` was created. | 9237db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9247db96d56Sopenharmony_ci| msg | You shouldn't need to | The format string passed in the original | 9257db96d56Sopenharmony_ci| | format this yourself. | logging call. Merged with ``args`` to | 9267db96d56Sopenharmony_ci| | | produce ``message``, or an arbitrary object | 9277db96d56Sopenharmony_ci| | | (see :ref:`arbitrary-object-messages`). | 9287db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9297db96d56Sopenharmony_ci| name | ``%(name)s`` | Name of the logger used to log the call. | 9307db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9317db96d56Sopenharmony_ci| pathname | ``%(pathname)s`` | Full pathname of the source file where the | 9327db96d56Sopenharmony_ci| | | logging call was issued (if available). | 9337db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9347db96d56Sopenharmony_ci| process | ``%(process)d`` | Process ID (if available). | 9357db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9367db96d56Sopenharmony_ci| processName | ``%(processName)s`` | Process name (if available). | 9377db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9387db96d56Sopenharmony_ci| relativeCreated| ``%(relativeCreated)d`` | Time in milliseconds when the LogRecord was | 9397db96d56Sopenharmony_ci| | | created, relative to the time the logging | 9407db96d56Sopenharmony_ci| | | module was loaded. | 9417db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9427db96d56Sopenharmony_ci| stack_info | You shouldn't need to | Stack frame information (where available) | 9437db96d56Sopenharmony_ci| | format this yourself. | from the bottom of the stack in the current | 9447db96d56Sopenharmony_ci| | | thread, up to and including the stack frame | 9457db96d56Sopenharmony_ci| | | of the logging call which resulted in the | 9467db96d56Sopenharmony_ci| | | creation of this record. | 9477db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9487db96d56Sopenharmony_ci| thread | ``%(thread)d`` | Thread ID (if available). | 9497db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9507db96d56Sopenharmony_ci| threadName | ``%(threadName)s`` | Thread name (if available). | 9517db96d56Sopenharmony_ci+----------------+-------------------------+-----------------------------------------------+ 9527db96d56Sopenharmony_ci 9537db96d56Sopenharmony_ci.. versionchanged:: 3.1 9547db96d56Sopenharmony_ci *processName* was added. 9557db96d56Sopenharmony_ci 9567db96d56Sopenharmony_ci 9577db96d56Sopenharmony_ci.. _logger-adapter: 9587db96d56Sopenharmony_ci 9597db96d56Sopenharmony_ciLoggerAdapter Objects 9607db96d56Sopenharmony_ci--------------------- 9617db96d56Sopenharmony_ci 9627db96d56Sopenharmony_ci:class:`LoggerAdapter` instances are used to conveniently pass contextual 9637db96d56Sopenharmony_ciinformation into logging calls. For a usage example, see the section on 9647db96d56Sopenharmony_ci:ref:`adding contextual information to your logging output <context-info>`. 9657db96d56Sopenharmony_ci 9667db96d56Sopenharmony_ci.. class:: LoggerAdapter(logger, extra) 9677db96d56Sopenharmony_ci 9687db96d56Sopenharmony_ci Returns an instance of :class:`LoggerAdapter` initialized with an 9697db96d56Sopenharmony_ci underlying :class:`Logger` instance and a dict-like object. 9707db96d56Sopenharmony_ci 9717db96d56Sopenharmony_ci .. method:: process(msg, kwargs) 9727db96d56Sopenharmony_ci 9737db96d56Sopenharmony_ci Modifies the message and/or keyword arguments passed to a logging call in 9747db96d56Sopenharmony_ci order to insert contextual information. This implementation takes the object 9757db96d56Sopenharmony_ci passed as *extra* to the constructor and adds it to *kwargs* using key 9767db96d56Sopenharmony_ci 'extra'. The return value is a (*msg*, *kwargs*) tuple which has the 9777db96d56Sopenharmony_ci (possibly modified) versions of the arguments passed in. 9787db96d56Sopenharmony_ci 9797db96d56Sopenharmony_ciIn addition to the above, :class:`LoggerAdapter` supports the following 9807db96d56Sopenharmony_cimethods of :class:`Logger`: :meth:`~Logger.debug`, :meth:`~Logger.info`, 9817db96d56Sopenharmony_ci:meth:`~Logger.warning`, :meth:`~Logger.error`, :meth:`~Logger.exception`, 9827db96d56Sopenharmony_ci:meth:`~Logger.critical`, :meth:`~Logger.log`, :meth:`~Logger.isEnabledFor`, 9837db96d56Sopenharmony_ci:meth:`~Logger.getEffectiveLevel`, :meth:`~Logger.setLevel` and 9847db96d56Sopenharmony_ci:meth:`~Logger.hasHandlers`. These methods have the same signatures as their 9857db96d56Sopenharmony_cicounterparts in :class:`Logger`, so you can use the two types of instances 9867db96d56Sopenharmony_ciinterchangeably. 9877db96d56Sopenharmony_ci 9887db96d56Sopenharmony_ci.. versionchanged:: 3.2 9897db96d56Sopenharmony_ci The :meth:`~Logger.isEnabledFor`, :meth:`~Logger.getEffectiveLevel`, 9907db96d56Sopenharmony_ci :meth:`~Logger.setLevel` and :meth:`~Logger.hasHandlers` methods were added 9917db96d56Sopenharmony_ci to :class:`LoggerAdapter`. These methods delegate to the underlying logger. 9927db96d56Sopenharmony_ci 9937db96d56Sopenharmony_ci.. versionchanged:: 3.6 9947db96d56Sopenharmony_ci Attribute :attr:`manager` and method :meth:`_log` were added, which 9957db96d56Sopenharmony_ci delegate to the underlying logger and allow adapters to be nested. 9967db96d56Sopenharmony_ci 9977db96d56Sopenharmony_ci 9987db96d56Sopenharmony_ciThread Safety 9997db96d56Sopenharmony_ci------------- 10007db96d56Sopenharmony_ci 10017db96d56Sopenharmony_ciThe logging module is intended to be thread-safe without any special work 10027db96d56Sopenharmony_cineeding to be done by its clients. It achieves this though using threading 10037db96d56Sopenharmony_cilocks; there is one lock to serialize access to the module's shared data, and 10047db96d56Sopenharmony_cieach handler also creates a lock to serialize access to its underlying I/O. 10057db96d56Sopenharmony_ci 10067db96d56Sopenharmony_ciIf you are implementing asynchronous signal handlers using the :mod:`signal` 10077db96d56Sopenharmony_cimodule, you may not be able to use logging from within such handlers. This is 10087db96d56Sopenharmony_cibecause lock implementations in the :mod:`threading` module are not always 10097db96d56Sopenharmony_cire-entrant, and so cannot be invoked from such signal handlers. 10107db96d56Sopenharmony_ci 10117db96d56Sopenharmony_ci 10127db96d56Sopenharmony_ciModule-Level Functions 10137db96d56Sopenharmony_ci---------------------- 10147db96d56Sopenharmony_ci 10157db96d56Sopenharmony_ciIn addition to the classes described above, there are a number of module-level 10167db96d56Sopenharmony_cifunctions. 10177db96d56Sopenharmony_ci 10187db96d56Sopenharmony_ci 10197db96d56Sopenharmony_ci.. function:: getLogger(name=None) 10207db96d56Sopenharmony_ci 10217db96d56Sopenharmony_ci Return a logger with the specified name or, if name is ``None``, return a 10227db96d56Sopenharmony_ci logger which is the root logger of the hierarchy. If specified, the name is 10237db96d56Sopenharmony_ci typically a dot-separated hierarchical name like *'a'*, *'a.b'* or *'a.b.c.d'*. 10247db96d56Sopenharmony_ci Choice of these names is entirely up to the developer who is using logging. 10257db96d56Sopenharmony_ci 10267db96d56Sopenharmony_ci All calls to this function with a given name return the same logger instance. 10277db96d56Sopenharmony_ci This means that logger instances never need to be passed between different parts 10287db96d56Sopenharmony_ci of an application. 10297db96d56Sopenharmony_ci 10307db96d56Sopenharmony_ci 10317db96d56Sopenharmony_ci.. function:: getLoggerClass() 10327db96d56Sopenharmony_ci 10337db96d56Sopenharmony_ci Return either the standard :class:`Logger` class, or the last class passed to 10347db96d56Sopenharmony_ci :func:`setLoggerClass`. This function may be called from within a new class 10357db96d56Sopenharmony_ci definition, to ensure that installing a customized :class:`Logger` class will 10367db96d56Sopenharmony_ci not undo customizations already applied by other code. For example:: 10377db96d56Sopenharmony_ci 10387db96d56Sopenharmony_ci class MyLogger(logging.getLoggerClass()): 10397db96d56Sopenharmony_ci # ... override behaviour here 10407db96d56Sopenharmony_ci 10417db96d56Sopenharmony_ci 10427db96d56Sopenharmony_ci.. function:: getLogRecordFactory() 10437db96d56Sopenharmony_ci 10447db96d56Sopenharmony_ci Return a callable which is used to create a :class:`LogRecord`. 10457db96d56Sopenharmony_ci 10467db96d56Sopenharmony_ci .. versionadded:: 3.2 10477db96d56Sopenharmony_ci This function has been provided, along with :func:`setLogRecordFactory`, 10487db96d56Sopenharmony_ci to allow developers more control over how the :class:`LogRecord` 10497db96d56Sopenharmony_ci representing a logging event is constructed. 10507db96d56Sopenharmony_ci 10517db96d56Sopenharmony_ci See :func:`setLogRecordFactory` for more information about the how the 10527db96d56Sopenharmony_ci factory is called. 10537db96d56Sopenharmony_ci 10547db96d56Sopenharmony_ci.. function:: debug(msg, *args, **kwargs) 10557db96d56Sopenharmony_ci 10567db96d56Sopenharmony_ci Logs a message with level :const:`DEBUG` on the root logger. The *msg* is the 10577db96d56Sopenharmony_ci message format string, and the *args* are the arguments which are merged into 10587db96d56Sopenharmony_ci *msg* using the string formatting operator. (Note that this means that you can 10597db96d56Sopenharmony_ci use keywords in the format string, together with a single dictionary argument.) 10607db96d56Sopenharmony_ci 10617db96d56Sopenharmony_ci There are three keyword arguments in *kwargs* which are inspected: *exc_info* 10627db96d56Sopenharmony_ci which, if it does not evaluate as false, causes exception information to be 10637db96d56Sopenharmony_ci added to the logging message. If an exception tuple (in the format returned by 10647db96d56Sopenharmony_ci :func:`sys.exc_info`) or an exception instance is provided, it is used; 10657db96d56Sopenharmony_ci otherwise, :func:`sys.exc_info` is called to get the exception information. 10667db96d56Sopenharmony_ci 10677db96d56Sopenharmony_ci The second optional keyword argument is *stack_info*, which defaults to 10687db96d56Sopenharmony_ci ``False``. If true, stack information is added to the logging 10697db96d56Sopenharmony_ci message, including the actual logging call. Note that this is not the same 10707db96d56Sopenharmony_ci stack information as that displayed through specifying *exc_info*: The 10717db96d56Sopenharmony_ci former is stack frames from the bottom of the stack up to the logging call 10727db96d56Sopenharmony_ci in the current thread, whereas the latter is information about stack frames 10737db96d56Sopenharmony_ci which have been unwound, following an exception, while searching for 10747db96d56Sopenharmony_ci exception handlers. 10757db96d56Sopenharmony_ci 10767db96d56Sopenharmony_ci You can specify *stack_info* independently of *exc_info*, e.g. to just show 10777db96d56Sopenharmony_ci how you got to a certain point in your code, even when no exceptions were 10787db96d56Sopenharmony_ci raised. The stack frames are printed following a header line which says: 10797db96d56Sopenharmony_ci 10807db96d56Sopenharmony_ci .. code-block:: none 10817db96d56Sopenharmony_ci 10827db96d56Sopenharmony_ci Stack (most recent call last): 10837db96d56Sopenharmony_ci 10847db96d56Sopenharmony_ci This mimics the ``Traceback (most recent call last):`` which is used when 10857db96d56Sopenharmony_ci displaying exception frames. 10867db96d56Sopenharmony_ci 10877db96d56Sopenharmony_ci The third optional keyword argument is *extra* which can be used to pass a 10887db96d56Sopenharmony_ci dictionary which is used to populate the __dict__ of the LogRecord created for 10897db96d56Sopenharmony_ci the logging event with user-defined attributes. These custom attributes can then 10907db96d56Sopenharmony_ci be used as you like. For example, they could be incorporated into logged 10917db96d56Sopenharmony_ci messages. For example:: 10927db96d56Sopenharmony_ci 10937db96d56Sopenharmony_ci FORMAT = '%(asctime)s %(clientip)-15s %(user)-8s %(message)s' 10947db96d56Sopenharmony_ci logging.basicConfig(format=FORMAT) 10957db96d56Sopenharmony_ci d = {'clientip': '192.168.0.1', 'user': 'fbloggs'} 10967db96d56Sopenharmony_ci logging.warning('Protocol problem: %s', 'connection reset', extra=d) 10977db96d56Sopenharmony_ci 10987db96d56Sopenharmony_ci would print something like: 10997db96d56Sopenharmony_ci 11007db96d56Sopenharmony_ci .. code-block:: none 11017db96d56Sopenharmony_ci 11027db96d56Sopenharmony_ci 2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset 11037db96d56Sopenharmony_ci 11047db96d56Sopenharmony_ci The keys in the dictionary passed in *extra* should not clash with the keys used 11057db96d56Sopenharmony_ci by the logging system. (See the :class:`Formatter` documentation for more 11067db96d56Sopenharmony_ci information on which keys are used by the logging system.) 11077db96d56Sopenharmony_ci 11087db96d56Sopenharmony_ci If you choose to use these attributes in logged messages, you need to exercise 11097db96d56Sopenharmony_ci some care. In the above example, for instance, the :class:`Formatter` has been 11107db96d56Sopenharmony_ci set up with a format string which expects 'clientip' and 'user' in the attribute 11117db96d56Sopenharmony_ci dictionary of the LogRecord. If these are missing, the message will not be 11127db96d56Sopenharmony_ci logged because a string formatting exception will occur. So in this case, you 11137db96d56Sopenharmony_ci always need to pass the *extra* dictionary with these keys. 11147db96d56Sopenharmony_ci 11157db96d56Sopenharmony_ci While this might be annoying, this feature is intended for use in specialized 11167db96d56Sopenharmony_ci circumstances, such as multi-threaded servers where the same code executes in 11177db96d56Sopenharmony_ci many contexts, and interesting conditions which arise are dependent on this 11187db96d56Sopenharmony_ci context (such as remote client IP address and authenticated user name, in the 11197db96d56Sopenharmony_ci above example). In such circumstances, it is likely that specialized 11207db96d56Sopenharmony_ci :class:`Formatter`\ s would be used with particular :class:`Handler`\ s. 11217db96d56Sopenharmony_ci 11227db96d56Sopenharmony_ci This function (as well as :func:`info`, :func:`warning`, :func:`error` and 11237db96d56Sopenharmony_ci :func:`critical`) will call :func:`basicConfig` if the root logger doesn't 11247db96d56Sopenharmony_ci have any handler attached. 11257db96d56Sopenharmony_ci 11267db96d56Sopenharmony_ci .. versionchanged:: 3.2 11277db96d56Sopenharmony_ci The *stack_info* parameter was added. 11287db96d56Sopenharmony_ci 11297db96d56Sopenharmony_ci.. function:: info(msg, *args, **kwargs) 11307db96d56Sopenharmony_ci 11317db96d56Sopenharmony_ci Logs a message with level :const:`INFO` on the root logger. The arguments are 11327db96d56Sopenharmony_ci interpreted as for :func:`debug`. 11337db96d56Sopenharmony_ci 11347db96d56Sopenharmony_ci 11357db96d56Sopenharmony_ci.. function:: warning(msg, *args, **kwargs) 11367db96d56Sopenharmony_ci 11377db96d56Sopenharmony_ci Logs a message with level :const:`WARNING` on the root logger. The arguments 11387db96d56Sopenharmony_ci are interpreted as for :func:`debug`. 11397db96d56Sopenharmony_ci 11407db96d56Sopenharmony_ci .. note:: There is an obsolete function ``warn`` which is functionally 11417db96d56Sopenharmony_ci identical to ``warning``. As ``warn`` is deprecated, please do not use 11427db96d56Sopenharmony_ci it - use ``warning`` instead. 11437db96d56Sopenharmony_ci 11447db96d56Sopenharmony_ci 11457db96d56Sopenharmony_ci.. function:: error(msg, *args, **kwargs) 11467db96d56Sopenharmony_ci 11477db96d56Sopenharmony_ci Logs a message with level :const:`ERROR` on the root logger. The arguments are 11487db96d56Sopenharmony_ci interpreted as for :func:`debug`. 11497db96d56Sopenharmony_ci 11507db96d56Sopenharmony_ci 11517db96d56Sopenharmony_ci.. function:: critical(msg, *args, **kwargs) 11527db96d56Sopenharmony_ci 11537db96d56Sopenharmony_ci Logs a message with level :const:`CRITICAL` on the root logger. The arguments 11547db96d56Sopenharmony_ci are interpreted as for :func:`debug`. 11557db96d56Sopenharmony_ci 11567db96d56Sopenharmony_ci 11577db96d56Sopenharmony_ci.. function:: exception(msg, *args, **kwargs) 11587db96d56Sopenharmony_ci 11597db96d56Sopenharmony_ci Logs a message with level :const:`ERROR` on the root logger. The arguments are 11607db96d56Sopenharmony_ci interpreted as for :func:`debug`. Exception info is added to the logging 11617db96d56Sopenharmony_ci message. This function should only be called from an exception handler. 11627db96d56Sopenharmony_ci 11637db96d56Sopenharmony_ci.. function:: log(level, msg, *args, **kwargs) 11647db96d56Sopenharmony_ci 11657db96d56Sopenharmony_ci Logs a message with level *level* on the root logger. The other arguments are 11667db96d56Sopenharmony_ci interpreted as for :func:`debug`. 11677db96d56Sopenharmony_ci 11687db96d56Sopenharmony_ci.. function:: disable(level=CRITICAL) 11697db96d56Sopenharmony_ci 11707db96d56Sopenharmony_ci Provides an overriding level *level* for all loggers which takes precedence over 11717db96d56Sopenharmony_ci the logger's own level. When the need arises to temporarily throttle logging 11727db96d56Sopenharmony_ci output down across the whole application, this function can be useful. Its 11737db96d56Sopenharmony_ci effect is to disable all logging calls of severity *level* and below, so that 11747db96d56Sopenharmony_ci if you call it with a value of INFO, then all INFO and DEBUG events would be 11757db96d56Sopenharmony_ci discarded, whereas those of severity WARNING and above would be processed 11767db96d56Sopenharmony_ci according to the logger's effective level. If 11777db96d56Sopenharmony_ci ``logging.disable(logging.NOTSET)`` is called, it effectively removes this 11787db96d56Sopenharmony_ci overriding level, so that logging output again depends on the effective 11797db96d56Sopenharmony_ci levels of individual loggers. 11807db96d56Sopenharmony_ci 11817db96d56Sopenharmony_ci Note that if you have defined any custom logging level higher than 11827db96d56Sopenharmony_ci ``CRITICAL`` (this is not recommended), you won't be able to rely on the 11837db96d56Sopenharmony_ci default value for the *level* parameter, but will have to explicitly supply a 11847db96d56Sopenharmony_ci suitable value. 11857db96d56Sopenharmony_ci 11867db96d56Sopenharmony_ci .. versionchanged:: 3.7 11877db96d56Sopenharmony_ci The *level* parameter was defaulted to level ``CRITICAL``. See 11887db96d56Sopenharmony_ci :issue:`28524` for more information about this change. 11897db96d56Sopenharmony_ci 11907db96d56Sopenharmony_ci.. function:: addLevelName(level, levelName) 11917db96d56Sopenharmony_ci 11927db96d56Sopenharmony_ci Associates level *level* with text *levelName* in an internal dictionary, which is 11937db96d56Sopenharmony_ci used to map numeric levels to a textual representation, for example when a 11947db96d56Sopenharmony_ci :class:`Formatter` formats a message. This function can also be used to define 11957db96d56Sopenharmony_ci your own levels. The only constraints are that all levels used must be 11967db96d56Sopenharmony_ci registered using this function, levels should be positive integers and they 11977db96d56Sopenharmony_ci should increase in increasing order of severity. 11987db96d56Sopenharmony_ci 11997db96d56Sopenharmony_ci .. note:: If you are thinking of defining your own levels, please see the 12007db96d56Sopenharmony_ci section on :ref:`custom-levels`. 12017db96d56Sopenharmony_ci 12027db96d56Sopenharmony_ci.. function:: getLevelNamesMapping() 12037db96d56Sopenharmony_ci 12047db96d56Sopenharmony_ci Returns a mapping from level names to their corresponding logging levels. For example, the 12057db96d56Sopenharmony_ci string "CRITICAL" maps to :const:`CRITICAL`. The returned mapping is copied from an internal 12067db96d56Sopenharmony_ci mapping on each call to this function. 12077db96d56Sopenharmony_ci 12087db96d56Sopenharmony_ci .. versionadded:: 3.11 12097db96d56Sopenharmony_ci 12107db96d56Sopenharmony_ci.. function:: getLevelName(level) 12117db96d56Sopenharmony_ci 12127db96d56Sopenharmony_ci Returns the textual or numeric representation of logging level *level*. 12137db96d56Sopenharmony_ci 12147db96d56Sopenharmony_ci If *level* is one of the predefined levels :const:`CRITICAL`, :const:`ERROR`, 12157db96d56Sopenharmony_ci :const:`WARNING`, :const:`INFO` or :const:`DEBUG` then you get the 12167db96d56Sopenharmony_ci corresponding string. If you have associated levels with names using 12177db96d56Sopenharmony_ci :func:`addLevelName` then the name you have associated with *level* is 12187db96d56Sopenharmony_ci returned. If a numeric value corresponding to one of the defined levels is 12197db96d56Sopenharmony_ci passed in, the corresponding string representation is returned. 12207db96d56Sopenharmony_ci 12217db96d56Sopenharmony_ci The *level* parameter also accepts a string representation of the level such 12227db96d56Sopenharmony_ci as 'INFO'. In such cases, this functions returns the corresponding numeric 12237db96d56Sopenharmony_ci value of the level. 12247db96d56Sopenharmony_ci 12257db96d56Sopenharmony_ci If no matching numeric or string value is passed in, the string 12267db96d56Sopenharmony_ci 'Level %s' % level is returned. 12277db96d56Sopenharmony_ci 12287db96d56Sopenharmony_ci .. note:: Levels are internally integers (as they need to be compared in the 12297db96d56Sopenharmony_ci logging logic). This function is used to convert between an integer level 12307db96d56Sopenharmony_ci and the level name displayed in the formatted log output by means of the 12317db96d56Sopenharmony_ci ``%(levelname)s`` format specifier (see :ref:`logrecord-attributes`), and 12327db96d56Sopenharmony_ci vice versa. 12337db96d56Sopenharmony_ci 12347db96d56Sopenharmony_ci .. versionchanged:: 3.4 12357db96d56Sopenharmony_ci In Python versions earlier than 3.4, this function could also be passed a 12367db96d56Sopenharmony_ci text level, and would return the corresponding numeric value of the level. 12377db96d56Sopenharmony_ci This undocumented behaviour was considered a mistake, and was removed in 12387db96d56Sopenharmony_ci Python 3.4, but reinstated in 3.4.2 due to retain backward compatibility. 12397db96d56Sopenharmony_ci 12407db96d56Sopenharmony_ci.. function:: makeLogRecord(attrdict) 12417db96d56Sopenharmony_ci 12427db96d56Sopenharmony_ci Creates and returns a new :class:`LogRecord` instance whose attributes are 12437db96d56Sopenharmony_ci defined by *attrdict*. This function is useful for taking a pickled 12447db96d56Sopenharmony_ci :class:`LogRecord` attribute dictionary, sent over a socket, and reconstituting 12457db96d56Sopenharmony_ci it as a :class:`LogRecord` instance at the receiving end. 12467db96d56Sopenharmony_ci 12477db96d56Sopenharmony_ci 12487db96d56Sopenharmony_ci.. function:: basicConfig(**kwargs) 12497db96d56Sopenharmony_ci 12507db96d56Sopenharmony_ci Does basic configuration for the logging system by creating a 12517db96d56Sopenharmony_ci :class:`StreamHandler` with a default :class:`Formatter` and adding it to the 12527db96d56Sopenharmony_ci root logger. The functions :func:`debug`, :func:`info`, :func:`warning`, 12537db96d56Sopenharmony_ci :func:`error` and :func:`critical` will call :func:`basicConfig` automatically 12547db96d56Sopenharmony_ci if no handlers are defined for the root logger. 12557db96d56Sopenharmony_ci 12567db96d56Sopenharmony_ci This function does nothing if the root logger already has handlers 12577db96d56Sopenharmony_ci configured, unless the keyword argument *force* is set to ``True``. 12587db96d56Sopenharmony_ci 12597db96d56Sopenharmony_ci .. note:: This function should be called from the main thread 12607db96d56Sopenharmony_ci before other threads are started. In versions of Python prior to 12617db96d56Sopenharmony_ci 2.7.1 and 3.2, if this function is called from multiple threads, 12627db96d56Sopenharmony_ci it is possible (in rare circumstances) that a handler will be added 12637db96d56Sopenharmony_ci to the root logger more than once, leading to unexpected results 12647db96d56Sopenharmony_ci such as messages being duplicated in the log. 12657db96d56Sopenharmony_ci 12667db96d56Sopenharmony_ci The following keyword arguments are supported. 12677db96d56Sopenharmony_ci 12687db96d56Sopenharmony_ci .. tabularcolumns:: |l|L| 12697db96d56Sopenharmony_ci 12707db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 12717db96d56Sopenharmony_ci | Format | Description | 12727db96d56Sopenharmony_ci +==============+=============================================+ 12737db96d56Sopenharmony_ci | *filename* | Specifies that a :class:`FileHandler` be | 12747db96d56Sopenharmony_ci | | created, using the specified filename, | 12757db96d56Sopenharmony_ci | | rather than a :class:`StreamHandler`. | 12767db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 12777db96d56Sopenharmony_ci | *filemode* | If *filename* is specified, open the file | 12787db96d56Sopenharmony_ci | | in this :ref:`mode <filemodes>`. Defaults | 12797db96d56Sopenharmony_ci | | to ``'a'``. | 12807db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 12817db96d56Sopenharmony_ci | *format* | Use the specified format string for the | 12827db96d56Sopenharmony_ci | | handler. Defaults to attributes | 12837db96d56Sopenharmony_ci | | ``levelname``, ``name`` and ``message`` | 12847db96d56Sopenharmony_ci | | separated by colons. | 12857db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 12867db96d56Sopenharmony_ci | *datefmt* | Use the specified date/time format, as | 12877db96d56Sopenharmony_ci | | accepted by :func:`time.strftime`. | 12887db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 12897db96d56Sopenharmony_ci | *style* | If *format* is specified, use this style | 12907db96d56Sopenharmony_ci | | for the format string. One of ``'%'``, | 12917db96d56Sopenharmony_ci | | ``'{'`` or ``'$'`` for :ref:`printf-style | 12927db96d56Sopenharmony_ci | | <old-string-formatting>`, | 12937db96d56Sopenharmony_ci | | :meth:`str.format` or | 12947db96d56Sopenharmony_ci | | :class:`string.Template` respectively. | 12957db96d56Sopenharmony_ci | | Defaults to ``'%'``. | 12967db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 12977db96d56Sopenharmony_ci | *level* | Set the root logger level to the specified | 12987db96d56Sopenharmony_ci | | :ref:`level <levels>`. | 12997db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 13007db96d56Sopenharmony_ci | *stream* | Use the specified stream to initialize the | 13017db96d56Sopenharmony_ci | | :class:`StreamHandler`. Note that this | 13027db96d56Sopenharmony_ci | | argument is incompatible with *filename* - | 13037db96d56Sopenharmony_ci | | if both are present, a ``ValueError`` is | 13047db96d56Sopenharmony_ci | | raised. | 13057db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 13067db96d56Sopenharmony_ci | *handlers* | If specified, this should be an iterable of | 13077db96d56Sopenharmony_ci | | already created handlers to add to the root | 13087db96d56Sopenharmony_ci | | logger. Any handlers which don't already | 13097db96d56Sopenharmony_ci | | have a formatter set will be assigned the | 13107db96d56Sopenharmony_ci | | default formatter created in this function. | 13117db96d56Sopenharmony_ci | | Note that this argument is incompatible | 13127db96d56Sopenharmony_ci | | with *filename* or *stream* - if both | 13137db96d56Sopenharmony_ci | | are present, a ``ValueError`` is raised. | 13147db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 13157db96d56Sopenharmony_ci | *force* | If this keyword argument is specified as | 13167db96d56Sopenharmony_ci | | true, any existing handlers attached to the | 13177db96d56Sopenharmony_ci | | root logger are removed and closed, before | 13187db96d56Sopenharmony_ci | | carrying out the configuration as specified | 13197db96d56Sopenharmony_ci | | by the other arguments. | 13207db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 13217db96d56Sopenharmony_ci | *encoding* | If this keyword argument is specified along | 13227db96d56Sopenharmony_ci | | with *filename*, its value is used when the | 13237db96d56Sopenharmony_ci | | :class:`FileHandler` is created, and thus | 13247db96d56Sopenharmony_ci | | used when opening the output file. | 13257db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 13267db96d56Sopenharmony_ci | *errors* | If this keyword argument is specified along | 13277db96d56Sopenharmony_ci | | with *filename*, its value is used when the | 13287db96d56Sopenharmony_ci | | :class:`FileHandler` is created, and thus | 13297db96d56Sopenharmony_ci | | used when opening the output file. If not | 13307db96d56Sopenharmony_ci | | specified, the value 'backslashreplace' is | 13317db96d56Sopenharmony_ci | | used. Note that if ``None`` is specified, | 13327db96d56Sopenharmony_ci | | it will be passed as such to :func:`open`, | 13337db96d56Sopenharmony_ci | | which means that it will be treated the | 13347db96d56Sopenharmony_ci | | same as passing 'errors'. | 13357db96d56Sopenharmony_ci +--------------+---------------------------------------------+ 13367db96d56Sopenharmony_ci 13377db96d56Sopenharmony_ci .. versionchanged:: 3.2 13387db96d56Sopenharmony_ci The *style* argument was added. 13397db96d56Sopenharmony_ci 13407db96d56Sopenharmony_ci .. versionchanged:: 3.3 13417db96d56Sopenharmony_ci The *handlers* argument was added. Additional checks were added to 13427db96d56Sopenharmony_ci catch situations where incompatible arguments are specified (e.g. 13437db96d56Sopenharmony_ci *handlers* together with *stream* or *filename*, or *stream* 13447db96d56Sopenharmony_ci together with *filename*). 13457db96d56Sopenharmony_ci 13467db96d56Sopenharmony_ci .. versionchanged:: 3.8 13477db96d56Sopenharmony_ci The *force* argument was added. 13487db96d56Sopenharmony_ci 13497db96d56Sopenharmony_ci .. versionchanged:: 3.9 13507db96d56Sopenharmony_ci The *encoding* and *errors* arguments were added. 13517db96d56Sopenharmony_ci 13527db96d56Sopenharmony_ci.. function:: shutdown() 13537db96d56Sopenharmony_ci 13547db96d56Sopenharmony_ci Informs the logging system to perform an orderly shutdown by flushing and 13557db96d56Sopenharmony_ci closing all handlers. This should be called at application exit and no 13567db96d56Sopenharmony_ci further use of the logging system should be made after this call. 13577db96d56Sopenharmony_ci 13587db96d56Sopenharmony_ci When the logging module is imported, it registers this function as an exit 13597db96d56Sopenharmony_ci handler (see :mod:`atexit`), so normally there's no need to do that 13607db96d56Sopenharmony_ci manually. 13617db96d56Sopenharmony_ci 13627db96d56Sopenharmony_ci 13637db96d56Sopenharmony_ci.. function:: setLoggerClass(klass) 13647db96d56Sopenharmony_ci 13657db96d56Sopenharmony_ci Tells the logging system to use the class *klass* when instantiating a logger. 13667db96d56Sopenharmony_ci The class should define :meth:`__init__` such that only a name argument is 13677db96d56Sopenharmony_ci required, and the :meth:`__init__` should call :meth:`Logger.__init__`. This 13687db96d56Sopenharmony_ci function is typically called before any loggers are instantiated by applications 13697db96d56Sopenharmony_ci which need to use custom logger behavior. After this call, as at any other 13707db96d56Sopenharmony_ci time, do not instantiate loggers directly using the subclass: continue to use 13717db96d56Sopenharmony_ci the :func:`logging.getLogger` API to get your loggers. 13727db96d56Sopenharmony_ci 13737db96d56Sopenharmony_ci 13747db96d56Sopenharmony_ci.. function:: setLogRecordFactory(factory) 13757db96d56Sopenharmony_ci 13767db96d56Sopenharmony_ci Set a callable which is used to create a :class:`LogRecord`. 13777db96d56Sopenharmony_ci 13787db96d56Sopenharmony_ci :param factory: The factory callable to be used to instantiate a log record. 13797db96d56Sopenharmony_ci 13807db96d56Sopenharmony_ci .. versionadded:: 3.2 13817db96d56Sopenharmony_ci This function has been provided, along with :func:`getLogRecordFactory`, to 13827db96d56Sopenharmony_ci allow developers more control over how the :class:`LogRecord` representing 13837db96d56Sopenharmony_ci a logging event is constructed. 13847db96d56Sopenharmony_ci 13857db96d56Sopenharmony_ci The factory has the following signature: 13867db96d56Sopenharmony_ci 13877db96d56Sopenharmony_ci ``factory(name, level, fn, lno, msg, args, exc_info, func=None, sinfo=None, **kwargs)`` 13887db96d56Sopenharmony_ci 13897db96d56Sopenharmony_ci :name: The logger name. 13907db96d56Sopenharmony_ci :level: The logging level (numeric). 13917db96d56Sopenharmony_ci :fn: The full pathname of the file where the logging call was made. 13927db96d56Sopenharmony_ci :lno: The line number in the file where the logging call was made. 13937db96d56Sopenharmony_ci :msg: The logging message. 13947db96d56Sopenharmony_ci :args: The arguments for the logging message. 13957db96d56Sopenharmony_ci :exc_info: An exception tuple, or ``None``. 13967db96d56Sopenharmony_ci :func: The name of the function or method which invoked the logging 13977db96d56Sopenharmony_ci call. 13987db96d56Sopenharmony_ci :sinfo: A stack traceback such as is provided by 13997db96d56Sopenharmony_ci :func:`traceback.print_stack`, showing the call hierarchy. 14007db96d56Sopenharmony_ci :kwargs: Additional keyword arguments. 14017db96d56Sopenharmony_ci 14027db96d56Sopenharmony_ci 14037db96d56Sopenharmony_ciModule-Level Attributes 14047db96d56Sopenharmony_ci----------------------- 14057db96d56Sopenharmony_ci 14067db96d56Sopenharmony_ci.. attribute:: lastResort 14077db96d56Sopenharmony_ci 14087db96d56Sopenharmony_ci A "handler of last resort" is available through this attribute. This 14097db96d56Sopenharmony_ci is a :class:`StreamHandler` writing to ``sys.stderr`` with a level of 14107db96d56Sopenharmony_ci ``WARNING``, and is used to handle logging events in the absence of any 14117db96d56Sopenharmony_ci logging configuration. The end result is to just print the message to 14127db96d56Sopenharmony_ci ``sys.stderr``. This replaces the earlier error message saying that 14137db96d56Sopenharmony_ci "no handlers could be found for logger XYZ". If you need the earlier 14147db96d56Sopenharmony_ci behaviour for some reason, ``lastResort`` can be set to ``None``. 14157db96d56Sopenharmony_ci 14167db96d56Sopenharmony_ci .. versionadded:: 3.2 14177db96d56Sopenharmony_ci 14187db96d56Sopenharmony_ciIntegration with the warnings module 14197db96d56Sopenharmony_ci------------------------------------ 14207db96d56Sopenharmony_ci 14217db96d56Sopenharmony_ciThe :func:`captureWarnings` function can be used to integrate :mod:`logging` 14227db96d56Sopenharmony_ciwith the :mod:`warnings` module. 14237db96d56Sopenharmony_ci 14247db96d56Sopenharmony_ci.. function:: captureWarnings(capture) 14257db96d56Sopenharmony_ci 14267db96d56Sopenharmony_ci This function is used to turn the capture of warnings by logging on and 14277db96d56Sopenharmony_ci off. 14287db96d56Sopenharmony_ci 14297db96d56Sopenharmony_ci If *capture* is ``True``, warnings issued by the :mod:`warnings` module will 14307db96d56Sopenharmony_ci be redirected to the logging system. Specifically, a warning will be 14317db96d56Sopenharmony_ci formatted using :func:`warnings.formatwarning` and the resulting string 14327db96d56Sopenharmony_ci logged to a logger named ``'py.warnings'`` with a severity of :const:`WARNING`. 14337db96d56Sopenharmony_ci 14347db96d56Sopenharmony_ci If *capture* is ``False``, the redirection of warnings to the logging system 14357db96d56Sopenharmony_ci will stop, and warnings will be redirected to their original destinations 14367db96d56Sopenharmony_ci (i.e. those in effect before ``captureWarnings(True)`` was called). 14377db96d56Sopenharmony_ci 14387db96d56Sopenharmony_ci 14397db96d56Sopenharmony_ci.. seealso:: 14407db96d56Sopenharmony_ci 14417db96d56Sopenharmony_ci Module :mod:`logging.config` 14427db96d56Sopenharmony_ci Configuration API for the logging module. 14437db96d56Sopenharmony_ci 14447db96d56Sopenharmony_ci Module :mod:`logging.handlers` 14457db96d56Sopenharmony_ci Useful handlers included with the logging module. 14467db96d56Sopenharmony_ci 14477db96d56Sopenharmony_ci :pep:`282` - A Logging System 14487db96d56Sopenharmony_ci The proposal which described this feature for inclusion in the Python standard 14497db96d56Sopenharmony_ci library. 14507db96d56Sopenharmony_ci 14517db96d56Sopenharmony_ci `Original Python logging package <https://old.red-dove.com/python_logging.html>`_ 14527db96d56Sopenharmony_ci This is the original source for the :mod:`logging` package. The version of the 14537db96d56Sopenharmony_ci package available from this site is suitable for use with Python 1.5.2, 2.1.x 14547db96d56Sopenharmony_ci and 2.2.x, which do not include the :mod:`logging` package in the standard 14557db96d56Sopenharmony_ci library. 1456