<metaproperty="og:description"content="Source code: Lib/logging/__init__.py Important: This page contains the API reference information. For tutorial information and discussion of more advanced topics, see Basic Tutorial, Advanced Tutor...">
<metaname="description"content="Source code: Lib/logging/__init__.py Important: This page contains the API reference information. For tutorial information and discussion of more advanced topics, see Basic Tutorial, Advanced Tutor...">
<linkrel="search"type="application/opensearchdescription+xml"title="Search within Python 3.11.5 documentation"href="https://docs.python.org/3/_static/opensearch.xml">
<linkrel="author"title="About these documents"href="https://docs.python.org/3/about.html">
<li><aclass="reference internal"href="https://docs.python.org/3/library/logging.html#integration-with-the-warnings-module">Integration with the warnings module</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<pclass="topless"><ahref="https://docs.python.org/3/library/getopt.html"title="previous chapter"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">getopt</span></code> — C-style parser for command line options</a></p>
<spanid="logging-logging-facility-for-python"></span><h1><aclass="reference internal"href="https://docs.python.org/3/library/logging.html#module-logging"title="logging: Flexible event logging system for applications."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">logging</span></code></a> — Logging facility for Python<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#module-logging"title="Permalink to this headline">¶</a></h1>
<p>The module provides a lot of functionality and flexibility. If you are
unfamiliar with logging, the best way to get to grips with it is to view the
tutorials (<strong>see the links above and on the right</strong>).</p>
<p>The basic classes defined by the module, together with their functions, are
listed below.</p>
<ulclass="simple">
<li><p>Loggers expose the interface that application code directly uses.</p></li>
<li><p>Handlers send the log records (created by loggers) to the appropriate
destination.</p></li>
<li><p>Filters provide a finer grained facility for determining which log records
to output.</p></li>
<li><p>Formatters specify the layout of log records in the final output.</p></li>
</ul>
<sectionid="logger-objects">
<spanid="logger"></span><h2>Logger Objects<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logger-objects"title="Permalink to this headline">¶</a></h2>
<p>Loggers have the following attributes and methods. Note that Loggers should
<em>NEVER</em> be instantiated directly, but always through the module-level function
<codeclass="docutils literal notranslate"><spanclass="pre">logging.getLogger(name)</span></code>. Multiple calls to <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.getLogger"title="logging.getLogger"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">getLogger()</span></code></a> with the same
name will always return a reference to the same Logger object.</p>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">name</span></code> is potentially a period-separated hierarchical value, like
<codeclass="docutils literal notranslate"><spanclass="pre">foo.bar.baz</span></code> (though it could also be just plain <codeclass="docutils literal notranslate"><spanclass="pre">foo</span></code>, for example).
Loggers that are further down in the hierarchical list are children of loggers
higher up in the list. For example, given a logger with a name of <codeclass="docutils literal notranslate"><spanclass="pre">foo</span></code>,
loggers with names of <codeclass="docutils literal notranslate"><spanclass="pre">foo.bar</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">foo.bar.baz</span></code>, and <codeclass="docutils literal notranslate"><spanclass="pre">foo.bam</span></code> are all
descendants of <codeclass="docutils literal notranslate"><spanclass="pre">foo</span></code>. The logger name hierarchy is analogous to the Python
package hierarchy, and identical to it if you organise your loggers on a
per-module basis using the recommended construction
<codeclass="docutils literal notranslate"><spanclass="pre">logging.getLogger(__name__)</span></code>. That’s because in a module, <codeclass="docutils literal notranslate"><spanclass="pre">__name__</span></code>
is the module’s name in the Python package namespace.</p>
<dlclass="py class">
<dtclass="sig sig-object py"id="logging.Logger">
<emclass="property"><spanclass="pre">class</span><spanclass="w"></span></em><spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">Logger</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger"title="Permalink to this definition">¶</a></dt>
<spanclass="sig-name descname"><spanclass="pre">propagate</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.propagate"title="Permalink to this definition">¶</a></dt>
<dd><p>If this attribute evaluates to true, events logged to this logger will be
passed to the handlers of higher level (ancestor) loggers, in addition to
any handlers attached to this logger. Messages are passed directly to the
ancestor loggers’ handlers - neither the level nor filters of the ancestor
loggers in question are considered.</p>
<p>If this evaluates to false, logging messages are not passed to the handlers
of ancestor loggers.</p>
<p>Spelling it out with an example: If the propagate attribute of the logger named
<codeclass="docutils literal notranslate"><spanclass="pre">A.B.C</span></code> evaluates to true, any event logged to <codeclass="docutils literal notranslate"><spanclass="pre">A.B.C</span></code> via a method call such as
<codeclass="docutils literal notranslate"><spanclass="pre">logging.getLogger('A.B.C').error(...)</span></code> will [subject to passing that logger’s
level and filter settings] be passed in turn to any handlers attached to loggers
named <codeclass="docutils literal notranslate"><spanclass="pre">A.B</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">A</span></code> and the root logger, after first being passed to any handlers
attached to <codeclass="docutils literal notranslate"><spanclass="pre">A.B.C</span></code>. If any logger in the chain <codeclass="docutils literal notranslate"><spanclass="pre">A.B.C</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">A.B</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">A</span></code> has its
<codeclass="docutils literal notranslate"><spanclass="pre">propagate</span></code> attribute set to false, then that is the last logger whose handlers
are offered the event to handle, and propagation stops at that point.</p>
<p>The constructor sets this attribute to <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>If you attach a handler to a logger <em>and</em> one or more of its
ancestors, it may emit the same record multiple times. In general, you
should not need to attach a handler to more than one logger - if you just
attach it to the appropriate logger which is highest in the logger
hierarchy, then it will see all events logged by all descendant loggers,
provided that their propagate setting is left set to <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>. A common
scenario is to attach handlers only to the root logger, and to let
<spanclass="sig-name descname"><spanclass="pre">setLevel</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.setLevel"title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the threshold for this logger to <em>level</em>. Logging messages which are less
severe than <em>level</em> will be ignored; logging messages which have severity <em>level</em>
or higher will be emitted by whichever handler or handlers service this logger,
unless a handler’s level has been set to a higher severity level than <em>level</em>.</p>
<p>When a logger is created, the level is set to <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.NOTSET"title="logging.NOTSET"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">NOTSET</span></code></a> (which causes
all messages to be processed when the logger is the root logger, or delegation
to the parent when the logger is a non-root logger). Note that the root logger
is created with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.WARNING"title="logging.WARNING"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">WARNING</span></code></a>.</p>
<p>The term ‘delegation to the parent’ means that if a logger has a level of
NOTSET, its chain of ancestor loggers is traversed until either an ancestor with
a level other than NOTSET is found, or the root is reached.</p>
<p>If an ancestor is found with a level other than NOTSET, then that ancestor’s
level is treated as the effective level of the logger where the ancestor search
began, and is used to determine how a logging event is handled.</p>
<p>If the root is reached, and it has a level of NOTSET, then all messages will be
processed. Otherwise, the root’s level will be used as the effective level.</p>
<p>See <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#levels"><spanclass="std std-ref">Logging Levels</span></a> for a list of levels.</p>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>The <em>level</em> parameter now accepts a string representation of the
level such as ‘INFO’ as an alternative to the integer constants
such as <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.INFO"title="logging.INFO"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">INFO</span></code></a>. Note, however, that levels are internally stored
as integers, and methods such as e.g. <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.getEffectiveLevel"title="logging.Logger.getEffectiveLevel"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">getEffectiveLevel()</span></code></a> and
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.isEnabledFor"title="logging.Logger.isEnabledFor"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">isEnabledFor()</span></code></a> will return/expect to be passed integers.</p>
<spanclass="sig-name descname"><spanclass="pre">isEnabledFor</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.isEnabledFor"title="Permalink to this definition">¶</a></dt>
<dd><p>Indicates if a message of severity <em>level</em> would be processed by this logger.
This method checks first the module-level level set by
<codeclass="docutils literal notranslate"><spanclass="pre">logging.disable(level)</span></code> and then the logger’s effective level as determined
by <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.getEffectiveLevel"title="logging.Logger.getEffectiveLevel"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">getEffectiveLevel()</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">getEffectiveLevel</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.getEffectiveLevel"title="Permalink to this definition">¶</a></dt>
<dd><p>Indicates the effective level for this logger. If a value other than
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.NOTSET"title="logging.NOTSET"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">NOTSET</span></code></a> has been set using <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.setLevel"title="logging.Logger.setLevel"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">setLevel()</span></code></a>, it is returned. Otherwise,
the hierarchy is traversed towards the root until a value other than
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.NOTSET"title="logging.NOTSET"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">NOTSET</span></code></a> is found, and that value is returned. The value returned is
an integer, typically one of <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.DEBUG"title="logging.DEBUG"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">logging.DEBUG</span></code></a>, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.INFO"title="logging.INFO"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">logging.INFO</span></code></a>
<spanclass="sig-name descname"><spanclass="pre">getChild</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">suffix</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.getChild"title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a logger which is a descendant to this logger, as determined by the suffix.
Thus, <codeclass="docutils literal notranslate"><spanclass="pre">logging.getLogger('abc').getChild('def.ghi')</span></code> would return the same
logger as would be returned by <codeclass="docutils literal notranslate"><spanclass="pre">logging.getLogger('abc.def.ghi')</span></code>. This is a
convenience method, useful when the parent logger is named using e.g. <codeclass="docutils literal notranslate"><spanclass="pre">__name__</span></code>
rather than a literal string.</p>
<divclass="versionadded">
<p><spanclass="versionmodified added">New in version 3.2.</span></p>
<spanclass="sig-name descname"><spanclass="pre">debug</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.debug"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.DEBUG"title="logging.DEBUG"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">DEBUG</span></code></a> on this logger. The <em>msg</em> is the
message format string, and the <em>args</em> are the arguments which are merged into
<em>msg</em> using the string formatting operator. (Note that this means that you can
use keywords in the format string, together with a single dictionary argument.)
No % formatting operation is performed on <em>msg</em> when no <em>args</em> are supplied.</p>
<p>There are four keyword arguments in <em>kwargs</em> which are inspected:
<em>exc_info</em>, <em>stack_info</em>, <em>stacklevel</em> and <em>extra</em>.</p>
<p>If <em>exc_info</em> does not evaluate as false, it causes exception information to be
added to the logging message. If an exception tuple (in the format returned by
<aclass="reference internal"href="https://docs.python.org/3/library/sys.html#sys.exc_info"title="sys.exc_info"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">sys.exc_info()</span></code></a>) or an exception instance is provided, it is used;
otherwise, <aclass="reference internal"href="https://docs.python.org/3/library/sys.html#sys.exc_info"title="sys.exc_info"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">sys.exc_info()</span></code></a> is called to get the exception information.</p>
<p>The second optional keyword argument is <em>stack_info</em>, which defaults to
<codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>. If true, stack information is added to the logging
message, including the actual logging call. Note that this is not the same
stack information as that displayed through specifying <em>exc_info</em>: The
former is stack frames from the bottom of the stack up to the logging call
in the current thread, whereas the latter is information about stack frames
which have been unwound, following an exception, while searching for
exception handlers.</p>
<p>You can specify <em>stack_info</em> independently of <em>exc_info</em>, e.g. to just show
how you got to a certain point in your code, even when no exceptions were
raised. The stack frames are printed following a header line which says:</p>
<p>This mimics the <codeclass="docutils literal notranslate"><spanclass="pre">Traceback</span><spanclass="pre">(most</span><spanclass="pre">recent</span><spanclass="pre">call</span><spanclass="pre">last):</span></code> which is used when
displaying exception frames.</p>
<p>The third optional keyword argument is <em>stacklevel</em>, which defaults to <codeclass="docutils literal notranslate"><spanclass="pre">1</span></code>.
If greater than 1, the corresponding number of stack frames are skipped
when computing the line number and function name set in the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a>
created for the logging event. This can be used in logging helpers so that
the function name, filename and line number recorded are not the information
for the helper function/method, but rather its caller. The name of this
parameter mirrors the equivalent one in the <aclass="reference internal"href="https://docs.python.org/3/library/warnings.html#module-warnings"title="warnings: Issue warning messages and control their disposition."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">warnings</span></code></a> module.</p>
<p>The fourth keyword argument is <em>extra</em> which can be used to pass a
dictionary which is used to populate the __dict__ of the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a>
created for the logging event with user-defined attributes. These custom
attributes can then be used as you like. For example, they could be
incorporated into logged messages. For example:</p>
<p>The keys in the dictionary passed in <em>extra</em> should not clash with the keys used
by the logging system. (See the section on <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logrecord-attributes"><spanclass="std std-ref">LogRecord attributes</span></a> for more
information on which keys are used by the logging system.)</p>
<p>If you choose to use these attributes in logged messages, you need to exercise
some care. In the above example, for instance, the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> has been
set up with a format string which expects ‘clientip’ and ‘user’ in the attribute
dictionary of the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a>. If these are missing, the message will
not be logged because a string formatting exception will occur. So in this case,
you always need to pass the <em>extra</em> dictionary with these keys.</p>
<p>While this might be annoying, this feature is intended for use in specialized
circumstances, such as multi-threaded servers where the same code executes in
many contexts, and interesting conditions which arise are dependent on this
context (such as remote client IP address and authenticated user name, in the
above example). In such circumstances, it is likely that specialized
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a>s would be used with particular <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler"title="logging.Handler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Handler</span></code></a>s.</p>
<p>If no handler is attached to this logger (or any of its ancestors,
taking into account the relevant <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.propagate"title="logging.Logger.propagate"><codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">Logger.propagate</span></code></a> attributes),
the message will be sent to the handler set on <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.lastResort"title="logging.lastResort"><codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">lastResort</span></code></a>.</p>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>The <em>stack_info</em> parameter was added.</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.5: </span>The <em>exc_info</em> parameter can now accept exception instances.</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.8: </span>The <em>stacklevel</em> parameter was added.</p>
<spanclass="sig-name descname"><spanclass="pre">info</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.info"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.INFO"title="logging.INFO"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">INFO</span></code></a> on this logger. The arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">warning</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.warning"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.WARNING"title="logging.WARNING"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">WARNING</span></code></a> on this logger. The arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>There is an obsolete method <codeclass="docutils literal notranslate"><spanclass="pre">warn</span></code> which is functionally
identical to <codeclass="docutils literal notranslate"><spanclass="pre">warning</span></code>. As <codeclass="docutils literal notranslate"><spanclass="pre">warn</span></code> is deprecated, please do not use
it - use <codeclass="docutils literal notranslate"><spanclass="pre">warning</span></code> instead.</p>
<spanclass="sig-name descname"><spanclass="pre">error</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.error"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.ERROR"title="logging.ERROR"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">ERROR</span></code></a> on this logger. The arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">critical</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.critical"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.CRITICAL"title="logging.CRITICAL"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">CRITICAL</span></code></a> on this logger. The arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">log</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.log"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with integer level <em>level</em> on this logger. The other arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">exception</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.exception"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.ERROR"title="logging.ERROR"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">ERROR</span></code></a> on this logger. The arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">debug()</span></code></a>. Exception info is added to the logging
message. This method should only be called from an exception handler.</p>
<spanclass="sig-name descname"><spanclass="pre">addFilter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">filter</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.addFilter"title="Permalink to this definition">¶</a></dt>
<dd><p>Adds the specified filter <em>filter</em> to this logger.</p>
<spanclass="sig-name descname"><spanclass="pre">removeFilter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">filter</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.removeFilter"title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the specified filter <em>filter</em> from this logger.</p>
<spanclass="sig-name descname"><spanclass="pre">filter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.filter"title="Permalink to this definition">¶</a></dt>
<dd><p>Apply this logger’s filters to the record and return <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> if the
record is to be processed. The filters are consulted in turn, until one of
them returns a false value. If none of them return a false value, the record
will be processed (passed to handlers). If one returns a false value, no
<spanclass="sig-name descname"><spanclass="pre">addHandler</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">hdlr</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.addHandler"title="Permalink to this definition">¶</a></dt>
<dd><p>Adds the specified handler <em>hdlr</em> to this logger.</p>
<spanclass="sig-name descname"><spanclass="pre">removeHandler</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">hdlr</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.removeHandler"title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the specified handler <em>hdlr</em> from this logger.</p>
<spanclass="sig-name descname"><spanclass="pre">findCaller</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">stack_info</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">False</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">stacklevel</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">1</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.findCaller"title="Permalink to this definition">¶</a></dt>
<dd><p>Finds the caller’s source filename and line number. Returns the filename, line
number, function name and stack information as a 4-element tuple. The stack
information is returned as <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> unless <em>stack_info</em> is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>.</p>
<p>The <em>stacklevel</em> parameter is passed from code calling the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">debug()</span></code></a>
and other APIs. If greater than 1, the excess is used to skip stack frames
before determining the values to be returned. This will generally be useful
when calling logging APIs from helper/wrapper code, so that the information
in the event log refers not to the helper/wrapper code, but to the code that
<spanclass="sig-name descname"><spanclass="pre">handle</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.handle"title="Permalink to this definition">¶</a></dt>
<dd><p>Handles a record by passing it to all handlers associated with this logger and
its ancestors (until a false value of <em>propagate</em> is found). This method is used
for unpickled records received from a socket, as well as those created locally.
Logger-level filtering is applied using <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.filter"title="logging.Logger.filter"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">filter()</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">hasHandlers</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Logger.hasHandlers"title="Permalink to this definition">¶</a></dt>
<dd><p>Checks to see if this logger has any handlers configured. This is done by
looking for handlers in this logger and its parents in the logger hierarchy.
Returns <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> if a handler was found, else <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>. The method stops searching
up the hierarchy whenever a logger with the ‘propagate’ attribute set to
false is found - that will be the last logger which is checked for the
existence of handlers.</p>
<divclass="versionadded">
<p><spanclass="versionmodified added">New in version 3.2.</span></p>
</div>
</dd></dl>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.7: </span>Loggers can now be pickled and unpickled.</p>
</div>
</dd></dl>
</section>
<sectionid="logging-levels">
<spanid="levels"></span><h2>Logging Levels<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging-levels"title="Permalink to this headline">¶</a></h2>
<p>The numeric values of logging levels are given in the following table. These are
primarily of interest if you want to define your own levels, and need them to
have specific values relative to the predefined levels. If you define a level
with the same numeric value, it overwrites the predefined value; the predefined
<thclass="head"><p>What it means / When to use it</p></th>
</tr>
</thead>
<tbody>
<trclass="row-even"><td><dlclass="py data">
<dtclass="sig sig-object py"id="logging.NOTSET">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">NOTSET</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.NOTSET"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
</td>
<td><p>0</p></td>
<td><p>When set on a logger, indicates that
ancestor loggers are to be consulted
to determine the effective level.
If that still resolves to
<codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">NOTSET</span></code>, then all events
are logged. When set on a handler,
all events are handled.</p></td>
</tr>
<trclass="row-odd"><td><dlclass="py data">
<dtclass="sig sig-object py"id="logging.DEBUG">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">DEBUG</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.DEBUG"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
</td>
<td><p>10</p></td>
<td><p>Detailed information, typically only
of interest to a developer trying to
diagnose a problem.</p></td>
</tr>
<trclass="row-even"><td><dlclass="py data">
<dtclass="sig sig-object py"id="logging.INFO">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">INFO</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.INFO"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
</td>
<td><p>20</p></td>
<td><p>Confirmation that things are working
as expected.</p></td>
</tr>
<trclass="row-odd"><td><dlclass="py data">
<dtclass="sig sig-object py"id="logging.WARNING">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">WARNING</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.WARNING"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
</td>
<td><p>30</p></td>
<td><p>An indication that something
unexpected happened, or that a
problem might occur in the near
future (e.g. ‘disk space low’). The
software is still working as
expected.</p></td>
</tr>
<trclass="row-even"><td><dlclass="py data">
<dtclass="sig sig-object py"id="logging.ERROR">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">ERROR</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.ERROR"title="Permalink to this definition">¶</a></dt>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">CRITICAL</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.CRITICAL"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
</td>
<td><p>50</p></td>
<td><p>A serious error, indicating that the
program itself may be unable to
continue running.</p></td>
</tr>
</tbody>
</table></div>
</section>
<sectionid="handler-objects">
<spanid="handler"></span><h2>Handler Objects<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#handler-objects"title="Permalink to this headline">¶</a></h2>
<p>Handlers have the following attributes and methods. Note that <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler"title="logging.Handler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Handler</span></code></a>
is never instantiated directly; this class acts as a base for more useful
subclasses. However, the <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__init__()</span></code> method in subclasses needs to call
<emclass="property"><spanclass="pre">class</span><spanclass="w"></span></em><spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">Handler</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler"title="Permalink to this definition">¶</a></dt>
<spanclass="sig-name descname"><spanclass="pre">__init__</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">NOTSET</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.__init__"title="Permalink to this definition">¶</a></dt>
<dd><p>Initializes the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler"title="logging.Handler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Handler</span></code></a> instance by setting its level, setting the list
of filters to the empty list and creating a lock (using <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler.createLock"title="logging.Handler.createLock"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">createLock()</span></code></a>) for
<spanclass="sig-name descname"><spanclass="pre">createLock</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.createLock"title="Permalink to this definition">¶</a></dt>
<dd><p>Initializes a thread lock which can be used to serialize access to underlying
I/O functionality which may not be threadsafe.</p>
<spanclass="sig-name descname"><spanclass="pre">acquire</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.acquire"title="Permalink to this definition">¶</a></dt>
<dd><p>Acquires the thread lock created with <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler.createLock"title="logging.Handler.createLock"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">createLock()</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">release</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.release"title="Permalink to this definition">¶</a></dt>
<dd><p>Releases the thread lock acquired with <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler.acquire"title="logging.Handler.acquire"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">acquire()</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">setLevel</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.setLevel"title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the threshold for this handler to <em>level</em>. Logging messages which are
less severe than <em>level</em> will be ignored. When a handler is created, the
level is set to <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.NOTSET"title="logging.NOTSET"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">NOTSET</span></code></a> (which causes all messages to be
processed).</p>
<p>See <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#levels"><spanclass="std std-ref">Logging Levels</span></a> for a list of levels.</p>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>The <em>level</em> parameter now accepts a string representation of the
level such as ‘INFO’ as an alternative to the integer constants
such as <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.INFO"title="logging.INFO"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">INFO</span></code></a>.</p>
<spanclass="sig-name descname"><spanclass="pre">setFormatter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">fmt</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.setFormatter"title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> for this handler to <em>fmt</em>.</p>
<spanclass="sig-name descname"><spanclass="pre">addFilter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">filter</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.addFilter"title="Permalink to this definition">¶</a></dt>
<dd><p>Adds the specified filter <em>filter</em> to this handler.</p>
<spanclass="sig-name descname"><spanclass="pre">removeFilter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">filter</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.removeFilter"title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the specified filter <em>filter</em> from this handler.</p>
<spanclass="sig-name descname"><spanclass="pre">filter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.filter"title="Permalink to this definition">¶</a></dt>
<dd><p>Apply this handler’s filters to the record and return <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> if the
record is to be processed. The filters are consulted in turn, until one of
them returns a false value. If none of them return a false value, the record
will be emitted. If one returns a false value, the handler will not emit the
<spanclass="sig-name descname"><spanclass="pre">flush</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.flush"title="Permalink to this definition">¶</a></dt>
<dd><p>Ensure all logging output has been flushed. This version does nothing and is
<spanclass="sig-name descname"><spanclass="pre">close</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.close"title="Permalink to this definition">¶</a></dt>
<dd><p>Tidy up any resources used by the handler. This version does no output but
removes the handler from an internal list of handlers which is closed when
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.shutdown"title="logging.shutdown"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">shutdown()</span></code></a> is called. Subclasses should ensure that this gets called
from overridden <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler.close"title="logging.Handler.close"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">close()</span></code></a> methods.</p>
<spanclass="sig-name descname"><spanclass="pre">handle</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.handle"title="Permalink to this definition">¶</a></dt>
<dd><p>Conditionally emits the specified logging record, depending on filters which may
have been added to the handler. Wraps the actual emission of the record with
<spanclass="sig-name descname"><spanclass="pre">handleError</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.handleError"title="Permalink to this definition">¶</a></dt>
<dd><p>This method should be called from handlers when an exception is encountered
during an <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler.emit"title="logging.Handler.emit"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">emit()</span></code></a> call. If the module-level attribute
<codeclass="docutils literal notranslate"><spanclass="pre">raiseExceptions</span></code> is <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>, exceptions get silently ignored. This is
what is mostly wanted for a logging system - most users will not care about
errors in the logging system, they are more interested in application
errors. You could, however, replace this with a custom handler if you wish.
The specified record is the one which was being processed when the exception
occurred. (The default value of <codeclass="docutils literal notranslate"><spanclass="pre">raiseExceptions</span></code> is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>, as that is
<spanclass="sig-name descname"><spanclass="pre">format</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.format"title="Permalink to this definition">¶</a></dt>
<dd><p>Do formatting for a record - if a formatter is set, use it. Otherwise, use the
<spanclass="sig-name descname"><spanclass="pre">emit</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Handler.emit"title="Permalink to this definition">¶</a></dt>
<dd><p>Do whatever it takes to actually log the specified logging record. This version
is intended to be implemented by subclasses and so raises a
<p>This method is called after a handler-level lock is acquired, which
is released after this method returns. When you override this method, note
that you should be careful when calling anything that invokes other parts of
the logging API which might do locking, because that might result in a
deadlock. Specifically:</p>
<ulclass="simple">
<li><p>Logging configuration APIs acquire the module-level lock, and then
individual handler-level locks as those handlers are configured.</p></li>
<li><p>Many logging APIs lock the module-level lock. If such an API is called
from this method, it could cause a deadlock if a configuration call is
made on another thread, because that thread will try to acquire the
module-level lock <em>before</em> the handler-level lock, whereas this thread
tries to acquire the module-level lock <em>after</em> the handler-level lock
(because in this method, the handler-level lock has already been acquired).</p></li>
</ul>
</div>
</dd></dl>
</dd></dl>
<p>For a list of handlers included as standard, see <aclass="reference internal"href="https://docs.python.org/3/library/logging.handlers.html#module-logging.handlers"title="logging.handlers: Handlers for the logging module."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">logging.handlers</span></code></a>.</p>
</section>
<sectionid="formatter-objects">
<spanid="id1"></span><h2>Formatter Objects<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#formatter-objects"title="Permalink to this headline">¶</a></h2>
<p><aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> objects have the following attributes and methods. They are
responsible for converting a <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> to (usually) a string which can
be interpreted by either a human or an external system. The base
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> allows a formatting string to be specified. If none is
supplied, the default value of <codeclass="docutils literal notranslate"><spanclass="pre">'%(message)s'</span></code> is used, which just includes
the message in the logging call. To have additional items of information in the
formatted output (such as a timestamp), keep reading.</p>
<p>A Formatter can be initialized with a format string which makes use of knowledge
of the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> attributes - such as the default value mentioned above
making use of the fact that the user’s message and arguments are pre-formatted
into a <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a>’s <em>message</em> attribute. This format string contains
standard Python %-style mapping keys. See section <aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#old-string-formatting"><spanclass="std std-ref">printf-style String Formatting</span></a>
for more information on string formatting.</p>
<p>The useful mapping keys in a <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> are given in the section on
<emclass="property"><spanclass="pre">class</span><spanclass="w"></span></em><spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">Formatter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">fmt</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">None</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">datefmt</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">None</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">style</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">'%'</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">validate</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">True</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">defaults</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">None</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a new instance of the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> class. The instance is
initialized with a format string for the message as a whole, as well as a
format string for the date/time portion of a message. If no <em>fmt</em> is
specified, <codeclass="docutils literal notranslate"><spanclass="pre">'%(message)s'</span></code> is used. If no <em>datefmt</em> is specified, a format
is used which is described in the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter.formatTime"title="logging.Formatter.formatTime"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">formatTime()</span></code></a> documentation.</p>
<p>The <em>style</em> parameter can be one of ‘%’, ‘{’ or ‘$’ and determines how
the format string will be merged with its data: using one of %-formatting,
<aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str.format"title="str.format"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">str.format()</span></code></a> or <aclass="reference internal"href="https://docs.python.org/3/library/string.html#string.Template"title="string.Template"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">string.Template</span></code></a>. This only applies to the
format string <em>fmt</em> (e.g. <codeclass="docutils literal notranslate"><spanclass="pre">'%(message)s'</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">{message}</span></code>), not to the
actual log messages passed to <codeclass="docutils literal notranslate"><spanclass="pre">Logger.debug</span></code> etc; see
<aclass="reference internal"href="https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles"><spanclass="std std-ref">Using particular formatting styles throughout your application</span></a> for more information on using {- and $-formatting
for log messages.</p>
<p>The <em>defaults</em> parameter can be a dictionary with default values to use in
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>The <em>style</em> parameter was added.</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.8: </span>The <em>validate</em> parameter was added. Incorrect or mismatched style and fmt
will raise a <codeclass="docutils literal notranslate"><spanclass="pre">ValueError</span></code>.
For example: <codeclass="docutils literal notranslate"><spanclass="pre">logging.Formatter('%(asctime)s</span><spanclass="pre">-</span><spanclass="pre">%(message)s',</span><spanclass="pre">style='{')</span></code>.</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.10: </span>The <em>defaults</em> parameter was added.</p>
<spanclass="sig-name descname"><spanclass="pre">format</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Formatter.format"title="Permalink to this definition">¶</a></dt>
<dd><p>The record’s attribute dictionary is used as the operand to a string
formatting operation. Returns the resulting string. Before formatting the
dictionary, a couple of preparatory steps are carried out. The <em>message</em>
attribute of the record is computed using <em>msg</em> % <em>args</em>. If the
formatting string contains <codeclass="docutils literal notranslate"><spanclass="pre">'(asctime)'</span></code>, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter.formatTime"title="logging.Formatter.formatTime"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">formatTime()</span></code></a> is called
to format the event time. If there is exception information, it is
formatted using <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter.formatException"title="logging.Formatter.formatException"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">formatException()</span></code></a> and appended to the message. Note
that the formatted exception information is cached in attribute
<em>exc_text</em>. This is useful because the exception information can be
pickled and sent across the wire, but you should be careful if you have
more than one <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> subclass which customizes the formatting
of exception information. In this case, you will have to clear the cached
value (by setting the <em>exc_text</em> attribute to <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>) after a formatter
has done its formatting, so that the next formatter to handle the event
doesn’t use the cached value, but recalculates it afresh.</p>
<p>If stack information is available, it’s appended after the exception
information, using <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter.formatStack"title="logging.Formatter.formatStack"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">formatStack()</span></code></a> to transform it if necessary.</p>
<spanclass="sig-name descname"><spanclass="pre">formatTime</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">datefmt</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">None</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Formatter.formatTime"title="Permalink to this definition">¶</a></dt>
<dd><p>This method should be called from <aclass="reference internal"href="https://docs.python.org/3/library/functions.html#format"title="format"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">format()</span></code></a> by a formatter which
wants to make use of a formatted time. This method can be overridden in
formatters to provide for any specific requirement, but the basic behavior
is as follows: if <em>datefmt</em> (a string) is specified, it is used with
<aclass="reference internal"href="https://docs.python.org/3/library/time.html#time.strftime"title="time.strftime"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">time.strftime()</span></code></a> to format the creation time of the
record. Otherwise, the format ‘%Y-%m-%d %H:%M:%S,uuu’ is used, where the
uuu part is a millisecond value and the other letters are as per the
<aclass="reference internal"href="https://docs.python.org/3/library/time.html#time.strftime"title="time.strftime"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">time.strftime()</span></code></a> documentation. An example time in this format is
<codeclass="docutils literal notranslate"><spanclass="pre">2003-01-23</span><spanclass="pre">00:29:50,411</span></code>. The resulting string is returned.</p>
<p>This function uses a user-configurable function to convert the creation
time to a tuple. By default, <aclass="reference internal"href="https://docs.python.org/3/library/time.html#time.localtime"title="time.localtime"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">time.localtime()</span></code></a> is used; to change
this for a particular formatter instance, set the <codeclass="docutils literal notranslate"><spanclass="pre">converter</span></code> attribute
to a function with the same signature as <aclass="reference internal"href="https://docs.python.org/3/library/time.html#time.localtime"title="time.localtime"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">time.localtime()</span></code></a> or
<aclass="reference internal"href="https://docs.python.org/3/library/time.html#time.gmtime"title="time.gmtime"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">time.gmtime()</span></code></a>. To change it for all formatters, for example if you
want all logging times to be shown in GMT, set the <codeclass="docutils literal notranslate"><spanclass="pre">converter</span></code>
attribute in the <codeclass="docutils literal notranslate"><spanclass="pre">Formatter</span></code> class.</p>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.3: </span>Previously, the default format was hard-coded as in this example:
<codeclass="docutils literal notranslate"><spanclass="pre">2010-09-06</span><spanclass="pre">22:38:15,292</span></code> where the part before the comma is
handled by a strptime format string (<codeclass="docutils literal notranslate"><spanclass="pre">'%Y-%m-%d</span><spanclass="pre">%H:%M:%S'</span></code>), and the
part after the comma is a millisecond value. Because strptime does not
have a format placeholder for milliseconds, the millisecond value is
appended using another format string, <codeclass="docutils literal notranslate"><spanclass="pre">'%s,%03d'</span></code> — and both of these
format strings have been hardcoded into this method. With the change,
these strings are defined as class-level attributes which can be
overridden at the instance level when desired. The names of the
attributes are <codeclass="docutils literal notranslate"><spanclass="pre">default_time_format</span></code> (for the strptime format string)
and <codeclass="docutils literal notranslate"><spanclass="pre">default_msec_format</span></code> (for appending the millisecond value).</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.9: </span>The <codeclass="docutils literal notranslate"><spanclass="pre">default_msec_format</span></code> can be <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>.</p>
<spanclass="sig-name descname"><spanclass="pre">formatException</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">exc_info</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Formatter.formatException"title="Permalink to this definition">¶</a></dt>
<dd><p>Formats the specified exception information (a standard exception tuple as
returned by <aclass="reference internal"href="https://docs.python.org/3/library/sys.html#sys.exc_info"title="sys.exc_info"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">sys.exc_info()</span></code></a>) as a string. This default implementation
just uses <aclass="reference internal"href="https://docs.python.org/3/library/traceback.html#traceback.print_exception"title="traceback.print_exception"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">traceback.print_exception()</span></code></a>. The resulting string is
<spanclass="sig-name descname"><spanclass="pre">formatStack</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">stack_info</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Formatter.formatStack"title="Permalink to this definition">¶</a></dt>
<dd><p>Formats the specified stack information (a string as returned by
<aclass="reference internal"href="https://docs.python.org/3/library/traceback.html#traceback.print_stack"title="traceback.print_stack"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">traceback.print_stack()</span></code></a>, but with the last newline removed) as a
string. This default implementation just returns the input value.</p>
<emclass="property"><spanclass="pre">class</span><spanclass="w"></span></em><spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">BufferingFormatter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">linefmt</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">None</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.BufferingFormatter"title="Permalink to this definition">¶</a></dt>
<dd><p>A base formatter class suitable for subclassing when you want to format a
number of records. You can pass a <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> instance which you want
to use to format each line (that corresponds to a single record). If not
specified, the default formatter (which just outputs the event message) is
<spanclass="sig-name descname"><spanclass="pre">formatHeader</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">records</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.BufferingFormatter.formatHeader"title="Permalink to this definition">¶</a></dt>
<dd><p>Return a header for a list of <em>records</em>. The base implementation just
returns the empty string. You will need to override this method if you
want specific behaviour, e.g. to show the count of records, a title or a
<spanclass="sig-name descname"><spanclass="pre">formatFooter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">records</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.BufferingFormatter.formatFooter"title="Permalink to this definition">¶</a></dt>
<dd><p>Return a footer for a list of <em>records</em>. The base implementation just
returns the empty string. You will need to override this method if you
want specific behaviour, e.g. to show the count of records or a separator
<spanclass="sig-name descname"><spanclass="pre">format</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">records</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.BufferingFormatter.format"title="Permalink to this definition">¶</a></dt>
<dd><p>Return formatted text for a list of <em>records</em>. The base implementation
just returns the empty string if there are no records; otherwise, it
returns the concatenation of the header, each record formatted with the
line formatter, and the footer.</p>
</dd></dl>
</dd></dl>
</section>
<sectionid="filter-objects">
<spanid="filter"></span><h2>Filter Objects<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#filter-objects"title="Permalink to this headline">¶</a></h2>
<p><codeclass="docutils literal notranslate"><spanclass="pre">Filters</span></code> can be used by <codeclass="docutils literal notranslate"><spanclass="pre">Handlers</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">Loggers</span></code> for more sophisticated
filtering than is provided by levels. The base filter class only allows events
which are below a certain point in the logger hierarchy. For example, a filter
initialized with ‘A.B’ will allow events logged by loggers ‘A.B’, ‘A.B.C’,
‘A.B.C.D’, ‘A.B.D’ etc. but not ‘A.BB’, ‘B.A.B’ etc. If initialized with the
empty string, all events are passed.</p>
<dlclass="py class">
<dtclass="sig sig-object py"id="logging.Filter">
<emclass="property"><spanclass="pre">class</span><spanclass="w"></span></em><spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">Filter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">name</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">''</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Filter"title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an instance of the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Filter"title="logging.Filter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Filter</span></code></a> class. If <em>name</em> is specified, it
names a logger which, together with its children, will have its events allowed
through the filter. If <em>name</em> is the empty string, allows every event.</p>
<spanclass="sig-name descname"><spanclass="pre">filter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">record</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.Filter.filter"title="Permalink to this definition">¶</a></dt>
<dd><p>Is the specified record to be logged? Returns zero for no, nonzero for
yes. If deemed appropriate, the record may be modified in-place by this
method.</p>
</dd></dl>
</dd></dl>
<p>Note that filters attached to handlers are consulted before an event is
emitted by the handler, whereas filters attached to loggers are consulted
whenever an event is logged (using <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">debug()</span></code></a>, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.info"title="logging.info"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">info()</span></code></a>,
etc.), before sending an event to handlers. This means that events which have
been generated by descendant loggers will not be filtered by a logger’s filter
setting, unless the filter has also been applied to those descendant loggers.</p>
<p>You don’t actually need to subclass <codeclass="docutils literal notranslate"><spanclass="pre">Filter</span></code>: you can pass any instance
which has a <codeclass="docutils literal notranslate"><spanclass="pre">filter</span></code> method with the same semantics.</p>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>You don’t need to create specialized <codeclass="docutils literal notranslate"><spanclass="pre">Filter</span></code> classes, or use other
classes with a <codeclass="docutils literal notranslate"><spanclass="pre">filter</span></code> method: you can use a function (or other
callable) as a filter. The filtering logic will check to see if the filter
object has a <codeclass="docutils literal notranslate"><spanclass="pre">filter</span></code> attribute: if it does, it’s assumed to be a
<codeclass="docutils literal notranslate"><spanclass="pre">Filter</span></code> and its <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Filter.filter"title="logging.Filter.filter"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">filter()</span></code></a> method is called. Otherwise, it’s
assumed to be a callable and called with the record as the single
parameter. The returned value should conform to that returned by
<p>Although filters are used primarily to filter records based on more
sophisticated criteria than levels, they get to see every record which is
processed by the handler or logger they’re attached to: this can be useful if
you want to do things like counting how many records were processed by a
particular logger or handler, or adding, changing or removing attributes in
the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> being processed. Obviously changing the LogRecord needs
to be done with some care, but it does allow the injection of contextual
information into logs (see <aclass="reference internal"href="https://docs.python.org/3/howto/logging-cookbook.html#filters-contextual"><spanclass="std std-ref">Using Filters to impart contextual information</span></a>).</p>
</section>
<sectionid="logrecord-objects">
<spanid="log-record"></span><h2>LogRecord Objects<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logrecord-objects"title="Permalink to this headline">¶</a></h2>
<p><aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> instances are created automatically by the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger"title="logging.Logger"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Logger</span></code></a>
every time something is logged, and can be created manually via
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.makeLogRecord"title="logging.makeLogRecord"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">makeLogRecord()</span></code></a> (for example, from a pickled event received over the
<emclass="property"><spanclass="pre">class</span><spanclass="w"></span></em><spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">LogRecord</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">name</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">pathname</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">lineno</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">exc_info</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">func</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">None</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">sinfo</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">None</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="Permalink to this definition">¶</a></dt>
<dd><p>Contains all the information pertinent to the event being logged.</p>
<p>The primary information is passed in <em>msg</em> and <em>args</em>,
which are combined using <codeclass="docutils literal notranslate"><spanclass="pre">msg</span><spanclass="pre">%</span><spanclass="pre">args</span></code> to create
the <codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">message</span></code> attribute of the record.</p>
<dlclass="field-list simple">
<dtclass="field-odd">Parameters</dt>
<ddclass="field-odd"><ulclass="simple">
<li><p><strong>name</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str"title="str"><em>str</em></a>) – The name of the logger used to log the event
represented by this <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code>.
Note that the logger name in the <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code>
will always have this value,
even though it may be emitted by a handler
attached to a different (ancestor) logger.</p></li>
<li><p><strong>level</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/functions.html#int"title="int"><em>int</em></a>) – The <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#levels"><spanclass="std std-ref">numeric level</span></a> of the logging event
(such as <codeclass="docutils literal notranslate"><spanclass="pre">10</span></code> for <codeclass="docutils literal notranslate"><spanclass="pre">DEBUG</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">20</span></code> for <codeclass="docutils literal notranslate"><spanclass="pre">INFO</span></code>, etc).
Note that this is converted to <em>two</em> attributes of the LogRecord:
<codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">levelno</span></code> for the numeric value
and <codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">levelname</span></code> for the corresponding level name.</p></li>
<li><p><strong>pathname</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str"title="str"><em>str</em></a>) – The full string path of the source file
where the logging call was made.</p></li>
<li><p><strong>lineno</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/functions.html#int"title="int"><em>int</em></a>) – The line number in the source file
where the logging call was made.</p></li>
<li><p><strong>msg</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/typing.html#typing.Any"title="typing.Any"><em>Any</em></a>) – The event description message,
which can be a %-format string with placeholders for variable data,
or an arbitrary object (see <aclass="reference internal"href="https://docs.python.org/3/howto/logging.html#arbitrary-object-messages"><spanclass="std std-ref">Using arbitrary objects as messages</span></a>).</p></li>
<li><p><strong>args</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#tuple"title="tuple"><em>tuple</em></a><em> | </em><aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#dict"title="dict"><em>dict</em></a><em>[</em><aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str"title="str"><em>str</em></a><em>, </em><aclass="reference internal"href="https://docs.python.org/3/library/typing.html#typing.Any"title="typing.Any"><em>Any</em></a><em>]</em>) – Variable data to merge into the <em>msg</em> argument
to obtain the event description.</p></li>
<li><p><strong>exc_info</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#tuple"title="tuple"><em>tuple</em></a><em>[</em><aclass="reference internal"href="https://docs.python.org/3/library/functions.html#type"title="type"><em>type</em></a><em>[</em><aclass="reference internal"href="https://docs.python.org/3/library/exceptions.html#BaseException"title="BaseException"><em>BaseException</em></a><em>]</em><em>, </em><aclass="reference internal"href="https://docs.python.org/3/library/exceptions.html#BaseException"title="BaseException"><em>BaseException</em></a><em>, </em><aclass="reference internal"href="https://docs.python.org/3/library/types.html#types.TracebackType"title="types.TracebackType"><em>types.TracebackType</em></a><em>] </em><em>| </em><em>None</em>) – An exception tuple with the current exception information,
as returned by <aclass="reference internal"href="https://docs.python.org/3/library/sys.html#sys.exc_info"title="sys.exc_info"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">sys.exc_info()</span></code></a>,
or <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> if no exception information is available.</p></li>
<li><p><strong>func</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str"title="str"><em>str</em></a><em> | </em><em>None</em>) – The name of the function or method
from which the logging call was invoked.</p></li>
<li><p><strong>sinfo</strong> (<aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str"title="str"><em>str</em></a><em> | </em><em>None</em>) – A text string representing stack information
<spanclass="sig-name descname"><spanclass="pre">getMessage</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.LogRecord.getMessage"title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the message for this <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> instance after merging any
user-supplied arguments with the message. If the user-supplied message
argument to the logging call is not a string, <aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str"title="str"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">str()</span></code></a> is called on it to
convert it to a string. This allows use of user-defined classes as
messages, whose <codeclass="docutils literal notranslate"><spanclass="pre">__str__</span></code> method can return the actual format string to
be used.</p>
</dd></dl>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>The creation of a <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> has been made more configurable by
providing a factory which is used to create the record. The factory can be
set using <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.getLogRecordFactory"title="logging.getLogRecordFactory"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">getLogRecordFactory()</span></code></a> and <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.setLogRecordFactory"title="logging.setLogRecordFactory"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">setLogRecordFactory()</span></code></a>
(see this for the factory’s signature).</p>
</div>
<p>This functionality can be used to inject your own values into a
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> at creation time. You can use the following pattern:</p>
<p>With this pattern, multiple factories could be chained, and as long
as they don’t overwrite each other’s attributes or unintentionally
overwrite the standard attributes listed above, there should be no
surprises.</p>
</dd></dl>
</section>
<sectionid="logrecord-attributes">
<spanid="id2"></span><h2>LogRecord attributes<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logrecord-attributes"title="Permalink to this headline">¶</a></h2>
<p>The LogRecord has a number of attributes, most of which are derived from the
parameters to the constructor. (Note that the names do not always correspond
exactly between the LogRecord constructor parameters and the LogRecord
attributes.) These attributes can be used to merge data from the record into
the format string. The following table lists (in alphabetical order) the
attribute names, their meanings and the corresponding placeholder in a %-style
format string.</p>
<p>If you are using {}-formatting (<aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str.format"title="str.format"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">str.format()</span></code></a>), you can use
<codeclass="docutils literal notranslate"><spanclass="pre">{attrname}</span></code> as the placeholder in the format string. If you are using
$-formatting (<aclass="reference internal"href="https://docs.python.org/3/library/string.html#string.Template"title="string.Template"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">string.Template</span></code></a>), use the form <codeclass="docutils literal notranslate"><spanclass="pre">${attrname}</span></code>. In
both cases, of course, replace <codeclass="docutils literal notranslate"><spanclass="pre">attrname</span></code> with the actual attribute name
you want to use.</p>
<p>In the case of {}-formatting, you can specify formatting flags by placing them
after the attribute name, separated from it with a colon. For example: a
placeholder of <codeclass="docutils literal notranslate"><spanclass="pre">{msecs:03d}</span></code> would format a millisecond value of <codeclass="docutils literal notranslate"><spanclass="pre">4</span></code> as
<codeclass="docutils literal notranslate"><spanclass="pre">004</span></code>. Refer to the <aclass="reference internal"href="https://docs.python.org/3/library/stdtypes.html#str.format"title="str.format"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">str.format()</span></code></a> documentation for full details on
<td><p>Time when the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> was created
(as returned by <aclass="reference internal"href="https://docs.python.org/3/library/time.html#time.time"title="time.time"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">time.time()</span></code></a>).</p></td>
</tr>
<trclass="row-odd"><td><p>exc_info</p></td>
<td><p>You shouldn’t need to
format this yourself.</p></td>
<td><p>Exception tuple (à la <codeclass="docutils literal notranslate"><spanclass="pre">sys.exc_info</span></code>) or,
if no exception has occurred, <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>.</p></td>
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> was created.</p></td>
</tr>
<trclass="row-even"><td><p>msg</p></td>
<td><p>You shouldn’t need to
format this yourself.</p></td>
<td><p>The format string passed in the original
logging call. Merged with <codeclass="docutils literal notranslate"><spanclass="pre">args</span></code> to
produce <codeclass="docutils literal notranslate"><spanclass="pre">message</span></code>, or an arbitrary object
(see <aclass="reference internal"href="https://docs.python.org/3/howto/logging.html#arbitrary-object-messages"><spanclass="std std-ref">Using arbitrary objects as messages</span></a>).</p></td>
<p><spanclass="versionmodified changed">Changed in version 3.1: </span><em>processName</em> was added.</p>
</div>
</section>
<sectionid="loggeradapter-objects">
<spanid="logger-adapter"></span><h2>LoggerAdapter Objects<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#loggeradapter-objects"title="Permalink to this headline">¶</a></h2>
<p><aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LoggerAdapter"title="logging.LoggerAdapter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LoggerAdapter</span></code></a> instances are used to conveniently pass contextual
information into logging calls. For a usage example, see the section on
<aclass="reference internal"href="https://docs.python.org/3/howto/logging-cookbook.html#context-info"><spanclass="std std-ref">adding contextual information to your logging output</span></a>.</p>
<emclass="property"><spanclass="pre">class</span><spanclass="w"></span></em><spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">LoggerAdapter</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">logger</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">extra</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.LoggerAdapter"title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an instance of <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LoggerAdapter"title="logging.LoggerAdapter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LoggerAdapter</span></code></a> initialized with an
underlying <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger"title="logging.Logger"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Logger</span></code></a> instance and a dict-like object.</p>
<spanclass="sig-name descname"><spanclass="pre">process</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.LoggerAdapter.process"title="Permalink to this definition">¶</a></dt>
<dd><p>Modifies the message and/or keyword arguments passed to a logging call in
order to insert contextual information. This implementation takes the object
passed as <em>extra</em> to the constructor and adds it to <em>kwargs</em> using key
‘extra’. The return value is a (<em>msg</em>, <em>kwargs</em>) tuple which has the
(possibly modified) versions of the arguments passed in.</p>
<spanclass="sig-name descname"><spanclass="pre">manager</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.LoggerAdapter.manager"title="Permalink to this definition">¶</a></dt>
<dd><p>Delegates to the underlying <codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">manager`</span></code> on <em>logger</em>.</p>
<spanclass="sig-name descname"><spanclass="pre">_log</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.LoggerAdapter._log"title="Permalink to this definition">¶</a></dt>
<dd><p>Delegates to the underlying <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">_log`()</span></code> method on <em>logger</em>.</p>
</dd></dl>
<p>In addition to the above, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LoggerAdapter"title="logging.LoggerAdapter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LoggerAdapter</span></code></a> supports the following
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.hasHandlers"title="logging.Logger.hasHandlers"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">hasHandlers()</span></code></a>. These methods have the same signatures as their
counterparts in <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger"title="logging.Logger"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Logger</span></code></a>, so you can use the two types of instances
interchangeably.</p>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>The <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.isEnabledFor"title="logging.Logger.isEnabledFor"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">isEnabledFor()</span></code></a>, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.getEffectiveLevel"title="logging.Logger.getEffectiveLevel"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">getEffectiveLevel()</span></code></a>,
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.setLevel"title="logging.Logger.setLevel"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">setLevel()</span></code></a> and <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger.hasHandlers"title="logging.Logger.hasHandlers"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">hasHandlers()</span></code></a> methods were added
to <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LoggerAdapter"title="logging.LoggerAdapter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LoggerAdapter</span></code></a>. These methods delegate to the underlying logger.</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.6: </span>Attribute <codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">manager</span></code> and method <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">_log()</span></code> were added, which
delegate to the underlying logger and allow adapters to be nested.</p>
</div>
</dd></dl>
</section>
<sectionid="thread-safety">
<h2>Thread Safety<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#thread-safety"title="Permalink to this headline">¶</a></h2>
<p>The logging module is intended to be thread-safe without any special work
needing to be done by its clients. It achieves this though using threading
locks; there is one lock to serialize access to the module’s shared data, and
each handler also creates a lock to serialize access to its underlying I/O.</p>
<p>If you are implementing asynchronous signal handlers using the <aclass="reference internal"href="https://docs.python.org/3/library/signal.html#module-signal"title="signal: Set handlers for asynchronous events."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">signal</span></code></a>
module, you may not be able to use logging from within such handlers. This is
because lock implementations in the <aclass="reference internal"href="https://docs.python.org/3/library/threading.html#module-threading"title="threading: Thread-based parallelism."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">threading</span></code></a> module are not always
re-entrant, and so cannot be invoked from such signal handlers.</p>
</section>
<sectionid="module-level-functions">
<h2>Module-Level Functions<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#module-level-functions"title="Permalink to this headline">¶</a></h2>
<p>In addition to the classes described above, there are a number of module-level
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">getLogger</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">name</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">None</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.getLogger"title="Permalink to this definition">¶</a></dt>
<dd><p>Return a logger with the specified name or, if name is <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>, return a
logger which is the root logger of the hierarchy. If specified, the name is
typically a dot-separated hierarchical name like <em>‘a’</em>, <em>‘a.b’</em> or <em>‘a.b.c.d’</em>.
Choice of these names is entirely up to the developer who is using logging.</p>
<p>All calls to this function with a given name return the same logger instance.
This means that logger instances never need to be passed between different parts
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">getLoggerClass</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.getLoggerClass"title="Permalink to this definition">¶</a></dt>
<dd><p>Return either the standard <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger"title="logging.Logger"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Logger</span></code></a> class, or the last class passed to
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.setLoggerClass"title="logging.setLoggerClass"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">setLoggerClass()</span></code></a>. This function may be called from within a new class
definition, to ensure that installing a customized <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Logger"title="logging.Logger"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Logger</span></code></a> class will
not undo customizations already applied by other code. For example:</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">getLogRecordFactory</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.getLogRecordFactory"title="Permalink to this definition">¶</a></dt>
<dd><p>Return a callable which is used to create a <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a>.</p>
<divclass="versionadded">
<p><spanclass="versionmodified added">New in version 3.2: </span>This function has been provided, along with <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.setLogRecordFactory"title="logging.setLogRecordFactory"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">setLogRecordFactory()</span></code></a>,
to allow developers more control over how the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a>
representing a logging event is constructed.</p>
</div>
<p>See <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.setLogRecordFactory"title="logging.setLogRecordFactory"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">setLogRecordFactory()</span></code></a> for more information about the how the
factory is called.</p>
</dd></dl>
<dlclass="py function">
<dtclass="sig sig-object py"id="logging.debug">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">debug</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.debug"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.DEBUG"title="logging.DEBUG"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">DEBUG</span></code></a> on the root logger. The <em>msg</em> is the
message format string, and the <em>args</em> are the arguments which are merged into
<em>msg</em> using the string formatting operator. (Note that this means that you can
use keywords in the format string, together with a single dictionary argument.)</p>
<p>There are three keyword arguments in <em>kwargs</em> which are inspected: <em>exc_info</em>
which, if it does not evaluate as false, causes exception information to be
added to the logging message. If an exception tuple (in the format returned by
<aclass="reference internal"href="https://docs.python.org/3/library/sys.html#sys.exc_info"title="sys.exc_info"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">sys.exc_info()</span></code></a>) or an exception instance is provided, it is used;
otherwise, <aclass="reference internal"href="https://docs.python.org/3/library/sys.html#sys.exc_info"title="sys.exc_info"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">sys.exc_info()</span></code></a> is called to get the exception information.</p>
<p>The second optional keyword argument is <em>stack_info</em>, which defaults to
<codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>. If true, stack information is added to the logging
message, including the actual logging call. Note that this is not the same
stack information as that displayed through specifying <em>exc_info</em>: The
former is stack frames from the bottom of the stack up to the logging call
in the current thread, whereas the latter is information about stack frames
which have been unwound, following an exception, while searching for
exception handlers.</p>
<p>You can specify <em>stack_info</em> independently of <em>exc_info</em>, e.g. to just show
how you got to a certain point in your code, even when no exceptions were
raised. The stack frames are printed following a header line which says:</p>
<p>This mimics the <codeclass="docutils literal notranslate"><spanclass="pre">Traceback</span><spanclass="pre">(most</span><spanclass="pre">recent</span><spanclass="pre">call</span><spanclass="pre">last):</span></code> which is used when
displaying exception frames.</p>
<p>The third optional keyword argument is <em>extra</em> which can be used to pass a
dictionary which is used to populate the __dict__ of the LogRecord created for
the logging event with user-defined attributes. These custom attributes can then
be used as you like. For example, they could be incorporated into logged
<p>The keys in the dictionary passed in <em>extra</em> should not clash with the keys used
by the logging system. (See the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> documentation for more
information on which keys are used by the logging system.)</p>
<p>If you choose to use these attributes in logged messages, you need to exercise
some care. In the above example, for instance, the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> has been
set up with a format string which expects ‘clientip’ and ‘user’ in the attribute
dictionary of the LogRecord. If these are missing, the message will not be
logged because a string formatting exception will occur. So in this case, you
always need to pass the <em>extra</em> dictionary with these keys.</p>
<p>While this might be annoying, this feature is intended for use in specialized
circumstances, such as multi-threaded servers where the same code executes in
many contexts, and interesting conditions which arise are dependent on this
context (such as remote client IP address and authenticated user name, in the
above example). In such circumstances, it is likely that specialized
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a>s would be used with particular <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Handler"title="logging.Handler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Handler</span></code></a>s.</p>
<p>This function (as well as <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.info"title="logging.info"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">info()</span></code></a>, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.warning"title="logging.warning"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">warning()</span></code></a>, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.error"title="logging.error"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">error()</span></code></a> and
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.critical"title="logging.critical"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">critical()</span></code></a>) will call <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.basicConfig"title="logging.basicConfig"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">basicConfig()</span></code></a> if the root logger doesn’t
have any handler attached.</p>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>The <em>stack_info</em> parameter was added.</p>
</div>
</dd></dl>
<dlclass="py function">
<dtclass="sig sig-object py"id="logging.info">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">info</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.info"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.INFO"title="logging.INFO"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">INFO</span></code></a> on the root logger. The arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
</dd></dl>
<dlclass="py function">
<dtclass="sig sig-object py"id="logging.warning">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">warning</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.warning"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.WARNING"title="logging.WARNING"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">WARNING</span></code></a> on the root logger. The arguments
are interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>There is an obsolete function <codeclass="docutils literal notranslate"><spanclass="pre">warn</span></code> which is functionally
identical to <codeclass="docutils literal notranslate"><spanclass="pre">warning</span></code>. As <codeclass="docutils literal notranslate"><spanclass="pre">warn</span></code> is deprecated, please do not use
it - use <codeclass="docutils literal notranslate"><spanclass="pre">warning</span></code> instead.</p>
</div>
</dd></dl>
<dlclass="py function">
<dtclass="sig sig-object py"id="logging.error">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">error</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.error"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.ERROR"title="logging.ERROR"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">ERROR</span></code></a> on the root logger. The arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">critical</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.critical"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.CRITICAL"title="logging.CRITICAL"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">CRITICAL</span></code></a> on the root logger. The arguments
are interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">exception</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.exception"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.ERROR"title="logging.ERROR"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">ERROR</span></code></a> on the root logger. The arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">debug()</span></code></a>. Exception info is added to the logging
message. This function should only be called from an exception handler.</p>
</dd></dl>
<dlclass="py function">
<dtclass="sig sig-object py"id="logging.log">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">log</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">msg</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">*</span></span><spanclass="n"><spanclass="pre">args</span></span></em>, <emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.log"title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <em>level</em> on the root logger. The other arguments are
interpreted as for <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.debug"title="logging.debug"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">debug()</span></code></a>.</p>
</dd></dl>
<dlclass="py function">
<dtclass="sig sig-object py"id="logging.disable">
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">disable</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span><spanclass="o"><spanclass="pre">=</span></span><spanclass="default_value"><spanclass="pre">CRITICAL</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.disable"title="Permalink to this definition">¶</a></dt>
<dd><p>Provides an overriding level <em>level</em> for all loggers which takes precedence over
the logger’s own level. When the need arises to temporarily throttle logging
output down across the whole application, this function can be useful. Its
effect is to disable all logging calls of severity <em>level</em> and below, so that
if you call it with a value of INFO, then all INFO and DEBUG events would be
discarded, whereas those of severity WARNING and above would be processed
according to the logger’s effective level. If
<codeclass="docutils literal notranslate"><spanclass="pre">logging.disable(logging.NOTSET)</span></code> is called, it effectively removes this
overriding level, so that logging output again depends on the effective
levels of individual loggers.</p>
<p>Note that if you have defined any custom logging level higher than
<codeclass="docutils literal notranslate"><spanclass="pre">CRITICAL</span></code> (this is not recommended), you won’t be able to rely on the
default value for the <em>level</em> parameter, but will have to explicitly supply a
suitable value.</p>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.7: </span>The <em>level</em> parameter was defaulted to level <codeclass="docutils literal notranslate"><spanclass="pre">CRITICAL</span></code>. See
<aclass="reference external"href="https://bugs.python.org/issue?@action=redirect&bpo=28524">bpo-28524</a> for more information about this change.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">addLevelName</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span></em>, <emclass="sig-param"><spanclass="n"><spanclass="pre">levelName</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.addLevelName"title="Permalink to this definition">¶</a></dt>
<dd><p>Associates level <em>level</em> with text <em>levelName</em> in an internal dictionary, which is
used to map numeric levels to a textual representation, for example when a
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> formats a message. This function can also be used to define
your own levels. The only constraints are that all levels used must be
registered using this function, levels should be positive integers and they
should increase in increasing order of severity.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>If you are thinking of defining your own levels, please see the
section on <aclass="reference internal"href="https://docs.python.org/3/howto/logging.html#custom-levels"><spanclass="std std-ref">Custom Levels</span></a>.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">getLevelNamesMapping</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.getLevelNamesMapping"title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a mapping from level names to their corresponding logging levels. For example, the
string “CRITICAL” maps to <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.CRITICAL"title="logging.CRITICAL"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">CRITICAL</span></code></a>. The returned mapping is copied from an internal
mapping on each call to this function.</p>
<divclass="versionadded">
<p><spanclass="versionmodified added">New in version 3.11.</span></p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">getLevelName</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">level</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.getLevelName"title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the textual or numeric representation of logging level <em>level</em>.</p>
<p>If <em>level</em> is one of the predefined levels <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.CRITICAL"title="logging.CRITICAL"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">CRITICAL</span></code></a>, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.ERROR"title="logging.ERROR"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">ERROR</span></code></a>,
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.WARNING"title="logging.WARNING"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">WARNING</span></code></a>, <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.INFO"title="logging.INFO"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">INFO</span></code></a> or <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.DEBUG"title="logging.DEBUG"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">DEBUG</span></code></a> then you get the
corresponding string. If you have associated levels with names using
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.addLevelName"title="logging.addLevelName"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">addLevelName()</span></code></a> then the name you have associated with <em>level</em> is
returned. If a numeric value corresponding to one of the defined levels is
passed in, the corresponding string representation is returned.</p>
<p>The <em>level</em> parameter also accepts a string representation of the level such
as ‘INFO’. In such cases, this functions returns the corresponding numeric
value of the level.</p>
<p>If no matching numeric or string value is passed in, the string
‘Level %s’ % level is returned.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>Levels are internally integers (as they need to be compared in the
logging logic). This function is used to convert between an integer level
and the level name displayed in the formatted log output by means of the
<codeclass="docutils literal notranslate"><spanclass="pre">%(levelname)s</span></code> format specifier (see <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logrecord-attributes"><spanclass="std std-ref">LogRecord attributes</span></a>), and
vice versa.</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.4: </span>In Python versions earlier than 3.4, this function could also be passed a
text level, and would return the corresponding numeric value of the level.
This undocumented behaviour was considered a mistake, and was removed in
Python 3.4, but reinstated in 3.4.2 due to retain backward compatibility.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">makeLogRecord</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">attrdict</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.makeLogRecord"title="Permalink to this definition">¶</a></dt>
<dd><p>Creates and returns a new <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> instance whose attributes are
defined by <em>attrdict</em>. This function is useful for taking a pickled
<aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> attribute dictionary, sent over a socket, and reconstituting
it as a <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> instance at the receiving end.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">basicConfig</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="o"><spanclass="pre">**</span></span><spanclass="n"><spanclass="pre">kwargs</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.basicConfig"title="Permalink to this definition">¶</a></dt>
<dd><p>Does basic configuration for the logging system by creating a
<aclass="reference internal"href="https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler"title="logging.StreamHandler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">StreamHandler</span></code></a> with a default <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.Formatter"title="logging.Formatter"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> and adding it to the
<td><p>Specifies that a <aclass="reference internal"href="https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler"title="logging.FileHandler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">FileHandler</span></code></a> be
created, using the specified filename,
rather than a <aclass="reference internal"href="https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler"title="logging.StreamHandler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">StreamHandler</span></code></a>.</p></td>
<td><p>If <em>filename</em> is specified, open the file
in this <aclass="reference internal"href="https://docs.python.org/3/library/functions.html#filemodes"><spanclass="std std-ref">mode</span></a>. Defaults
to <codeclass="docutils literal notranslate"><spanclass="pre">'a'</span></code>.</p></td>
<aclass="reference internal"href="https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler"title="logging.StreamHandler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">StreamHandler</span></code></a>. Note that this
argument is incompatible with <em>filename</em> -
if both are present, a <codeclass="docutils literal notranslate"><spanclass="pre">ValueError</span></code> is
<td><p>If this keyword argument is specified along
with <em>filename</em>, its value is used when the
<aclass="reference internal"href="https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler"title="logging.FileHandler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">FileHandler</span></code></a> is created, and thus
used when opening the output file. If not
specified, the value ‘backslashreplace’ is
used. Note that if <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> is specified,
it will be passed as such to <aclass="reference internal"href="https://docs.python.org/3/library/functions.html#open"title="open"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">open()</span></code></a>,
which means that it will be treated the
same as passing ‘errors’.</p></td>
</tr>
</tbody>
</table></div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.2: </span>The <em>style</em> argument was added.</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.3: </span>The <em>handlers</em> argument was added. Additional checks were added to
catch situations where incompatible arguments are specified (e.g.
<em>handlers</em> together with <em>stream</em> or <em>filename</em>, or <em>stream</em>
together with <em>filename</em>).</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.8: </span>The <em>force</em> argument was added.</p>
</div>
<divclass="versionchanged">
<p><spanclass="versionmodified changed">Changed in version 3.9: </span>The <em>encoding</em> and <em>errors</em> arguments were added.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">shutdown</span></span><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.shutdown"title="Permalink to this definition">¶</a></dt>
<dd><p>Informs the logging system to perform an orderly shutdown by flushing and
closing all handlers. This should be called at application exit and no
further use of the logging system should be made after this call.</p>
<p>When the logging module is imported, it registers this function as an exit
handler (see <aclass="reference internal"href="https://docs.python.org/3/library/atexit.html#module-atexit"title="atexit: Register and execute cleanup functions."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">atexit</span></code></a>), so normally there’s no need to do that
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">setLoggerClass</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">klass</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.setLoggerClass"title="Permalink to this definition">¶</a></dt>
<dd><p>Tells the logging system to use the class <em>klass</em> when instantiating a logger.
The class should define <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__init__()</span></code> such that only a name argument is
required, and the <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__init__()</span></code> should call <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">Logger.__init__()</span></code>. This
function is typically called before any loggers are instantiated by applications
which need to use custom logger behavior. After this call, as at any other
time, do not instantiate loggers directly using the subclass: continue to use
the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.getLogger"title="logging.getLogger"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">logging.getLogger()</span></code></a> API to get your loggers.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">setLogRecordFactory</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">factory</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.setLogRecordFactory"title="Permalink to this definition">¶</a></dt>
<dd><p>Set a callable which is used to create a <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a>.</p>
<dlclass="field-list simple">
<dtclass="field-odd">Parameters</dt>
<ddclass="field-odd"><p><strong>factory</strong>– The factory callable to be used to instantiate a log record.</p>
</dd>
</dl>
<divclass="versionadded">
<p><spanclass="versionmodified added">New in version 3.2: </span>This function has been provided, along with <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.getLogRecordFactory"title="logging.getLogRecordFactory"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">getLogRecordFactory()</span></code></a>, to
allow developers more control over how the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.LogRecord"title="logging.LogRecord"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">LogRecord</span></code></a> representing
<h2>Module-Level Attributes<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#module-level-attributes"title="Permalink to this headline">¶</a></h2>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">lastResort</span></span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.lastResort"title="Permalink to this definition">¶</a></dt>
<dd><p>A “handler of last resort” is available through this attribute. This
is a <aclass="reference internal"href="https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler"title="logging.StreamHandler"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">StreamHandler</span></code></a> writing to <codeclass="docutils literal notranslate"><spanclass="pre">sys.stderr</span></code> with a level of
<codeclass="docutils literal notranslate"><spanclass="pre">WARNING</span></code>, and is used to handle logging events in the absence of any
logging configuration. The end result is to just print the message to
<codeclass="docutils literal notranslate"><spanclass="pre">sys.stderr</span></code>. This replaces the earlier error message saying that
“no handlers could be found for logger XYZ”. If you need the earlier
behaviour for some reason, <codeclass="docutils literal notranslate"><spanclass="pre">lastResort</span></code> can be set to <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>.</p>
<divclass="versionadded">
<p><spanclass="versionmodified added">New in version 3.2.</span></p>
<h2>Integration with the warnings module<aclass="headerlink"href="https://docs.python.org/3/library/logging.html#integration-with-the-warnings-module"title="Permalink to this headline">¶</a></h2>
<p>The <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.captureWarnings"title="logging.captureWarnings"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">captureWarnings()</span></code></a> function can be used to integrate <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#module-logging"title="logging: Flexible event logging system for applications."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">logging</span></code></a>
with the <aclass="reference internal"href="https://docs.python.org/3/library/warnings.html#module-warnings"title="warnings: Issue warning messages and control their disposition."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">warnings</span></code></a> module.</p>
<spanclass="sig-prename descclassname"><spanclass="pre">logging.</span></span><spanclass="sig-name descname"><spanclass="pre">captureWarnings</span></span><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n"><spanclass="pre">capture</span></span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="https://docs.python.org/3/library/logging.html#logging.captureWarnings"title="Permalink to this definition">¶</a></dt>
<dd><p>This function is used to turn the capture of warnings by logging on and
off.</p>
<p>If <em>capture</em> is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>, warnings issued by the <aclass="reference internal"href="https://docs.python.org/3/library/warnings.html#module-warnings"title="warnings: Issue warning messages and control their disposition."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">warnings</span></code></a> module will
be redirected to the logging system. Specifically, a warning will be
formatted using <aclass="reference internal"href="https://docs.python.org/3/library/warnings.html#warnings.formatwarning"title="warnings.formatwarning"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">warnings.formatwarning()</span></code></a> and the resulting string
logged to a logger named <codeclass="docutils literal notranslate"><spanclass="pre">'py.warnings'</span></code> with a severity of <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#logging.WARNING"title="logging.WARNING"><codeclass="xref py py-const docutils literal notranslate"><spanclass="pre">WARNING</span></code></a>.</p>
<p>If <em>capture</em> is <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>, the redirection of warnings to the logging system
will stop, and warnings will be redirected to their original destinations
(i.e. those in effect before <codeclass="docutils literal notranslate"><spanclass="pre">captureWarnings(True)</span></code> was called).</p>
</dd></dl>
<divclass="admonition seealso">
<pclass="admonition-title">See also</p>
<dlclass="simple">
<dt>Module <aclass="reference internal"href="https://docs.python.org/3/library/logging.config.html#module-logging.config"title="logging.config: Configuration of the logging module."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">logging.config</span></code></a></dt><dd><p>Configuration API for the logging module.</p>
</dd>
<dt>Module <aclass="reference internal"href="https://docs.python.org/3/library/logging.handlers.html#module-logging.handlers"title="logging.handlers: Handlers for the logging module."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">logging.handlers</span></code></a></dt><dd><p>Useful handlers included with the logging module.</p>
</dd>
<dt><spanclass="target"id="index-1"></span><aclass="pep reference external"href="https://peps.python.org/pep-0282/"><strong>PEP 282</strong></a> - A Logging System</dt><dd><p>The proposal which described this feature for inclusion in the Python standard
library.</p>
</dd>
<dt><aclass="reference external"href="https://old.red-dove.com/python_logging.html">Original Python logging package</a></dt><dd><p>This is the original source for the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#module-logging"title="logging: Flexible event logging system for applications."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">logging</span></code></a> package. The version of the
package available from this site is suitable for use with Python 1.5.2, 2.1.x
and 2.2.x, which do not include the <aclass="reference internal"href="https://docs.python.org/3/library/logging.html#module-logging"title="logging: Flexible event logging system for applications."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">logging</span></code></a> package in the standard
<li><aclass="reference internal"href="https://docs.python.org/3/library/logging.html#integration-with-the-warnings-module">Integration with the warnings module</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<pclass="topless"><ahref="https://docs.python.org/3/library/getopt.html"title="previous chapter"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">getopt</span></code> — C-style parser for command line options</a></p>