parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="logging.config — Logging configuration" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/logging.config.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/logging/config.py Important: This page contains only reference information. For tutorials, please see Basic Tutorial, Advanced Tutorial, Logging Cookbook. This section describes th..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_logging.config_34dbd393.png" />15<meta property="og:image:alt" content="Source code: Lib/logging/config.py Important: This page contains only reference information. For tutorials, please see Basic Tutorial, Advanced Tutorial, Logging Cookbook. This section describes th..." />16<meta name="description" content="Source code: Lib/logging/config.py Important: This page contains only reference information. For tutorials, please see Basic Tutorial, Advanced Tutorial, Logging Cookbook. This section describes th..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>logging.config — Logging configuration — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="logging.handlers — Logging handlers" href="logging.handlers.html" />43 <link rel="prev" title="logging — Logging facility for Python" href="logging.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/logging.config.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code> — Logging configuration</a><ul>108<li><a class="reference internal" href="#configuration-functions">Configuration functions</a></li>109<li><a class="reference internal" href="#security-considerations">Security considerations</a></li>110<li><a class="reference internal" href="#configuration-dictionary-schema">Configuration dictionary schema</a><ul>111<li><a class="reference internal" href="#dictionary-schema-details">Dictionary Schema Details</a></li>112<li><a class="reference internal" href="#incremental-configuration">Incremental Configuration</a></li>113<li><a class="reference internal" href="#object-connections">Object connections</a></li>114<li><a class="reference internal" href="#user-defined-objects">User-defined objects</a></li>115<li><a class="reference internal" href="#handler-configuration-order">Handler configuration order</a></li>116<li><a class="reference internal" href="#access-to-external-objects">Access to external objects</a></li>117<li><a class="reference internal" href="#access-to-internal-objects">Access to internal objects</a></li>118<li><a class="reference internal" href="#import-resolution-and-custom-importers">Import resolution and custom importers</a></li>119<li><a class="reference internal" href="#configuring-queuehandler-and-queuelistener">Configuring QueueHandler and QueueListener</a></li>120</ul>121</li>122<li><a class="reference internal" href="#configuration-file-format">Configuration file format</a></li>123</ul>124</li>125</ul>126 127 </div>128 <div>129 <h4>Previous topic</h4>130 <p class="topless"><a href="logging.html"131 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></p>132 </div>133 <div>134 <h4>Next topic</h4>135 <p class="topless"><a href="logging.handlers.html"136 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.handlers</span></code> — Logging handlers</a></p>137 </div>138 <script>139 document.addEventListener('DOMContentLoaded', () => {140 const title = document.querySelector('meta[property="og:title"]').content;141 const elements = document.querySelectorAll('.improvepage');142 const pageurl = window.location.href.split('?')[0];143 elements.forEach(element => {144 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));145 url.searchParams.set('pagetitle', title);146 url.searchParams.set('pageurl', pageurl);147 url.searchParams.set('pagesource', "library/logging.config.rst");148 element.href = url.toString();149 });150 });151 </script>152 <div role="note" aria-label="source link">153 <h3>This page</h3>154 <ul class="this-page-menu">155 <li><a href="../bugs.html">Report a bug</a></li>156 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>157 <li>158 <a href="https://github.com/python/cpython/blob/main/Doc/library/logging.config.rst?plain=1"159 rel="nofollow">Show source160 </a>161 </li>162 163 </ul>164 </div>165 </nav>166 </div>167</div>168 169 170 <div class="related" role="navigation" aria-label="Related">171 <h3>Navigation</h3>172 <ul>173 <li class="right" style="margin-right: 10px">174 <a href="../genindex.html" title="General Index"175 accesskey="I">index</a></li>176 <li class="right" >177 <a href="../py-modindex.html" title="Python Module Index"178 >modules</a> |</li>179 <li class="right" >180 <a href="logging.handlers.html" title="logging.handlers — Logging handlers"181 accesskey="N">next</a> |</li>182 <li class="right" >183 <a href="logging.html" title="logging — Logging facility for Python"184 accesskey="P">previous</a> |</li>185 186 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>187 <li><a href="https://www.python.org/">Python</a> »</li>188 <li class="switchers">189 <div class="language_switcher_placeholder"></div>190 <div class="version_switcher_placeholder"></div>191 </li>192 <li>193 194 </li>195 <li id="cpython-language-and-version">196 <a href="../index.html">3.15.0a6 Documentation</a> »197 </li>198 199 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>200 <li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">Generic Operating System Services</a> »</li>201 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code> — Logging configuration</a></li>202 <li class="right">203 204 205 <div class="inline-search" role="search">206 <form class="inline-search" action="../search.html" method="get">207 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">208 <input type="submit" value="Go">209 </form>210 </div>211 |212 </li>213 <li class="right">214<label class="theme-selector-label">215 Theme216 <select class="theme-selector" oninput="activateTheme(this.value)">217 <option value="auto" selected>Auto</option>218 <option value="light">Light</option>219 <option value="dark">Dark</option>220 </select>221</label> |</li>222 223 </ul>224 </div> 225 226 <div class="document">227 <div class="documentwrapper">228 <div class="bodywrapper">229 <div class="body" role="main">230 231 <section id="module-logging.config">232<span id="logging-config-logging-configuration"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code> — Logging configuration<a class="headerlink" href="#module-logging.config" title="Link to this heading">¶</a></h1>233<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/logging/config.py">Lib/logging/config.py</a></p>234<aside class="sidebar">235<p class="sidebar-title">Important</p>236<p>This page contains only reference information. For tutorials,237please see</p>238<ul class="simple">239<li><p><a class="reference internal" href="../howto/logging.html#logging-basic-tutorial"><span class="std std-ref">Basic Tutorial</span></a></p></li>240<li><p><a class="reference internal" href="../howto/logging.html#logging-advanced-tutorial"><span class="std std-ref">Advanced Tutorial</span></a></p></li>241<li><p><a class="reference internal" href="../howto/logging-cookbook.html#logging-cookbook"><span class="std std-ref">Logging Cookbook</span></a></p></li>242</ul>243</aside>244<hr class="docutils" />245<p>This section describes the API for configuring the logging module.</p>246<section id="configuration-functions">247<span id="logging-config-api"></span><h2>Configuration functions<a class="headerlink" href="#configuration-functions" title="Link to this heading">¶</a></h2>248<p>The following functions configure the logging module. They are located in the249<code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code> module. Their use is optional — you can configure the250logging module using these functions or by making calls to the main API (defined251in <a class="reference internal" href="logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> itself) and defining handlers which are declared either in252<code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> or <a class="reference internal" href="logging.handlers.html#module-logging.handlers" title="logging.handlers: Handlers for the logging module."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.handlers</span></code></a>.</p>253<dl class="py function">254<dt class="sig sig-object py" id="logging.config.dictConfig">255<span class="sig-prename descclassname"><span class="pre">logging.config.</span></span><span class="sig-name descname"><span class="pre">dictConfig</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">config</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.config.dictConfig" title="Link to this definition">¶</a></dt>256<dd><p>Takes the logging configuration from a dictionary. The contents of257this dictionary are described in <a class="reference internal" href="#logging-config-dictschema"><span class="std std-ref">Configuration dictionary schema</span></a>258below.</p>259<p>If an error is encountered during configuration, this function will260raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>, <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>, <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>261or <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> with a suitably descriptive message. The262following is a (possibly incomplete) list of conditions which will263raise an error:</p>264<ul class="simple">265<li><p>A <code class="docutils literal notranslate"><span class="pre">level</span></code> which is not a string or which is a string not266corresponding to an actual logging level.</p></li>267<li><p>A <code class="docutils literal notranslate"><span class="pre">propagate</span></code> value which is not a boolean.</p></li>268<li><p>An id which does not have a corresponding destination.</p></li>269<li><p>A non-existent handler id found during an incremental call.</p></li>270<li><p>An invalid logger name.</p></li>271<li><p>Inability to resolve to an internal or external object.</p></li>272</ul>273<p>Parsing is performed by the <code class="xref py py-class docutils literal notranslate"><span class="pre">DictConfigurator</span></code> class, whose274constructor is passed the dictionary used for configuration, and275has a <code class="xref py py-meth docutils literal notranslate"><span class="pre">configure()</span></code> method. The <code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code> module276has a callable attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">dictConfigClass</span></code>277which is initially set to <code class="xref py py-class docutils literal notranslate"><span class="pre">DictConfigurator</span></code>.278You can replace the value of <code class="xref py py-attr docutils literal notranslate"><span class="pre">dictConfigClass</span></code> with a279suitable implementation of your own.</p>280<p><code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code> calls <code class="xref py py-attr docutils literal notranslate"><span class="pre">dictConfigClass</span></code> passing281the specified dictionary, and then calls the <code class="xref py py-meth docutils literal notranslate"><span class="pre">configure()</span></code> method on282the returned object to put the configuration into effect:</p>283<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">dictConfig</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>284 <span class="n">dictConfigClass</span><span class="p">(</span><span class="n">config</span><span class="p">)</span><span class="o">.</span><span class="n">configure</span><span class="p">()</span>285</pre></div>286</div>287<p>For example, a subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">DictConfigurator</span></code> could call288<code class="docutils literal notranslate"><span class="pre">DictConfigurator.__init__()</span></code> in its own <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code>, then289set up custom prefixes which would be usable in the subsequent290<code class="xref py py-meth docutils literal notranslate"><span class="pre">configure()</span></code> call. <code class="xref py py-attr docutils literal notranslate"><span class="pre">dictConfigClass</span></code> would be bound to291this new subclass, and then <code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code> could be called exactly as292in the default, uncustomized state.</p>293<div class="versionadded">294<p><span class="versionmodified added">Added in version 3.2.</span></p>295</div>296</dd></dl>297 298<dl class="py function">299<dt class="sig sig-object py" id="logging.config.fileConfig">300<span class="sig-prename descclassname"><span class="pre">logging.config.</span></span><span class="sig-name descname"><span class="pre">fileConfig</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">defaults</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disable_existing_loggers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.config.fileConfig" title="Link to this definition">¶</a></dt>301<dd><p>Reads the logging configuration from a <a class="reference internal" href="configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">configparser</span></code></a>-format file. The302format of the file should be as described in303<a class="reference internal" href="#logging-config-fileformat"><span class="std std-ref">Configuration file format</span></a>.304This function can be called several times from an application, allowing an305end user to select from various pre-canned configurations (if the developer306provides a mechanism to present the choices and load the chosen307configuration).</p>308<p>It will raise <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> if the file309doesn’t exist and <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> if the file is invalid or310empty.</p>311<dl class="field-list simple">312<dt class="field-odd">Parameters<span class="colon">:</span></dt>313<dd class="field-odd"><ul class="simple">314<li><p><strong>fname</strong> – A filename, or a file-like object, or an instance derived315from <a class="reference internal" href="configparser.html#configparser.RawConfigParser" title="configparser.RawConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawConfigParser</span></code></a>. If a316<code class="xref py py-class docutils literal notranslate"><span class="pre">RawConfigParser</span></code>-derived instance is passed, it is used as317is. Otherwise, a <a class="reference internal" href="configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code></a> is318instantiated, and the configuration read by it from the319object passed in <code class="docutils literal notranslate"><span class="pre">fname</span></code>. If that has a <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python."><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a>320method, it is assumed to be a file-like object and read using321<a class="reference internal" href="configparser.html#configparser.ConfigParser.read_file" title="configparser.ConfigParser.read_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read_file()</span></code></a>; otherwise,322it is assumed to be a filename and passed to323<a class="reference internal" href="configparser.html#configparser.ConfigParser.read" title="configparser.ConfigParser.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a>.</p></li>324<li><p><strong>defaults</strong> – Defaults to be passed to the <code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code> can be specified325in this argument.</p></li>326<li><p><strong>disable_existing_loggers</strong> – If specified as <code class="docutils literal notranslate"><span class="pre">False</span></code>, loggers which327exist when this call is made are left328enabled. The default is <code class="docutils literal notranslate"><span class="pre">True</span></code> because this329enables old behaviour in a330backward-compatible way. This behaviour is to331disable any existing non-root loggers unless332they or their ancestors are explicitly named333in the logging configuration.</p></li>334<li><p><strong>encoding</strong> – The encoding used to open file when <em>fname</em> is filename.</p></li>335</ul>336</dd>337</dl>338<div class="versionchanged">339<p><span class="versionmodified changed">Changed in version 3.4: </span>An instance of a subclass of <a class="reference internal" href="configparser.html#configparser.RawConfigParser" title="configparser.RawConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawConfigParser</span></code></a> is340 now accepted as a value for <code class="docutils literal notranslate"><span class="pre">fname</span></code>. This facilitates:</p>341<blockquote>342<div><ul class="simple">343<li><p>Use of a configuration file where logging configuration is just part344of the overall application configuration.</p></li>345<li><p>Use of a configuration read from a file, and then modified by the using346application (e.g. based on command-line parameters or other aspects347of the runtime environment) before being passed to <code class="docutils literal notranslate"><span class="pre">fileConfig</span></code>.</p></li>348</ul>349</div></blockquote>350<div class="versionchanged">351<p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>encoding</em> parameter.</p>352</div>353<div class="versionchanged">354<p><span class="versionmodified changed">Changed in version 3.12: </span>An exception will be thrown if the provided file355doesn’t exist or is invalid or empty.</p>356</div>357</div>358</dd></dl>359 360<dl class="py function">361<dt class="sig sig-object py" id="logging.config.listen">362<span class="sig-prename descclassname"><span class="pre">logging.config.</span></span><span class="sig-name descname"><span class="pre">listen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">port</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEFAULT_LOGGING_CONFIG_PORT</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verify</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.config.listen" title="Link to this definition">¶</a></dt>363<dd><p>Starts up a socket server on the specified port, and listens for new364configurations. If no port is specified, the module’s default365<code class="xref py py-const docutils literal notranslate"><span class="pre">DEFAULT_LOGGING_CONFIG_PORT</span></code> is used. Logging configurations will be366sent as a file suitable for processing by <a class="reference internal" href="#logging.config.dictConfig" title="logging.config.dictConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code></a> or367<a class="reference internal" href="#logging.config.fileConfig" title="logging.config.fileConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileConfig()</span></code></a>. Returns a <a class="reference internal" href="threading.html#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> instance on which368you can call <a class="reference internal" href="threading.html#threading.Thread.start" title="threading.Thread.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a> to start the server, and which369you can <a class="reference internal" href="threading.html#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> when appropriate. To stop the server,370call <a class="reference internal" href="#logging.config.stopListening" title="logging.config.stopListening"><code class="xref py py-func docutils literal notranslate"><span class="pre">stopListening()</span></code></a>.</p>371<p>The <code class="docutils literal notranslate"><span class="pre">verify</span></code> argument, if specified, should be a callable which should372verify whether bytes received across the socket are valid and should be373processed. This could be done by encrypting and/or signing what is sent374across the socket, such that the <code class="docutils literal notranslate"><span class="pre">verify</span></code> callable can perform375signature verification and/or decryption. The <code class="docutils literal notranslate"><span class="pre">verify</span></code> callable is called376with a single argument - the bytes received across the socket - and should377return the bytes to be processed, or <code class="docutils literal notranslate"><span class="pre">None</span></code> to indicate that the bytes should378be discarded. The returned bytes could be the same as the passed in bytes379(e.g. when only verification is done), or they could be completely different380(perhaps if decryption were performed).</p>381<p>To send a configuration to the socket, read in the configuration file and382send it to the socket as a sequence of bytes preceded by a four-byte length383string packed in binary using <code class="docutils literal notranslate"><span class="pre">struct.pack('>L',</span> <span class="pre">n)</span></code>.</p>384<div class="admonition note" id="logging-eval-security">385<p class="admonition-title">Note</p>386<p>Because portions of the configuration are passed through387<a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>, use of this function may open its users to a security risk.388While the function only binds to a socket on <code class="docutils literal notranslate"><span class="pre">localhost</span></code>, and so does389not accept connections from remote machines, there are scenarios where390untrusted code could be run under the account of the process which calls391<code class="xref py py-func docutils literal notranslate"><span class="pre">listen()</span></code>. Specifically, if the process calling <code class="xref py py-func docutils literal notranslate"><span class="pre">listen()</span></code> runs392on a multi-user machine where users cannot trust each other, then a393malicious user could arrange to run essentially arbitrary code in a394victim user’s process, simply by connecting to the victim’s395<code class="xref py py-func docutils literal notranslate"><span class="pre">listen()</span></code> socket and sending a configuration which runs whatever396code the attacker wants to have executed in the victim’s process. This is397especially easy to do if the default port is used, but not hard even if a398different port is used. To avoid the risk of this happening, use the399<code class="docutils literal notranslate"><span class="pre">verify</span></code> argument to <code class="xref py py-func docutils literal notranslate"><span class="pre">listen()</span></code> to prevent unrecognised400configurations from being applied.</p>401</div>402<div class="versionchanged">403<p><span class="versionmodified changed">Changed in version 3.4: </span>The <code class="docutils literal notranslate"><span class="pre">verify</span></code> argument was added.</p>404</div>405<div class="admonition note">406<p class="admonition-title">Note</p>407<p>If you want to send configurations to the listener which don’t408disable existing loggers, you will need to use a JSON format for409the configuration, which will use <a class="reference internal" href="#logging.config.dictConfig" title="logging.config.dictConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code></a> for configuration.410This method allows you to specify <code class="docutils literal notranslate"><span class="pre">disable_existing_loggers</span></code> as411<code class="docutils literal notranslate"><span class="pre">False</span></code> in the configuration you send.</p>412</div>413</dd></dl>414 415<dl class="py function">416<dt class="sig sig-object py" id="logging.config.stopListening">417<span class="sig-prename descclassname"><span class="pre">logging.config.</span></span><span class="sig-name descname"><span class="pre">stopListening</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.config.stopListening" title="Link to this definition">¶</a></dt>418<dd><p>Stops the listening server which was created with a call to <a class="reference internal" href="#logging.config.listen" title="logging.config.listen"><code class="xref py py-func docutils literal notranslate"><span class="pre">listen()</span></code></a>.419This is typically called before calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code> on the return value from420<code class="xref py py-func docutils literal notranslate"><span class="pre">listen()</span></code>.</p>421</dd></dl>422 423</section>424<section id="security-considerations">425<h2>Security considerations<a class="headerlink" href="#security-considerations" title="Link to this heading">¶</a></h2>426<p>The logging configuration functionality tries to offer convenience, and in part this427is done by offering the ability to convert text in configuration files into Python428objects used in logging configuration - for example, as described in429<a class="reference internal" href="#logging-config-dict-userdef"><span class="std std-ref">User-defined objects</span></a>. However, these same mechanisms (importing430callables from user-defined modules and calling them with parameters from the431configuration) could be used to invoke any code you like, and for this reason you432should treat configuration files from untrusted sources with <em>extreme caution</em> and433satisfy yourself that nothing bad can happen if you load them, before actually loading434them.</p>435</section>436<section id="configuration-dictionary-schema">437<span id="logging-config-dictschema"></span><h2>Configuration dictionary schema<a class="headerlink" href="#configuration-dictionary-schema" title="Link to this heading">¶</a></h2>438<p>Describing a logging configuration requires listing the various439objects to create and the connections between them; for example, you440may create a handler named ‘console’ and then say that the logger441named ‘startup’ will send its messages to the ‘console’ handler.442These objects aren’t limited to those provided by the <a class="reference internal" href="logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a>443module because you might write your own formatter or handler class.444The parameters to these classes may also need to include external445objects such as <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>. The syntax for describing these446objects and connections is defined in <a class="reference internal" href="#logging-config-dict-connections"><span class="std std-ref">Object connections</span></a>447below.</p>448<section id="dictionary-schema-details">449<h3>Dictionary Schema Details<a class="headerlink" href="#dictionary-schema-details" title="Link to this heading">¶</a></h3>450<p>The dictionary passed to <a class="reference internal" href="#logging.config.dictConfig" title="logging.config.dictConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code></a> must contain the following451keys:</p>452<ul class="simple">453<li><p><em>version</em> - to be set to an integer value representing the schema454version. The only valid value at present is 1, but having this key455allows the schema to evolve while still preserving backwards456compatibility.</p></li>457</ul>458<p>All other keys are optional, but if present they will be interpreted459as described below. In all cases below where a ‘configuring dict’ is460mentioned, it will be checked for the special <code class="docutils literal notranslate"><span class="pre">'()'</span></code> key to see if a461custom instantiation is required. If so, the mechanism described in462<a class="reference internal" href="#logging-config-dict-userdef"><span class="std std-ref">User-defined objects</span></a> below is used to create an instance;463otherwise, the context is used to determine what to instantiate.</p>464<ul id="logging-config-dictschema-formatters">465<li><p><em>formatters</em> - the corresponding value will be a dict in which each466key is a formatter id and each value is a dict describing how to467configure the corresponding <a class="reference internal" href="logging.html#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> instance.</p>468<p>The configuring dict is searched for the following optional keys469which correspond to the arguments passed to create a470<a class="reference internal" href="logging.html#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> object:</p>471<ul class="simple">472<li><p><code class="docutils literal notranslate"><span class="pre">format</span></code></p></li>473<li><p><code class="docutils literal notranslate"><span class="pre">datefmt</span></code></p></li>474<li><p><code class="docutils literal notranslate"><span class="pre">style</span></code></p></li>475<li><p><code class="docutils literal notranslate"><span class="pre">validate</span></code> (since version >=3.8)</p></li>476<li><p><code class="docutils literal notranslate"><span class="pre">defaults</span></code> (since version >=3.12)</p></li>477</ul>478<p>An optional <code class="docutils literal notranslate"><span class="pre">class</span></code> key indicates the name of the formatter’s479class (as a dotted module and class name). The instantiation480arguments are as for <a class="reference internal" href="logging.html#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a>, thus this key is481most useful for instantiating a customised subclass of482<code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code>. For example, the alternative class483might present exception tracebacks in an expanded or condensed484format. If your formatter requires different or extra configuration485keys, you should use <a class="reference internal" href="#logging-config-dict-userdef"><span class="std std-ref">User-defined objects</span></a>.</p>486</li>487<li><p><em>filters</em> - the corresponding value will be a dict in which each key488is a filter id and each value is a dict describing how to configure489the corresponding Filter instance.</p>490<p>The configuring dict is searched for the key <code class="docutils literal notranslate"><span class="pre">name</span></code> (defaulting to the491empty string) and this is used to construct a <a class="reference internal" href="logging.html#logging.Filter" title="logging.Filter"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.Filter</span></code></a>492instance.</p>493</li>494<li><p><em>handlers</em> - the corresponding value will be a dict in which each495key is a handler id and each value is a dict describing how to496configure the corresponding Handler instance.</p>497<p>The configuring dict is searched for the following keys:</p>498<ul>499<li><p><code class="docutils literal notranslate"><span class="pre">class</span></code> (mandatory). This is the fully qualified name of the500handler class.</p></li>501<li><p><code class="docutils literal notranslate"><span class="pre">level</span></code> (optional). The level of the handler.</p></li>502<li><p><code class="docutils literal notranslate"><span class="pre">formatter</span></code> (optional). The id of the formatter for this503handler.</p></li>504<li><p><code class="docutils literal notranslate"><span class="pre">filters</span></code> (optional). A list of ids of the filters for this505handler.</p>506<div class="versionchanged">507<p><span class="versionmodified changed">Changed in version 3.11: </span><code class="docutils literal notranslate"><span class="pre">filters</span></code> can take filter instances in addition to ids.</p>508</div>509</li>510</ul>511<p>All <em>other</em> keys are passed through as keyword arguments to the512handler’s constructor. For example, given the snippet:</p>513<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">handlers</span><span class="p">:</span>514<span class="w"> </span><span class="nt">console</span><span class="p">:</span>515<span class="w"> </span><span class="nt">class </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">logging.StreamHandler</span>516<span class="w"> </span><span class="nt">formatter</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">brief</span>517<span class="w"> </span><span class="nt">level </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">INFO</span>518<span class="w"> </span><span class="nt">filters</span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="nv">allow_foo</span><span class="p p-Indicator">]</span>519<span class="w"> </span><span class="nt">stream </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">ext://sys.stdout</span>520<span class="w"> </span><span class="nt">file</span><span class="p">:</span>521<span class="w"> </span><span class="nt">class </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">logging.handlers.RotatingFileHandler</span>522<span class="w"> </span><span class="nt">formatter</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">precise</span>523<span class="w"> </span><span class="nt">filename</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">logconfig.log</span>524<span class="w"> </span><span class="nt">maxBytes</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">1024</span>525<span class="w"> </span><span class="nt">backupCount</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">3</span>526</pre></div>527</div>528<p>the handler with id <code class="docutils literal notranslate"><span class="pre">console</span></code> is instantiated as a529<a class="reference internal" href="logging.handlers.html#logging.StreamHandler" title="logging.StreamHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.StreamHandler</span></code></a>, using <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> as the underlying530stream. The handler with id <code class="docutils literal notranslate"><span class="pre">file</span></code> is instantiated as a531<a class="reference internal" href="logging.handlers.html#logging.handlers.RotatingFileHandler" title="logging.handlers.RotatingFileHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.handlers.RotatingFileHandler</span></code></a> with the keyword arguments532<code class="docutils literal notranslate"><span class="pre">filename='logconfig.log',</span> <span class="pre">maxBytes=1024,</span> <span class="pre">backupCount=3</span></code>.</p>533</li>534<li><p><em>loggers</em> - the corresponding value will be a dict in which each key535is a logger name and each value is a dict describing how to536configure the corresponding Logger instance.</p>537<p>The configuring dict is searched for the following keys:</p>538<ul>539<li><p><code class="docutils literal notranslate"><span class="pre">level</span></code> (optional). The level of the logger.</p></li>540<li><p><code class="docutils literal notranslate"><span class="pre">propagate</span></code> (optional). The propagation setting of the logger.</p></li>541<li><p><code class="docutils literal notranslate"><span class="pre">filters</span></code> (optional). A list of ids of the filters for this542logger.</p>543<div class="versionchanged">544<p><span class="versionmodified changed">Changed in version 3.11: </span><code class="docutils literal notranslate"><span class="pre">filters</span></code> can take filter instances in addition to ids.</p>545</div>546</li>547<li><p><code class="docutils literal notranslate"><span class="pre">handlers</span></code> (optional). A list of ids of the handlers for this548logger.</p></li>549</ul>550<p>The specified loggers will be configured according to the level,551propagation, filters and handlers specified.</p>552</li>553<li><p><em>root</em> - this will be the configuration for the root logger.554Processing of the configuration will be as for any logger, except555that the <code class="docutils literal notranslate"><span class="pre">propagate</span></code> setting will not be applicable.</p></li>556<li><p><em>incremental</em> - whether the configuration is to be interpreted as557incremental to the existing configuration. This value defaults to558<code class="docutils literal notranslate"><span class="pre">False</span></code>, which means that the specified configuration replaces the559existing configuration with the same semantics as used by the560existing <a class="reference internal" href="#logging.config.fileConfig" title="logging.config.fileConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileConfig()</span></code></a> API.</p>561<p>If the specified value is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the configuration is processed562as described in the section on <a class="reference internal" href="#logging-config-dict-incremental"><span class="std std-ref">Incremental Configuration</span></a>.</p>563</li>564<li><p><em>disable_existing_loggers</em> - whether any existing non-root loggers are565to be disabled. This setting mirrors the parameter of the same name in566<a class="reference internal" href="#logging.config.fileConfig" title="logging.config.fileConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileConfig()</span></code></a>. If absent, this parameter defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code>.567This value is ignored if <em>incremental</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p></li>568</ul>569</section>570<section id="incremental-configuration">571<span id="logging-config-dict-incremental"></span><h3>Incremental Configuration<a class="headerlink" href="#incremental-configuration" title="Link to this heading">¶</a></h3>572<p>It is difficult to provide complete flexibility for incremental573configuration. For example, because objects such as filters574and formatters are anonymous, once a configuration is set up, it is575not possible to refer to such anonymous objects when augmenting a576configuration.</p>577<p>Furthermore, there is not a compelling case for arbitrarily altering578the object graph of loggers, handlers, filters, formatters at579run-time, once a configuration is set up; the verbosity of loggers and580handlers can be controlled just by setting levels (and, in the case of581loggers, propagation flags). Changing the object graph arbitrarily in582a safe way is problematic in a multi-threaded environment; while not583impossible, the benefits are not worth the complexity it adds to the584implementation.</p>585<p>Thus, when the <code class="docutils literal notranslate"><span class="pre">incremental</span></code> key of a configuration dict is present586and is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the system will completely ignore any <code class="docutils literal notranslate"><span class="pre">formatters</span></code> and587<code class="docutils literal notranslate"><span class="pre">filters</span></code> entries, and process only the <code class="docutils literal notranslate"><span class="pre">level</span></code>588settings in the <code class="docutils literal notranslate"><span class="pre">handlers</span></code> entries, and the <code class="docutils literal notranslate"><span class="pre">level</span></code> and589<code class="docutils literal notranslate"><span class="pre">propagate</span></code> settings in the <code class="docutils literal notranslate"><span class="pre">loggers</span></code> and <code class="docutils literal notranslate"><span class="pre">root</span></code> entries.</p>590<p>Using a value in the configuration dict lets configurations to be sent591over the wire as pickled dicts to a socket listener. Thus, the logging592verbosity of a long-running application can be altered over time with593no need to stop and restart the application.</p>594</section>595<section id="object-connections">596<span id="logging-config-dict-connections"></span><h3>Object connections<a class="headerlink" href="#object-connections" title="Link to this heading">¶</a></h3>597<p>The schema describes a set of logging objects - loggers,598handlers, formatters, filters - which are connected to each other in599an object graph. Thus, the schema needs to represent connections600between the objects. For example, say that, once configured, a601particular logger has attached to it a particular handler. For the602purposes of this discussion, we can say that the logger represents the603source, and the handler the destination, of a connection between the604two. Of course in the configured objects this is represented by the605logger holding a reference to the handler. In the configuration dict,606this is done by giving each destination object an id which identifies607it unambiguously, and then using the id in the source object’s608configuration to indicate that a connection exists between the source609and the destination object with that id.</p>610<p>So, for example, consider the following YAML snippet:</p>611<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">formatters</span><span class="p">:</span>612<span class="w"> </span><span class="nt">brief</span><span class="p">:</span>613<span class="w"> </span><span class="c1"># configuration for formatter with id 'brief' goes here</span>614<span class="w"> </span><span class="nt">precise</span><span class="p">:</span>615<span class="w"> </span><span class="c1"># configuration for formatter with id 'precise' goes here</span>616<span class="nt">handlers</span><span class="p">:</span>617<span class="w"> </span><span class="nt">h1</span><span class="p">:</span><span class="w"> </span><span class="c1">#This is an id</span>618<span class="w"> </span><span class="c1"># configuration of handler with id 'h1' goes here</span>619<span class="w"> </span><span class="nt">formatter</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">brief</span>620<span class="w"> </span><span class="nt">h2</span><span class="p">:</span><span class="w"> </span><span class="c1">#This is another id</span>621<span class="w"> </span><span class="c1"># configuration of handler with id 'h2' goes here</span>622<span class="w"> </span><span class="nt">formatter</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">precise</span>623<span class="nt">loggers</span><span class="p">:</span>624<span class="w"> </span><span class="nt">foo.bar.baz</span><span class="p">:</span>625<span class="w"> </span><span class="c1"># other configuration for logger 'foo.bar.baz'</span>626<span class="w"> </span><span class="nt">handlers</span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="nv">h1</span><span class="p p-Indicator">,</span><span class="w"> </span><span class="nv">h2</span><span class="p p-Indicator">]</span>627</pre></div>628</div>629<p>(Note: YAML used here because it’s a little more readable than the630equivalent Python source form for the dictionary.)</p>631<p>The ids for loggers are the logger names which would be used632programmatically to obtain a reference to those loggers, e.g.633<code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>. The ids for Formatters and Filters can be any string634value (such as <code class="docutils literal notranslate"><span class="pre">brief</span></code>, <code class="docutils literal notranslate"><span class="pre">precise</span></code> above) and they are transient,635in that they are only meaningful for processing the configuration636dictionary and used to determine connections between objects, and are637not persisted anywhere when the configuration call is complete.</p>638<p>The above snippet indicates that logger named <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code> should639have two handlers attached to it, which are described by the handler640ids <code class="docutils literal notranslate"><span class="pre">h1</span></code> and <code class="docutils literal notranslate"><span class="pre">h2</span></code>. The formatter for <code class="docutils literal notranslate"><span class="pre">h1</span></code> is that described by id641<code class="docutils literal notranslate"><span class="pre">brief</span></code>, and the formatter for <code class="docutils literal notranslate"><span class="pre">h2</span></code> is that described by id642<code class="docutils literal notranslate"><span class="pre">precise</span></code>.</p>643</section>644<section id="user-defined-objects">645<span id="logging-config-dict-userdef"></span><h3>User-defined objects<a class="headerlink" href="#user-defined-objects" title="Link to this heading">¶</a></h3>646<p>The schema supports user-defined objects for handlers, filters and647formatters. (Loggers do not need to have different types for648different instances, so there is no support in this configuration649schema for user-defined logger classes.)</p>650<p>Objects to be configured are described by dictionaries651which detail their configuration. In some places, the logging system652will be able to infer from the context how an object is to be653instantiated, but when a user-defined object is to be instantiated,654the system will not know how to do this. In order to provide complete655flexibility for user-defined object instantiation, the user needs656to provide a ‘factory’ - a callable which is called with a657configuration dictionary and which returns the instantiated object.658This is signalled by an absolute import path to the factory being659made available under the special key <code class="docutils literal notranslate"><span class="pre">'()'</span></code>. Here’s a concrete660example:</p>661<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">formatters</span><span class="p">:</span>662<span class="w"> </span><span class="nt">brief</span><span class="p">:</span>663<span class="w"> </span><span class="nt">format</span><span class="p">:</span><span class="w"> </span><span class="s">'%(message)s'</span>664<span class="w"> </span><span class="nt">default</span><span class="p">:</span>665<span class="w"> </span><span class="nt">format</span><span class="p">:</span><span class="w"> </span><span class="s">'%(asctime)s</span><span class="nv"> </span><span class="s">%(levelname)-8s</span><span class="nv"> </span><span class="s">%(name)-15s</span><span class="nv"> </span><span class="s">%(message)s'</span>666<span class="w"> </span><span class="nt">datefmt</span><span class="p">:</span><span class="w"> </span><span class="s">'%Y-%m-%d</span><span class="nv"> </span><span class="s">%H:%M:%S'</span>667<span class="w"> </span><span class="nt">custom</span><span class="p">:</span>668<span class="w"> </span><span class="nt">()</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">my.package.customFormatterFactory</span>669<span class="w"> </span><span class="nt">bar</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">baz</span>670<span class="w"> </span><span class="nt">spam</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">99.9</span>671<span class="w"> </span><span class="nt">answer</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">42</span>672</pre></div>673</div>674<p>The above YAML snippet defines three formatters. The first, with id675<code class="docutils literal notranslate"><span class="pre">brief</span></code>, is a standard <a class="reference internal" href="logging.html#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.Formatter</span></code></a> instance with the676specified format string. The second, with id <code class="docutils literal notranslate"><span class="pre">default</span></code>, has a677longer format and also defines the time format explicitly, and will678result in a <code class="xref py py-class docutils literal notranslate"><span class="pre">logging.Formatter</span></code> initialized with those two format679strings. Shown in Python source form, the <code class="docutils literal notranslate"><span class="pre">brief</span></code> and <code class="docutils literal notranslate"><span class="pre">default</span></code>680formatters have configuration sub-dictionaries:</p>681<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>682 <span class="s1">'format'</span> <span class="p">:</span> <span class="s1">'</span><span class="si">%(message)s</span><span class="s1">'</span>683<span class="p">}</span>684</pre></div>685</div>686<p>and:</p>687<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>688 <span class="s1">'format'</span> <span class="p">:</span> <span class="s1">'</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(levelname)-8s</span><span class="s1"> </span><span class="si">%(name)-15s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">,</span>689 <span class="s1">'datefmt'</span> <span class="p">:</span> <span class="s1">'%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S'</span>690<span class="p">}</span>691</pre></div>692</div>693<p>respectively, and as these dictionaries do not contain the special key694<code class="docutils literal notranslate"><span class="pre">'()'</span></code>, the instantiation is inferred from the context: as a result,695standard <a class="reference internal" href="logging.html#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.Formatter</span></code></a> instances are created. The696configuration sub-dictionary for the third formatter, with id697<code class="docutils literal notranslate"><span class="pre">custom</span></code>, is:</p>698<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>699 <span class="s1">'()'</span> <span class="p">:</span> <span class="s1">'my.package.customFormatterFactory'</span><span class="p">,</span>700 <span class="s1">'bar'</span> <span class="p">:</span> <span class="s1">'baz'</span><span class="p">,</span>701 <span class="s1">'spam'</span> <span class="p">:</span> <span class="mf">99.9</span><span class="p">,</span>702 <span class="s1">'answer'</span> <span class="p">:</span> <span class="mi">42</span>703<span class="p">}</span>704</pre></div>705</div>706<p>and this contains the special key <code class="docutils literal notranslate"><span class="pre">'()'</span></code>, which means that707user-defined instantiation is wanted. In this case, the specified708factory callable will be used. If it is an actual callable it will be709used directly - otherwise, if you specify a string (as in the example)710the actual callable will be located using normal import mechanisms.711The callable will be called with the <strong>remaining</strong> items in the712configuration sub-dictionary as keyword arguments. In the above713example, the formatter with id <code class="docutils literal notranslate"><span class="pre">custom</span></code> will be assumed to be714returned by the call:</p>715<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">my</span><span class="o">.</span><span class="n">package</span><span class="o">.</span><span class="n">customFormatterFactory</span><span class="p">(</span><span class="n">bar</span><span class="o">=</span><span class="s1">'baz'</span><span class="p">,</span> <span class="n">spam</span><span class="o">=</span><span class="mf">99.9</span><span class="p">,</span> <span class="n">answer</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>716</pre></div>717</div>718<div class="admonition warning">719<p class="admonition-title">Warning</p>720<p>The values for keys such as <code class="docutils literal notranslate"><span class="pre">bar</span></code>, <code class="docutils literal notranslate"><span class="pre">spam</span></code> and <code class="docutils literal notranslate"><span class="pre">answer</span></code> in721the above example should not be configuration dictionaries or references such722as <code class="docutils literal notranslate"><span class="pre">cfg://foo</span></code> or <code class="docutils literal notranslate"><span class="pre">ext://bar</span></code>, because they will not be processed by the723configuration machinery, but passed to the callable as-is.</p>724</div>725<p>The key <code class="docutils literal notranslate"><span class="pre">'()'</span></code> has been used as the special key because it is not a726valid keyword parameter name, and so will not clash with the names of727the keyword arguments used in the call. The <code class="docutils literal notranslate"><span class="pre">'()'</span></code> also serves as a728mnemonic that the corresponding value is a callable.</p>729<div class="versionchanged">730<p><span class="versionmodified changed">Changed in version 3.11: </span>The <code class="docutils literal notranslate"><span class="pre">filters</span></code> member of <code class="docutils literal notranslate"><span class="pre">handlers</span></code> and <code class="docutils literal notranslate"><span class="pre">loggers</span></code> can take731filter instances in addition to ids.</p>732</div>733<p>You can also specify a special key <code class="docutils literal notranslate"><span class="pre">'.'</span></code> whose value is a734mapping of attribute names to values. If found, the specified attributes will735be set on the user-defined object before it is returned. Thus, with the736following configuration:</p>737<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>738 <span class="s1">'()'</span> <span class="p">:</span> <span class="s1">'my.package.customFormatterFactory'</span><span class="p">,</span>739 <span class="s1">'bar'</span> <span class="p">:</span> <span class="s1">'baz'</span><span class="p">,</span>740 <span class="s1">'spam'</span> <span class="p">:</span> <span class="mf">99.9</span><span class="p">,</span>741 <span class="s1">'answer'</span> <span class="p">:</span> <span class="mi">42</span><span class="p">,</span>742 <span class="s1">'.'</span> <span class="p">{</span>743 <span class="s1">'foo'</span><span class="p">:</span> <span class="s1">'bar'</span><span class="p">,</span>744 <span class="s1">'baz'</span><span class="p">:</span> <span class="s1">'bozz'</span>745 <span class="p">}</span>746<span class="p">}</span>747</pre></div>748</div>749<p>the returned formatter will have attribute <code class="docutils literal notranslate"><span class="pre">foo</span></code> set to <code class="docutils literal notranslate"><span class="pre">'bar'</span></code> and750attribute <code class="docutils literal notranslate"><span class="pre">baz</span></code> set to <code class="docutils literal notranslate"><span class="pre">'bozz'</span></code>.</p>751<div class="admonition warning">752<p class="admonition-title">Warning</p>753<p>The values for attributes such as <code class="docutils literal notranslate"><span class="pre">foo</span></code> and <code class="docutils literal notranslate"><span class="pre">baz</span></code> in754the above example should not be configuration dictionaries or references such755as <code class="docutils literal notranslate"><span class="pre">cfg://foo</span></code> or <code class="docutils literal notranslate"><span class="pre">ext://bar</span></code>, because they will not be processed by the756configuration machinery, but set as attribute values as-is.</p>757</div>758</section>759<section id="handler-configuration-order">760<span id="handler-config-dict-order"></span><h3>Handler configuration order<a class="headerlink" href="#handler-configuration-order" title="Link to this heading">¶</a></h3>761<p>Handlers are configured in alphabetical order of their keys, and a configured762handler replaces the configuration dictionary in (a working copy of) the763<code class="docutils literal notranslate"><span class="pre">handlers</span></code> dictionary in the schema. If you use a construct such as764<code class="docutils literal notranslate"><span class="pre">cfg://handlers.foo</span></code>, then initially <code class="docutils literal notranslate"><span class="pre">handlers['foo']</span></code> points to the765configuration dictionary for the handler named <code class="docutils literal notranslate"><span class="pre">foo</span></code>, and later (once that766handler has been configured) it points to the configured handler instance.767Thus, <code class="docutils literal notranslate"><span class="pre">cfg://handlers.foo</span></code> could resolve to either a dictionary or a handler768instance. In general, it is wise to name handlers in a way such that dependent769handlers are configured <em>after</em> any handlers they depend on; that allows770something like <code class="docutils literal notranslate"><span class="pre">cfg://handlers.foo</span></code> to be used in configuring a handler that771depends on handler <code class="docutils literal notranslate"><span class="pre">foo</span></code>. If that dependent handler were named <code class="docutils literal notranslate"><span class="pre">bar</span></code>,772problems would result, because the configuration of <code class="docutils literal notranslate"><span class="pre">bar</span></code> would be attempted773before that of <code class="docutils literal notranslate"><span class="pre">foo</span></code>, and <code class="docutils literal notranslate"><span class="pre">foo</span></code> would not yet have been configured.774However, if the dependent handler were named <code class="docutils literal notranslate"><span class="pre">foobar</span></code>, it would be configured775after <code class="docutils literal notranslate"><span class="pre">foo</span></code>, with the result that <code class="docutils literal notranslate"><span class="pre">cfg://handlers.foo</span></code> would resolve to776configured handler <code class="docutils literal notranslate"><span class="pre">foo</span></code>, and not its configuration dictionary.</p>777</section>778<section id="access-to-external-objects">779<span id="logging-config-dict-externalobj"></span><h3>Access to external objects<a class="headerlink" href="#access-to-external-objects" title="Link to this heading">¶</a></h3>780<p>There are times where a configuration needs to refer to objects781external to the configuration, for example <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>. If the782configuration dict is constructed using Python code, this is783straightforward, but a problem arises when the configuration is784provided via a text file (e.g. JSON, YAML). In a text file, there is785no standard way to distinguish <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> from the literal string786<code class="docutils literal notranslate"><span class="pre">'sys.stderr'</span></code>. To facilitate this distinction, the configuration787system looks for certain special prefixes in string values and788treat them specially. For example, if the literal string789<code class="docutils literal notranslate"><span class="pre">'ext://sys.stderr'</span></code> is provided as a value in the configuration,790then the <code class="docutils literal notranslate"><span class="pre">ext://</span></code> will be stripped off and the remainder of the791value processed using normal import mechanisms.</p>792<p>The handling of such prefixes is done in a way analogous to protocol793handling: there is a generic mechanism to look for prefixes which794match the regular expression <code class="docutils literal notranslate"><span class="pre">^(?P<prefix>[a-z]+)://(?P<suffix>.*)$</span></code>795whereby, if the <code class="docutils literal notranslate"><span class="pre">prefix</span></code> is recognised, the <code class="docutils literal notranslate"><span class="pre">suffix</span></code> is processed796in a prefix-dependent manner and the result of the processing replaces797the string value. If the prefix is not recognised, then the string798value will be left as-is.</p>799</section>800<section id="access-to-internal-objects">801<span id="logging-config-dict-internalobj"></span><h3>Access to internal objects<a class="headerlink" href="#access-to-internal-objects" title="Link to this heading">¶</a></h3>802<p>As well as external objects, there is sometimes also a need to refer803to objects in the configuration. This will be done implicitly by the804configuration system for things that it knows about. For example, the805string value <code class="docutils literal notranslate"><span class="pre">'DEBUG'</span></code> for a <code class="docutils literal notranslate"><span class="pre">level</span></code> in a logger or handler will806automatically be converted to the value <code class="docutils literal notranslate"><span class="pre">logging.DEBUG</span></code>, and the807<code class="docutils literal notranslate"><span class="pre">handlers</span></code>, <code class="docutils literal notranslate"><span class="pre">filters</span></code> and <code class="docutils literal notranslate"><span class="pre">formatter</span></code> entries will take an808object id and resolve to the appropriate destination object.</p>809<p>However, a more generic mechanism is needed for user-defined810objects which are not known to the <a class="reference internal" href="logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> module. For811example, consider <a class="reference internal" href="logging.handlers.html#logging.handlers.MemoryHandler" title="logging.handlers.MemoryHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.handlers.MemoryHandler</span></code></a>, which takes812a <code class="docutils literal notranslate"><span class="pre">target</span></code> argument which is another handler to delegate to. Since813the system already knows about this class, then in the configuration,814the given <code class="docutils literal notranslate"><span class="pre">target</span></code> just needs to be the object id of the relevant815target handler, and the system will resolve to the handler from the816id. If, however, a user defines a <code class="docutils literal notranslate"><span class="pre">my.package.MyHandler</span></code> which has817an <code class="docutils literal notranslate"><span class="pre">alternate</span></code> handler, the configuration system would not know that818the <code class="docutils literal notranslate"><span class="pre">alternate</span></code> referred to a handler. To cater for this, a generic819resolution system allows the user to specify:</p>820<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">handlers</span><span class="p">:</span>821<span class="w"> </span><span class="nt">file</span><span class="p">:</span>822<span class="w"> </span><span class="c1"># configuration of file handler goes here</span>823 824<span class="w"> </span><span class="nt">custom</span><span class="p">:</span>825<span class="w"> </span><span class="nt">()</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">my.package.MyHandler</span>826<span class="w"> </span><span class="nt">alternate</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">cfg://handlers.file</span>827</pre></div>828</div>829<p>The literal string <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.file'</span></code> will be resolved in an830analogous way to strings with the <code class="docutils literal notranslate"><span class="pre">ext://</span></code> prefix, but looking831in the configuration itself rather than the import namespace. The832mechanism allows access by dot or by index, in a similar way to833that provided by <code class="docutils literal notranslate"><span class="pre">str.format</span></code>. Thus, given the following snippet:</p>834<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">handlers</span><span class="p">:</span>835<span class="w"> </span><span class="nt">email</span><span class="p">:</span>836<span class="w"> </span><span class="nt">class</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">logging.handlers.SMTPHandler</span>837<span class="w"> </span><span class="nt">mailhost</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">localhost</span>838<span class="w"> </span><span class="nt">fromaddr</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">my_app@domain.tld</span>839<span class="w"> </span><span class="nt">toaddrs</span><span class="p">:</span>840<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">support_team@domain.tld</span>841<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">dev_team@domain.tld</span>842<span class="w"> </span><span class="nt">subject</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">Houston, we have a problem.</span>843</pre></div>844</div>845<p>in the configuration, the string <code class="docutils literal notranslate"><span class="pre">'cfg://handlers'</span></code> would resolve to846the dict with key <code class="docutils literal notranslate"><span class="pre">handlers</span></code>, the string <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email</span></code>847would resolve to the dict with key <code class="docutils literal notranslate"><span class="pre">email</span></code> in the <code class="docutils literal notranslate"><span class="pre">handlers</span></code> dict,848and so on. The string <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email.toaddrs[1]</span></code> would849resolve to <code class="docutils literal notranslate"><span class="pre">'dev_team@domain.tld'</span></code> and the string850<code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email.toaddrs[0]'</span></code> would resolve to the value851<code class="docutils literal notranslate"><span class="pre">'support_team@domain.tld'</span></code>. The <code class="docutils literal notranslate"><span class="pre">subject</span></code> value could be accessed852using either <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email.subject'</span></code> or, equivalently,853<code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email[subject]'</span></code>. The latter form only needs to be854used if the key contains spaces or non-alphanumeric characters. Please note855that the characters <code class="docutils literal notranslate"><span class="pre">[</span></code> and <code class="docutils literal notranslate"><span class="pre">]</span></code> are not allowed in the keys. If an856index value consists only of decimal digits, access will be attempted857using the corresponding integer value, falling back to the string858value if needed.</p>859<p>Given a string <code class="docutils literal notranslate"><span class="pre">cfg://handlers.myhandler.mykey.123</span></code>, this will860resolve to <code class="docutils literal notranslate"><span class="pre">config_dict['handlers']['myhandler']['mykey']['123']</span></code>.861If the string is specified as <code class="docutils literal notranslate"><span class="pre">cfg://handlers.myhandler.mykey[123]</span></code>,862the system will attempt to retrieve the value from863<code class="docutils literal notranslate"><span class="pre">config_dict['handlers']['myhandler']['mykey'][123]</span></code>, and fall back864to <code class="docutils literal notranslate"><span class="pre">config_dict['handlers']['myhandler']['mykey']['123']</span></code> if that865fails.</p>866</section>867<section id="import-resolution-and-custom-importers">868<span id="logging-import-resolution"></span><h3>Import resolution and custom importers<a class="headerlink" href="#import-resolution-and-custom-importers" title="Link to this heading">¶</a></h3>869<p>Import resolution, by default, uses the builtin <a class="reference internal" href="functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function870to do its importing. You may want to replace this with your own importing871mechanism: if so, you can replace the <code class="xref py py-attr docutils literal notranslate"><span class="pre">importer</span></code> attribute of the872<code class="xref py py-class docutils literal notranslate"><span class="pre">DictConfigurator</span></code> or its superclass, the873<code class="xref py py-class docutils literal notranslate"><span class="pre">BaseConfigurator</span></code> class. However, you need to be874careful because of the way functions are accessed from classes via875descriptors. If you are using a Python callable to do your imports, and you876want to define it at class level rather than instance level, you need to wrap877it with <a class="reference internal" href="functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a>. For example:</p>878<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">importlib</span><span class="w"> </span><span class="kn">import</span> <span class="n">import_module</span>879<span class="kn">from</span><span class="w"> </span><span class="nn">logging.config</span><span class="w"> </span><span class="kn">import</span> <span class="n">BaseConfigurator</span>880 881<span class="n">BaseConfigurator</span><span class="o">.</span><span class="n">importer</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">import_module</span><span class="p">)</span>882</pre></div>883</div>884<p>You don’t need to wrap with <a class="reference internal" href="functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> if you’re setting the import885callable on a configurator <em>instance</em>.</p>886</section>887<section id="configuring-queuehandler-and-queuelistener">888<span id="configure-queue"></span><h3>Configuring QueueHandler and QueueListener<a class="headerlink" href="#configuring-queuehandler-and-queuelistener" title="Link to this heading">¶</a></h3>889<p>If you want to configure a <a class="reference internal" href="logging.handlers.html#logging.handlers.QueueHandler" title="logging.handlers.QueueHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueueHandler</span></code></a>, noting that this890is normally used in conjunction with a <a class="reference internal" href="logging.handlers.html#logging.handlers.QueueListener" title="logging.handlers.QueueListener"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueueListener</span></code></a>, you891can configure both together. After the configuration, the <code class="docutils literal notranslate"><span class="pre">QueueListener</span></code> instance892will be available as the <a class="reference internal" href="logging.handlers.html#logging.handlers.QueueHandler.listener" title="logging.handlers.QueueHandler.listener"><code class="xref py py-attr docutils literal notranslate"><span class="pre">listener</span></code></a> attribute of893the created handler, and that in turn will be available to you using894<a class="reference internal" href="logging.html#logging.getHandlerByName" title="logging.getHandlerByName"><code class="xref py py-func docutils literal notranslate"><span class="pre">getHandlerByName()</span></code></a> and passing the name you have used for the895<code class="docutils literal notranslate"><span class="pre">QueueHandler</span></code> in your configuration. The dictionary schema for configuring the pair896is shown in the example YAML snippet below.</p>897<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">handlers</span><span class="p">:</span>898<span class="w"> </span><span class="nt">qhand</span><span class="p">:</span>899<span class="w"> </span><span class="nt">class</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">logging.handlers.QueueHandler</span>900<span class="w"> </span><span class="nt">queue</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">my.module.queue_factory</span>901<span class="w"> </span><span class="nt">listener</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">my.package.CustomListener</span>902<span class="w"> </span><span class="nt">handlers</span><span class="p">:</span>903<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">hand_name_1</span>904<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">hand_name_2</span>905<span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">...</span>906</pre></div>907</div>908<p>The <code class="docutils literal notranslate"><span class="pre">queue</span></code> and <code class="docutils literal notranslate"><span class="pre">listener</span></code> keys are optional.</p>909<p>If the <code class="docutils literal notranslate"><span class="pre">queue</span></code> key is present, the corresponding value can be one of the following:</p>910<ul>911<li><p>An object implementing the <a class="reference internal" href="queue.html#queue.Queue.put_nowait" title="queue.Queue.put_nowait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.put_nowait</span></code></a>912and <a class="reference internal" href="queue.html#queue.Queue.get" title="queue.Queue.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.get</span></code></a> public API. For instance, this may be913an actual instance of <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a> or a subclass thereof, or a proxy914obtained by <a class="reference internal" href="multiprocessing.html#multiprocessing.managers.SyncManager.Queue" title="multiprocessing.managers.SyncManager.Queue"><code class="xref py py-meth docutils literal notranslate"><span class="pre">multiprocessing.managers.SyncManager.Queue()</span></code></a>.</p>915<p>This is of course only possible if you are constructing or modifying916the configuration dictionary in code.</p>917</li>918<li><p>A string that resolves to a callable which, when called with no arguments, returns919the queue instance to use. That callable could be a <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a> subclass920or a function which returns a suitable queue instance,921such as <code class="docutils literal notranslate"><span class="pre">my.module.queue_factory()</span></code>.</p></li>922<li><p>A dict with a <code class="docutils literal notranslate"><span class="pre">'()'</span></code> key which is constructed in the usual way as discussed in923<a class="reference internal" href="#logging-config-dict-userdef"><span class="std std-ref">User-defined objects</span></a>. The result of this construction should be a924<a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a> instance.</p></li>925</ul>926<p>If the <code class="docutils literal notranslate"><span class="pre">queue</span></code> key is absent, a standard unbounded <a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.Queue</span></code></a> instance is927created and used.</p>928<p>If the <code class="docutils literal notranslate"><span class="pre">listener</span></code> key is present, the corresponding value can be one of the following:</p>929<ul class="simple">930<li><p>A subclass of <a class="reference internal" href="logging.handlers.html#logging.handlers.QueueListener" title="logging.handlers.QueueListener"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.handlers.QueueListener</span></code></a>. This is of course only931possible if you are constructing or modifying the configuration dictionary in932code.</p></li>933<li><p>A string which resolves to a class which is a subclass of <code class="docutils literal notranslate"><span class="pre">QueueListener</span></code>, such as934<code class="docutils literal notranslate"><span class="pre">'my.package.CustomListener'</span></code>.</p></li>935<li><p>A dict with a <code class="docutils literal notranslate"><span class="pre">'()'</span></code> key which is constructed in the usual way as discussed in936<a class="reference internal" href="#logging-config-dict-userdef"><span class="std std-ref">User-defined objects</span></a>. The result of this construction should be a937callable with the same signature as the <code class="docutils literal notranslate"><span class="pre">QueueListener</span></code> initializer.</p></li>938</ul>939<p>If the <code class="docutils literal notranslate"><span class="pre">listener</span></code> key is absent, <a class="reference internal" href="logging.handlers.html#logging.handlers.QueueListener" title="logging.handlers.QueueListener"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.handlers.QueueListener</span></code></a> is used.</p>940<p>The values under the <code class="docutils literal notranslate"><span class="pre">handlers</span></code> key are the names of other handlers in the941configuration (not shown in the above snippet) which will be passed to the queue942listener.</p>943<p>Any custom queue handler and listener classes will need to be defined with the same944initialization signatures as <a class="reference internal" href="logging.handlers.html#logging.handlers.QueueHandler" title="logging.handlers.QueueHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueueHandler</span></code></a> and945<a class="reference internal" href="logging.handlers.html#logging.handlers.QueueListener" title="logging.handlers.QueueListener"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueueListener</span></code></a>.</p>946<div class="versionadded">947<p><span class="versionmodified added">Added in version 3.12.</span></p>948</div>949</section>950</section>951<section id="configuration-file-format">952<span id="logging-config-fileformat"></span><h2>Configuration file format<a class="headerlink" href="#configuration-file-format" title="Link to this heading">¶</a></h2>953<p>The configuration file format understood by <a class="reference internal" href="#logging.config.fileConfig" title="logging.config.fileConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileConfig()</span></code></a> is based on954<a class="reference internal" href="configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">configparser</span></code></a> functionality. The file must contain sections called955<code class="docutils literal notranslate"><span class="pre">[loggers]</span></code>, <code class="docutils literal notranslate"><span class="pre">[handlers]</span></code> and <code class="docutils literal notranslate"><span class="pre">[formatters]</span></code> which identify by name the956entities of each type which are defined in the file. For each such entity, there957is a separate section which identifies how that entity is configured. Thus, for958a logger named <code class="docutils literal notranslate"><span class="pre">log01</span></code> in the <code class="docutils literal notranslate"><span class="pre">[loggers]</span></code> section, the relevant959configuration details are held in a section <code class="docutils literal notranslate"><span class="pre">[logger_log01]</span></code>. Similarly, a960handler called <code class="docutils literal notranslate"><span class="pre">hand01</span></code> in the <code class="docutils literal notranslate"><span class="pre">[handlers]</span></code> section will have its961configuration held in a section called <code class="docutils literal notranslate"><span class="pre">[handler_hand01]</span></code>, while a formatter962called <code class="docutils literal notranslate"><span class="pre">form01</span></code> in the <code class="docutils literal notranslate"><span class="pre">[formatters]</span></code> section will have its configuration963specified in a section called <code class="docutils literal notranslate"><span class="pre">[formatter_form01]</span></code>. The root logger964configuration must be specified in a section called <code class="docutils literal notranslate"><span class="pre">[logger_root]</span></code>.</p>965<div class="admonition note">966<p class="admonition-title">Note</p>967<p>The <a class="reference internal" href="#logging.config.fileConfig" title="logging.config.fileConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileConfig()</span></code></a> API is older than the <a class="reference internal" href="#logging.config.dictConfig" title="logging.config.dictConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code></a> API and does968not provide functionality to cover certain aspects of logging. For example,969you cannot configure <a class="reference internal" href="logging.html#logging.Filter" title="logging.Filter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Filter</span></code></a> objects, which provide for970filtering of messages beyond simple integer levels, using <code class="xref py py-func docutils literal notranslate"><span class="pre">fileConfig()</span></code>.971If you need to have instances of <code class="xref py py-class docutils literal notranslate"><span class="pre">Filter</span></code> in your logging972configuration, you will need to use <code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code>. Note that future973enhancements to configuration functionality will be added to974<code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code>, so it’s worth considering transitioning to this newer975API when it’s convenient to do so.</p>976</div>977<p>Examples of these sections in the file are given below.</p>978<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[loggers]</span>979<span class="na">keys</span><span class="o">=</span><span class="s">root,log02,log03,log04,log05,log06,log07</span>980 981<span class="k">[handlers]</span>982<span class="na">keys</span><span class="o">=</span><span class="s">hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09</span>983 984<span class="k">[formatters]</span>985<span class="na">keys</span><span class="o">=</span><span class="s">form01,form02,form03,form04,form05,form06,form07,form08,form09</span>986</pre></div>987</div>988<p>The root logger must specify a level and a list of handlers. An example of a989root logger section is given below.</p>990<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[logger_root]</span>991<span class="na">level</span><span class="o">=</span><span class="s">NOTSET</span>992<span class="na">handlers</span><span class="o">=</span><span class="s">hand01</span>993</pre></div>994</div>995<p>The <code class="docutils literal notranslate"><span class="pre">level</span></code> entry can be one of <code class="docutils literal notranslate"><span class="pre">DEBUG,</span> <span class="pre">INFO,</span> <span class="pre">WARNING,</span> <span class="pre">ERROR,</span> <span class="pre">CRITICAL</span></code> or996<code class="docutils literal notranslate"><span class="pre">NOTSET</span></code>. For the root logger only, <code class="docutils literal notranslate"><span class="pre">NOTSET</span></code> means that all messages will be997logged. Level values are <a class="reference internal" href="functions.html#func-eval"><span class="std std-ref">evaluated</span></a> in the context of the <code class="docutils literal notranslate"><span class="pre">logging</span></code>998package’s namespace.</p>999<p>The <code class="docutils literal notranslate"><span class="pre">handlers</span></code> entry is a comma-separated list of handler names, which must1000appear in the <code class="docutils literal notranslate"><span class="pre">[handlers]</span></code> section. These names must appear in the1001<code class="docutils literal notranslate"><span class="pre">[handlers]</span></code> section and have corresponding sections in the configuration1002file.</p>1003<p>For loggers other than the root logger, some additional information is required.1004This is illustrated by the following example.</p>1005<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[logger_parser]</span>1006<span class="na">level</span><span class="o">=</span><span class="s">DEBUG</span>1007<span class="na">handlers</span><span class="o">=</span><span class="s">hand01</span>1008<span class="na">propagate</span><span class="o">=</span><span class="s">1</span>1009<span class="na">qualname</span><span class="o">=</span><span class="s">compiler.parser</span>1010</pre></div>1011</div>1012<p>The <code class="docutils literal notranslate"><span class="pre">level</span></code> and <code class="docutils literal notranslate"><span class="pre">handlers</span></code> entries are interpreted as for the root logger,1013except that if a non-root logger’s level is specified as <code class="docutils literal notranslate"><span class="pre">NOTSET</span></code>, the system1014consults loggers higher up the hierarchy to determine the effective level of the1015logger. The <code class="docutils literal notranslate"><span class="pre">propagate</span></code> entry is set to 1 to indicate that messages must1016propagate to handlers higher up the logger hierarchy from this logger, or 0 to1017indicate that messages are <strong>not</strong> propagated to handlers up the hierarchy. The1018<code class="docutils literal notranslate"><span class="pre">qualname</span></code> entry is the hierarchical channel name of the logger, that is to1019say the name used by the application to get the logger.</p>1020<p>Sections which specify handler configuration are exemplified by the following.</p>1021<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[handler_hand01]</span>1022<span class="na">class</span><span class="o">=</span><span class="s">StreamHandler</span>1023<span class="na">level</span><span class="o">=</span><span class="s">NOTSET</span>1024<span class="na">formatter</span><span class="o">=</span><span class="s">form01</span>1025<span class="na">args</span><span class="o">=</span><span class="s">(sys.stdout,)</span>1026</pre></div>1027</div>1028<p>The <code class="docutils literal notranslate"><span class="pre">class</span></code> entry indicates the handler’s class (as determined by <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>1029in the <code class="docutils literal notranslate"><span class="pre">logging</span></code> package’s namespace). The <code class="docutils literal notranslate"><span class="pre">level</span></code> is interpreted as for1030loggers, and <code class="docutils literal notranslate"><span class="pre">NOTSET</span></code> is taken to mean ‘log everything’.</p>1031<p>The <code class="docutils literal notranslate"><span class="pre">formatter</span></code> entry indicates the key name of the formatter for this1032handler. If blank, a default formatter (<code class="docutils literal notranslate"><span class="pre">logging._defaultFormatter</span></code>) is used.1033If a name is specified, it must appear in the <code class="docutils literal notranslate"><span class="pre">[formatters]</span></code> section and have1034a corresponding section in the configuration file.</p>1035<p>The <code class="docutils literal notranslate"><span class="pre">args</span></code> entry, when <a class="reference internal" href="functions.html#func-eval"><span class="std std-ref">evaluated</span></a> in the context of the <code class="docutils literal notranslate"><span class="pre">logging</span></code>1036package’s namespace, is the list of arguments to the constructor for the handler1037class. Refer to the constructors for the relevant handlers, or to the examples1038below, to see how typical entries are constructed. If not provided, it defaults1039to <code class="docutils literal notranslate"><span class="pre">()</span></code>.</p>1040<p>The optional <code class="docutils literal notranslate"><span class="pre">kwargs</span></code> entry, when <a class="reference internal" href="functions.html#func-eval"><span class="std std-ref">evaluated</span></a> in the context of the1041<code class="docutils literal notranslate"><span class="pre">logging</span></code> package’s namespace, is the keyword argument dict to the constructor1042for the handler class. If not provided, it defaults to <code class="docutils literal notranslate"><span class="pre">{}</span></code>.</p>1043<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[handler_hand02]</span>1044<span class="na">class</span><span class="o">=</span><span class="s">FileHandler</span>1045<span class="na">level</span><span class="o">=</span><span class="s">DEBUG</span>1046<span class="na">formatter</span><span class="o">=</span><span class="s">form02</span>1047<span class="na">args</span><span class="o">=</span><span class="s">('python.log', 'w')</span>1048 1049<span class="k">[handler_hand03]</span>1050<span class="na">class</span><span class="o">=</span><span class="s">handlers.SocketHandler</span>1051<span class="na">level</span><span class="o">=</span><span class="s">INFO</span>1052<span class="na">formatter</span><span class="o">=</span><span class="s">form03</span>1053<span class="na">args</span><span class="o">=</span><span class="s">('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)</span>1054 1055<span class="k">[handler_hand04]</span>1056<span class="na">class</span><span class="o">=</span><span class="s">handlers.DatagramHandler</span>1057<span class="na">level</span><span class="o">=</span><span class="s">WARN</span>1058<span class="na">formatter</span><span class="o">=</span><span class="s">form04</span>1059<span class="na">args</span><span class="o">=</span><span class="s">('localhost', handlers.DEFAULT_UDP_LOGGING_PORT)</span>1060 1061<span class="k">[handler_hand05]</span>1062<span class="na">class</span><span class="o">=</span><span class="s">handlers.SysLogHandler</span>1063<span class="na">level</span><span class="o">=</span><span class="s">ERROR</span>1064<span class="na">formatter</span><span class="o">=</span><span class="s">form05</span>1065<span class="na">args</span><span class="o">=</span><span class="s">(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)</span>1066 1067<span class="k">[handler_hand06]</span>1068<span class="na">class</span><span class="o">=</span><span class="s">handlers.NTEventLogHandler</span>1069<span class="na">level</span><span class="o">=</span><span class="s">CRITICAL</span>1070<span class="na">formatter</span><span class="o">=</span><span class="s">form06</span>1071<span class="na">args</span><span class="o">=</span><span class="s">('Python Application', '', 'Application')</span>1072 1073<span class="k">[handler_hand07]</span>1074<span class="na">class</span><span class="o">=</span><span class="s">handlers.SMTPHandler</span>1075<span class="na">level</span><span class="o">=</span><span class="s">WARN</span>1076<span class="na">formatter</span><span class="o">=</span><span class="s">form07</span>1077<span class="na">args</span><span class="o">=</span><span class="s">('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')</span>1078<span class="na">kwargs</span><span class="o">=</span><span class="s">{'timeout': 10.0}</span>1079 1080<span class="k">[handler_hand08]</span>1081<span class="na">class</span><span class="o">=</span><span class="s">handlers.MemoryHandler</span>1082<span class="na">level</span><span class="o">=</span><span class="s">NOTSET</span>1083<span class="na">formatter</span><span class="o">=</span><span class="s">form08</span>1084<span class="na">target</span><span class="o">=</span>1085<span class="na">args</span><span class="o">=</span><span class="s">(10, ERROR)</span>1086 1087<span class="k">[handler_hand09]</span>1088<span class="na">class</span><span class="o">=</span><span class="s">handlers.HTTPHandler</span>1089<span class="na">level</span><span class="o">=</span><span class="s">NOTSET</span>1090<span class="na">formatter</span><span class="o">=</span><span class="s">form09</span>1091<span class="na">args</span><span class="o">=</span><span class="s">('localhost:9022', '/log', 'GET')</span>1092<span class="na">kwargs</span><span class="o">=</span><span class="s">{'secure': True}</span>1093</pre></div>1094</div>1095<p>Sections which specify formatter configuration are typified by the following.</p>1096<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[formatter_form01]</span>1097<span class="na">format</span><span class="o">=</span><span class="s">F1 %(asctime)s %(levelname)s %(message)s %(customfield)s</span>1098<span class="na">datefmt</span><span class="o">=</span>1099<span class="na">style</span><span class="o">=</span><span class="s">%</span>1100<span class="na">validate</span><span class="o">=</span><span class="s">True</span>1101<span class="na">defaults={'customfield'</span><span class="o">:</span><span class="w"> </span><span class="s">'defaultvalue'</span><span class="na">}</span>1102<span class="na">class</span><span class="o">=</span><span class="s">logging.Formatter</span>1103</pre></div>1104</div>1105<p>The arguments for the formatter configuration are the same as the keys1106in the dictionary schema <a class="reference internal" href="#logging-config-dictschema-formatters"><span class="std std-ref">formatters section</span></a>.</p>1107<p>The <code class="docutils literal notranslate"><span class="pre">defaults</span></code> entry, when <a class="reference internal" href="functions.html#func-eval"><span class="std std-ref">evaluated</span></a> in the context of1108the <code class="docutils literal notranslate"><span class="pre">logging</span></code> package’s namespace, is a dictionary of default values for1109custom formatting fields. If not provided, it defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>1110<div class="admonition note">1111<p class="admonition-title">Note</p>1112<p>Due to the use of <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> as described above, there are1113potential security risks which result from using the <a class="reference internal" href="#logging.config.listen" title="logging.config.listen"><code class="xref py py-func docutils literal notranslate"><span class="pre">listen()</span></code></a> to send1114and receive configurations via sockets. The risks are limited to where1115multiple users with no mutual trust run code on the same machine; see the1116<code class="xref py py-func docutils literal notranslate"><span class="pre">listen()</span></code> documentation for more information.</p>1117</div>1118<div class="admonition seealso">1119<p class="admonition-title">See also</p>1120<dl class="simple">1121<dt>Module <a class="reference internal" href="logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a></dt><dd><p>API reference for the logging module.</p>1122</dd>1123<dt>Module <a class="reference internal" href="logging.handlers.html#module-logging.handlers" title="logging.handlers: Handlers for the logging module."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.handlers</span></code></a></dt><dd><p>Useful handlers included with the logging module.</p>1124</dd>1125</dl>1126</div>1127</section>1128</section>1129 1130 1131 <div class="clearer"></div>1132 </div>1133 </div>1134 </div>1135 <div class="sphinxsidebar" role="navigation" aria-label="Main">1136 <div class="sphinxsidebarwrapper">1137 <div>1138 <h3><a href="../contents.html">Table of Contents</a></h3>1139 <ul>1140<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code> — Logging configuration</a><ul>1141<li><a class="reference internal" href="#configuration-functions">Configuration functions</a></li>1142<li><a class="reference internal" href="#security-considerations">Security considerations</a></li>1143<li><a class="reference internal" href="#configuration-dictionary-schema">Configuration dictionary schema</a><ul>1144<li><a class="reference internal" href="#dictionary-schema-details">Dictionary Schema Details</a></li>1145<li><a class="reference internal" href="#incremental-configuration">Incremental Configuration</a></li>1146<li><a class="reference internal" href="#object-connections">Object connections</a></li>1147<li><a class="reference internal" href="#user-defined-objects">User-defined objects</a></li>1148<li><a class="reference internal" href="#handler-configuration-order">Handler configuration order</a></li>1149<li><a class="reference internal" href="#access-to-external-objects">Access to external objects</a></li>1150<li><a class="reference internal" href="#access-to-internal-objects">Access to internal objects</a></li>1151<li><a class="reference internal" href="#import-resolution-and-custom-importers">Import resolution and custom importers</a></li>1152<li><a class="reference internal" href="#configuring-queuehandler-and-queuelistener">Configuring QueueHandler and QueueListener</a></li>1153</ul>1154</li>1155<li><a class="reference internal" href="#configuration-file-format">Configuration file format</a></li>1156</ul>1157</li>1158</ul>1159 1160 </div>1161 <div>1162 <h4>Previous topic</h4>1163 <p class="topless"><a href="logging.html"1164 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></p>1165 </div>1166 <div>1167 <h4>Next topic</h4>1168 <p class="topless"><a href="logging.handlers.html"1169 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.handlers</span></code> — Logging handlers</a></p>1170 </div>1171 <script>1172 document.addEventListener('DOMContentLoaded', () => {1173 const title = document.querySelector('meta[property="og:title"]').content;1174 const elements = document.querySelectorAll('.improvepage');1175 const pageurl = window.location.href.split('?')[0];1176 elements.forEach(element => {1177 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));1178 url.searchParams.set('pagetitle', title);1179 url.searchParams.set('pageurl', pageurl);1180 url.searchParams.set('pagesource', "library/logging.config.rst");1181 element.href = url.toString();1182 });1183 });1184 </script>1185 <div role="note" aria-label="source link">1186 <h3>This page</h3>1187 <ul class="this-page-menu">1188 <li><a href="../bugs.html">Report a bug</a></li>1189 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>1190 <li>1191 <a href="https://github.com/python/cpython/blob/main/Doc/library/logging.config.rst?plain=1"1192 rel="nofollow">Show source1193 </a>1194 </li>1195 1196 </ul>1197 </div>1198 </div>1199<div id="sidebarbutton" title="Collapse sidebar">1200<span>«</span>