gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1248 lines
110KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="logging.config — Logging configuration" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/logging.config.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <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..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <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..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>logging.config — Logging configuration &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="logging.handlers — Logging handlers" href="logging.handlers.html" />
  33. <link rel="prev" title="logging — Logging facility for Python" href="logging.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/logging.config.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <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>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <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>
  86. <li><a class="reference internal" href="#configuration-functions">Configuration functions</a></li>
  87. <li><a class="reference internal" href="#security-considerations">Security considerations</a></li>
  88. <li><a class="reference internal" href="#configuration-dictionary-schema">Configuration dictionary schema</a><ul>
  89. <li><a class="reference internal" href="#dictionary-schema-details">Dictionary Schema Details</a></li>
  90. <li><a class="reference internal" href="#incremental-configuration">Incremental Configuration</a></li>
  91. <li><a class="reference internal" href="#object-connections">Object connections</a></li>
  92. <li><a class="reference internal" href="#user-defined-objects">User-defined objects</a></li>
  93. <li><a class="reference internal" href="#handler-configuration-order">Handler configuration order</a></li>
  94. <li><a class="reference internal" href="#access-to-external-objects">Access to external objects</a></li>
  95. <li><a class="reference internal" href="#access-to-internal-objects">Access to internal objects</a></li>
  96. <li><a class="reference internal" href="#import-resolution-and-custom-importers">Import resolution and custom importers</a></li>
  97. <li><a class="reference internal" href="#configuring-queuehandler-and-queuelistener">Configuring QueueHandler and QueueListener</a></li>
  98. </ul>
  99. </li>
  100. <li><a class="reference internal" href="#configuration-file-format">Configuration file format</a></li>
  101. </ul>
  102. </li>
  103. </ul>
  104. </div>
  105. <div>
  106. <h4>Previous topic</h4>
  107. <p class="topless"><a href="logging.html"
  108. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></p>
  109. </div>
  110. <div>
  111. <h4>Next topic</h4>
  112. <p class="topless"><a href="logging.handlers.html"
  113. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.handlers</span></code> — Logging handlers</a></p>
  114. </div>
  115. <div role="note" aria-label="source link">
  116. <h3>This Page</h3>
  117. <ul class="this-page-menu">
  118. <li><a href="../bugs.html">Report a Bug</a></li>
  119. <li>
  120. <a href="https://github.com/python/cpython/blob/main/Doc/library/logging.config.rst"
  121. rel="nofollow">Show Source
  122. </a>
  123. </li>
  124. </ul>
  125. </div>
  126. </nav>
  127. </div>
  128. </div>
  129. <div class="related" role="navigation" aria-label="related navigation">
  130. <h3>Navigation</h3>
  131. <ul>
  132. <li class="right" style="margin-right: 10px">
  133. <a href="../genindex.html" title="General Index"
  134. accesskey="I">index</a></li>
  135. <li class="right" >
  136. <a href="../py-modindex.html" title="Python Module Index"
  137. >modules</a> |</li>
  138. <li class="right" >
  139. <a href="logging.handlers.html" title="logging.handlers — Logging handlers"
  140. accesskey="N">next</a> |</li>
  141. <li class="right" >
  142. <a href="logging.html" title="logging — Logging facility for Python"
  143. accesskey="P">previous</a> |</li>
  144. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  145. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  146. <li class="switchers">
  147. <div class="language_switcher_placeholder"></div>
  148. <div class="version_switcher_placeholder"></div>
  149. </li>
  150. <li>
  151. </li>
  152. <li id="cpython-language-and-version">
  153. <a href="../index.html">3.12.3 Documentation</a> &#187;
  154. </li>
  155. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  156. <li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">Generic Operating System Services</a> &#187;</li>
  157. <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>
  158. <li class="right">
  159. <div class="inline-search" role="search">
  160. <form class="inline-search" action="../search.html" method="get">
  161. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  162. <input type="submit" value="Go" />
  163. </form>
  164. </div>
  165. |
  166. </li>
  167. <li class="right">
  168. <label class="theme-selector-label">
  169. Theme
  170. <select class="theme-selector" oninput="activateTheme(this.value)">
  171. <option value="auto" selected>Auto</option>
  172. <option value="light">Light</option>
  173. <option value="dark">Dark</option>
  174. </select>
  175. </label> |</li>
  176. </ul>
  177. </div>
  178. <div class="document">
  179. <div class="documentwrapper">
  180. <div class="bodywrapper">
  181. <div class="body" role="main">
  182. <section id="module-logging.config">
  183. <span id="logging-config-logging-configuration"></span><h1><a class="reference internal" href="#module-logging.config" title="logging.config: Configuration of the logging module."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code></a> — Logging configuration<a class="headerlink" href="#module-logging.config" title="Link to this heading">¶</a></h1>
  184. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/logging/config.py">Lib/logging/config.py</a></p>
  185. <aside class="sidebar">
  186. <p class="sidebar-title">Important</p>
  187. <p>This page contains only reference information. For tutorials,
  188. please see</p>
  189. <ul class="simple">
  190. <li><p><a class="reference internal" href="../howto/logging.html#logging-basic-tutorial"><span class="std std-ref">Basic Tutorial</span></a></p></li>
  191. <li><p><a class="reference internal" href="../howto/logging.html#logging-advanced-tutorial"><span class="std std-ref">Advanced Tutorial</span></a></p></li>
  192. <li><p><a class="reference internal" href="../howto/logging-cookbook.html#logging-cookbook"><span class="std std-ref">Logging Cookbook</span></a></p></li>
  193. </ul>
  194. </aside>
  195. <hr class="docutils" />
  196. <p>This section describes the API for configuring the logging module.</p>
  197. <section id="configuration-functions">
  198. <span id="logging-config-api"></span><h2>Configuration functions<a class="headerlink" href="#configuration-functions" title="Link to this heading">¶</a></h2>
  199. <p>The following functions configure the logging module. They are located in the
  200. <a class="reference internal" href="#module-logging.config" title="logging.config: Configuration of the logging module."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code></a> module. Their use is optional — you can configure the
  201. logging module using these functions or by making calls to the main API (defined
  202. in <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 in
  203. <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> 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>
  204. <dl class="py function">
  205. <dt class="sig sig-object py" id="logging.config.dictConfig">
  206. <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>
  207. <dd><p>Takes the logging configuration from a dictionary. The contents of
  208. this dictionary are described in <a class="reference internal" href="#logging-config-dictschema"><span class="std std-ref">Configuration dictionary schema</span></a>
  209. below.</p>
  210. <p>If an error is encountered during configuration, this function will
  211. raise 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>
  212. or <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. The
  213. following is a (possibly incomplete) list of conditions which will
  214. raise an error:</p>
  215. <ul class="simple">
  216. <li><p>A <code class="docutils literal notranslate"><span class="pre">level</span></code> which is not a string or which is a string not
  217. corresponding to an actual logging level.</p></li>
  218. <li><p>A <code class="docutils literal notranslate"><span class="pre">propagate</span></code> value which is not a boolean.</p></li>
  219. <li><p>An id which does not have a corresponding destination.</p></li>
  220. <li><p>A non-existent handler id found during an incremental call.</p></li>
  221. <li><p>An invalid logger name.</p></li>
  222. <li><p>Inability to resolve to an internal or external object.</p></li>
  223. </ul>
  224. <p>Parsing is performed by the <code class="xref py py-class docutils literal notranslate"><span class="pre">DictConfigurator</span></code> class, whose
  225. constructor is passed the dictionary used for configuration, and
  226. has a <code class="xref py py-meth docutils literal notranslate"><span class="pre">configure()</span></code> method. The <a class="reference internal" href="#module-logging.config" title="logging.config: Configuration of the logging module."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code></a> module
  227. has a callable attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">dictConfigClass</span></code>
  228. which is initially set to <code class="xref py py-class docutils literal notranslate"><span class="pre">DictConfigurator</span></code>.
  229. You can replace the value of <code class="xref py py-attr docutils literal notranslate"><span class="pre">dictConfigClass</span></code> with a
  230. suitable implementation of your own.</p>
  231. <p><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> calls <code class="xref py py-attr docutils literal notranslate"><span class="pre">dictConfigClass</span></code> passing
  232. the specified dictionary, and then calls the <code class="xref py py-meth docutils literal notranslate"><span class="pre">configure()</span></code> method on
  233. the returned object to put the configuration into effect:</p>
  234. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">dictConfig</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
  235. <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>
  236. </pre></div>
  237. </div>
  238. <p>For example, a subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">DictConfigurator</span></code> could call
  239. <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>, then
  240. set up custom prefixes which would be usable in the subsequent
  241. <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 to
  242. this new subclass, and then <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> could be called exactly as
  243. in the default, uncustomized state.</p>
  244. <div class="versionadded">
  245. <p><span class="versionmodified added">New in version 3.2.</span></p>
  246. </div>
  247. </dd></dl>
  248. <dl class="py function">
  249. <dt class="sig sig-object py" id="logging.config.fileConfig">
  250. <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>
  251. <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. The
  252. format of the file should be as described in
  253. <a class="reference internal" href="#logging-config-fileformat"><span class="std std-ref">Configuration file format</span></a>.
  254. This function can be called several times from an application, allowing an
  255. end user to select from various pre-canned configurations (if the developer
  256. provides a mechanism to present the choices and load the chosen
  257. configuration).</p>
  258. <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 file
  259. doesn’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 or
  260. empty.</p>
  261. <dl class="field-list simple">
  262. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  263. <dd class="field-odd"><ul class="simple">
  264. <li><p><strong>fname</strong> – A filename, or a file-like object, or an instance derived
  265. from <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 a
  266. <code class="xref py py-class docutils literal notranslate"><span class="pre">RawConfigParser</span></code>-derived instance is passed, it is used as
  267. is. 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> is
  268. instantiated, and the configuration read by it from the
  269. object 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. (Unix)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a>
  270. method, it is assumed to be a file-like object and read using
  271. <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,
  272. it is assumed to be a filename and passed to
  273. <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>
  274. <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 specified
  275. in this argument.</p></li>
  276. <li><p><strong>disable_existing_loggers</strong> – If specified as <code class="docutils literal notranslate"><span class="pre">False</span></code>, loggers which
  277. exist when this call is made are left
  278. enabled. The default is <code class="docutils literal notranslate"><span class="pre">True</span></code> because this
  279. enables old behaviour in a
  280. backward-compatible way. This behaviour is to
  281. disable any existing non-root loggers unless
  282. they or their ancestors are explicitly named
  283. in the logging configuration.</p></li>
  284. <li><p><strong>encoding</strong> – The encoding used to open file when <em>fname</em> is filename.</p></li>
  285. </ul>
  286. </dd>
  287. </dl>
  288. <div class="versionchanged">
  289. <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> is
  290. now accepted as a value for <code class="docutils literal notranslate"><span class="pre">fname</span></code>. This facilitates:</p>
  291. <blockquote>
  292. <div><ul class="simple">
  293. <li><p>Use of a configuration file where logging configuration is just part
  294. of the overall application configuration.</p></li>
  295. <li><p>Use of a configuration read from a file, and then modified by the using
  296. application (e.g. based on command-line parameters or other aspects
  297. of the runtime environment) before being passed to <code class="docutils literal notranslate"><span class="pre">fileConfig</span></code>.</p></li>
  298. </ul>
  299. </div></blockquote>
  300. <div class="versionchanged">
  301. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>encoding</em> parameter.</p>
  302. </div>
  303. <div class="versionchanged">
  304. <p><span class="versionmodified changed">Changed in version 3.12: </span>An exception will be thrown if the provided file
  305. doesn’t exist or is invalid or empty.</p>
  306. </div>
  307. </div>
  308. </dd></dl>
  309. <dl class="py function">
  310. <dt class="sig sig-object py" id="logging.config.listen">
  311. <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>
  312. <dd><p>Starts up a socket server on the specified port, and listens for new
  313. configurations. If no port is specified, the module’s default
  314. <code class="xref py py-const docutils literal notranslate"><span class="pre">DEFAULT_LOGGING_CONFIG_PORT</span></code> is used. Logging configurations will be
  315. sent 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> or
  316. <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 which
  317. you 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 which
  318. you 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,
  319. call <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>
  320. <p>The <code class="docutils literal notranslate"><span class="pre">verify</span></code> argument, if specified, should be a callable which should
  321. verify whether bytes received across the socket are valid and should be
  322. processed. This could be done by encrypting and/or signing what is sent
  323. across the socket, such that the <code class="docutils literal notranslate"><span class="pre">verify</span></code> callable can perform
  324. signature verification and/or decryption. The <code class="docutils literal notranslate"><span class="pre">verify</span></code> callable is called
  325. with a single argument - the bytes received across the socket - and should
  326. return the bytes to be processed, or <code class="docutils literal notranslate"><span class="pre">None</span></code> to indicate that the bytes should
  327. be discarded. The returned bytes could be the same as the passed in bytes
  328. (e.g. when only verification is done), or they could be completely different
  329. (perhaps if decryption were performed).</p>
  330. <p>To send a configuration to the socket, read in the configuration file and
  331. send it to the socket as a sequence of bytes preceded by a four-byte length
  332. string packed in binary using <code class="docutils literal notranslate"><span class="pre">struct.pack('&gt;L',</span> <span class="pre">n)</span></code>.</p>
  333. <div class="admonition note" id="logging-eval-security">
  334. <p class="admonition-title">Note</p>
  335. <p>Because portions of the configuration are passed through
  336. <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.
  337. While the function only binds to a socket on <code class="docutils literal notranslate"><span class="pre">localhost</span></code>, and so does
  338. not accept connections from remote machines, there are scenarios where
  339. untrusted code could be run under the account of the process which calls
  340. <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>. Specifically, if the process calling <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> runs
  341. on a multi-user machine where users cannot trust each other, then a
  342. malicious user could arrange to run essentially arbitrary code in a
  343. victim user’s process, simply by connecting to the victim’s
  344. <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> socket and sending a configuration which runs whatever
  345. code the attacker wants to have executed in the victim’s process. This is
  346. especially easy to do if the default port is used, but not hard even if a
  347. different port is used. To avoid the risk of this happening, use the
  348. <code class="docutils literal notranslate"><span class="pre">verify</span></code> argument 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> to prevent unrecognised
  349. configurations from being applied.</p>
  350. </div>
  351. <div class="versionchanged">
  352. <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>
  353. </div>
  354. <div class="admonition note">
  355. <p class="admonition-title">Note</p>
  356. <p>If you want to send configurations to the listener which don’t
  357. disable existing loggers, you will need to use a JSON format for
  358. the 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.
  359. This method allows you to specify <code class="docutils literal notranslate"><span class="pre">disable_existing_loggers</span></code> as
  360. <code class="docutils literal notranslate"><span class="pre">False</span></code> in the configuration you send.</p>
  361. </div>
  362. </dd></dl>
  363. <dl class="py function">
  364. <dt class="sig sig-object py" id="logging.config.stopListening">
  365. <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>
  366. <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>.
  367. This is typically called before calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code> on the return value from
  368. <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>.</p>
  369. </dd></dl>
  370. </section>
  371. <section id="security-considerations">
  372. <h2>Security considerations<a class="headerlink" href="#security-considerations" title="Link to this heading">¶</a></h2>
  373. <p>The logging configuration functionality tries to offer convenience, and in part this
  374. is done by offering the ability to convert text in configuration files into Python
  375. objects used in logging configuration - for example, as described in
  376. <a class="reference internal" href="#logging-config-dict-userdef"><span class="std std-ref">User-defined objects</span></a>. However, these same mechanisms (importing
  377. callables from user-defined modules and calling them with parameters from the
  378. configuration) could be used to invoke any code you like, and for this reason you
  379. should treat configuration files from untrusted sources with <em>extreme caution</em> and
  380. satisfy yourself that nothing bad can happen if you load them, before actually loading
  381. them.</p>
  382. </section>
  383. <section id="configuration-dictionary-schema">
  384. <span id="logging-config-dictschema"></span><h2>Configuration dictionary schema<a class="headerlink" href="#configuration-dictionary-schema" title="Link to this heading">¶</a></h2>
  385. <p>Describing a logging configuration requires listing the various
  386. objects to create and the connections between them; for example, you
  387. may create a handler named ‘console’ and then say that the logger
  388. named ‘startup’ will send its messages to the ‘console’ handler.
  389. These 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>
  390. module because you might write your own formatter or handler class.
  391. The parameters to these classes may also need to include external
  392. objects such as <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>. The syntax for describing these
  393. objects and connections is defined in <a class="reference internal" href="#logging-config-dict-connections"><span class="std std-ref">Object connections</span></a>
  394. below.</p>
  395. <section id="dictionary-schema-details">
  396. <h3>Dictionary Schema Details<a class="headerlink" href="#dictionary-schema-details" title="Link to this heading">¶</a></h3>
  397. <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 following
  398. keys:</p>
  399. <ul class="simple">
  400. <li><p><em>version</em> - to be set to an integer value representing the schema
  401. version. The only valid value at present is 1, but having this key
  402. allows the schema to evolve while still preserving backwards
  403. compatibility.</p></li>
  404. </ul>
  405. <p>All other keys are optional, but if present they will be interpreted
  406. as described below. In all cases below where a ‘configuring dict’ is
  407. mentioned, it will be checked for the special <code class="docutils literal notranslate"><span class="pre">'()'</span></code> key to see if a
  408. custom instantiation is required. If so, the mechanism described in
  409. <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;
  410. otherwise, the context is used to determine what to instantiate.</p>
  411. <ul id="logging-config-dictschema-formatters">
  412. <li><p><em>formatters</em> - the corresponding value will be a dict in which each
  413. key is a formatter id and each value is a dict describing how to
  414. configure 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>
  415. <p>The configuring dict is searched for the following optional keys
  416. which correspond to the arguments passed to create a
  417. <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>
  418. <ul class="simple">
  419. <li><p><code class="docutils literal notranslate"><span class="pre">format</span></code></p></li>
  420. <li><p><code class="docutils literal notranslate"><span class="pre">datefmt</span></code></p></li>
  421. <li><p><code class="docutils literal notranslate"><span class="pre">style</span></code></p></li>
  422. <li><p><code class="docutils literal notranslate"><span class="pre">validate</span></code> (since version &gt;=3.8)</p></li>
  423. <li><p><code class="docutils literal notranslate"><span class="pre">defaults</span></code> (since version &gt;=3.12)</p></li>
  424. </ul>
  425. <p>An optional <code class="docutils literal notranslate"><span class="pre">class</span></code> key indicates the name of the formatter’s
  426. class (as a dotted module and class name). The instantiation
  427. arguments 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 is
  428. most useful for instantiating a customised subclass of
  429. <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>. For example, the alternative class
  430. might present exception tracebacks in an expanded or condensed
  431. format. If your formatter requires different or extra configuration
  432. keys, you should use <a class="reference internal" href="#logging-config-dict-userdef"><span class="std std-ref">User-defined objects</span></a>.</p>
  433. </li>
  434. <li><p><em>filters</em> - the corresponding value will be a dict in which each key
  435. is a filter id and each value is a dict describing how to configure
  436. the corresponding Filter instance.</p>
  437. <p>The configuring dict is searched for the key <code class="docutils literal notranslate"><span class="pre">name</span></code> (defaulting to the
  438. empty 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>
  439. instance.</p>
  440. </li>
  441. <li><p><em>handlers</em> - the corresponding value will be a dict in which each
  442. key is a handler id and each value is a dict describing how to
  443. configure the corresponding Handler instance.</p>
  444. <p>The configuring dict is searched for the following keys:</p>
  445. <ul>
  446. <li><p><code class="docutils literal notranslate"><span class="pre">class</span></code> (mandatory). This is the fully qualified name of the
  447. handler class.</p></li>
  448. <li><p><code class="docutils literal notranslate"><span class="pre">level</span></code> (optional). The level of the handler.</p></li>
  449. <li><p><code class="docutils literal notranslate"><span class="pre">formatter</span></code> (optional). The id of the formatter for this
  450. handler.</p></li>
  451. <li><p><code class="docutils literal notranslate"><span class="pre">filters</span></code> (optional). A list of ids of the filters for this
  452. handler.</p>
  453. <div class="versionchanged">
  454. <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>
  455. </div>
  456. </li>
  457. </ul>
  458. <p>All <em>other</em> keys are passed through as keyword arguments to the
  459. handler’s constructor. For example, given the snippet:</p>
  460. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">handlers</span><span class="p">:</span>
  461. <span class="w"> </span><span class="nt">console</span><span class="p">:</span>
  462. <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>
  463. <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>
  464. <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>
  465. <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>
  466. <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>
  467. <span class="w"> </span><span class="nt">file</span><span class="p">:</span>
  468. <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>
  469. <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>
  470. <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>
  471. <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>
  472. <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>
  473. </pre></div>
  474. </div>
  475. <p>the handler with id <code class="docutils literal notranslate"><span class="pre">console</span></code> is instantiated as a
  476. <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 underlying
  477. stream. The handler with id <code class="docutils literal notranslate"><span class="pre">file</span></code> is instantiated as a
  478. <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 arguments
  479. <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>
  480. </li>
  481. <li><p><em>loggers</em> - the corresponding value will be a dict in which each key
  482. is a logger name and each value is a dict describing how to
  483. configure the corresponding Logger instance.</p>
  484. <p>The configuring dict is searched for the following keys:</p>
  485. <ul>
  486. <li><p><code class="docutils literal notranslate"><span class="pre">level</span></code> (optional). The level of the logger.</p></li>
  487. <li><p><code class="docutils literal notranslate"><span class="pre">propagate</span></code> (optional). The propagation setting of the logger.</p></li>
  488. <li><p><code class="docutils literal notranslate"><span class="pre">filters</span></code> (optional). A list of ids of the filters for this
  489. logger.</p>
  490. <div class="versionchanged">
  491. <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>
  492. </div>
  493. </li>
  494. <li><p><code class="docutils literal notranslate"><span class="pre">handlers</span></code> (optional). A list of ids of the handlers for this
  495. logger.</p></li>
  496. </ul>
  497. <p>The specified loggers will be configured according to the level,
  498. propagation, filters and handlers specified.</p>
  499. </li>
  500. <li><p><em>root</em> - this will be the configuration for the root logger.
  501. Processing of the configuration will be as for any logger, except
  502. that the <code class="docutils literal notranslate"><span class="pre">propagate</span></code> setting will not be applicable.</p></li>
  503. <li><p><em>incremental</em> - whether the configuration is to be interpreted as
  504. incremental to the existing configuration. This value defaults to
  505. <code class="docutils literal notranslate"><span class="pre">False</span></code>, which means that the specified configuration replaces the
  506. existing configuration with the same semantics as used by the
  507. existing <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>
  508. <p>If the specified value is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the configuration is processed
  509. as described in the section on <a class="reference internal" href="#logging-config-dict-incremental"><span class="std std-ref">Incremental Configuration</span></a>.</p>
  510. </li>
  511. <li><p><em>disable_existing_loggers</em> - whether any existing non-root loggers are
  512. to be disabled. This setting mirrors the parameter of the same name in
  513. <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>.
  514. This value is ignored if <em>incremental</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p></li>
  515. </ul>
  516. </section>
  517. <section id="incremental-configuration">
  518. <span id="logging-config-dict-incremental"></span><h3>Incremental Configuration<a class="headerlink" href="#incremental-configuration" title="Link to this heading">¶</a></h3>
  519. <p>It is difficult to provide complete flexibility for incremental
  520. configuration. For example, because objects such as filters
  521. and formatters are anonymous, once a configuration is set up, it is
  522. not possible to refer to such anonymous objects when augmenting a
  523. configuration.</p>
  524. <p>Furthermore, there is not a compelling case for arbitrarily altering
  525. the object graph of loggers, handlers, filters, formatters at
  526. run-time, once a configuration is set up; the verbosity of loggers and
  527. handlers can be controlled just by setting levels (and, in the case of
  528. loggers, propagation flags). Changing the object graph arbitrarily in
  529. a safe way is problematic in a multi-threaded environment; while not
  530. impossible, the benefits are not worth the complexity it adds to the
  531. implementation.</p>
  532. <p>Thus, when the <code class="docutils literal notranslate"><span class="pre">incremental</span></code> key of a configuration dict is present
  533. and 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> and
  534. <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>
  535. settings 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> and
  536. <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>
  537. <p>Using a value in the configuration dict lets configurations to be sent
  538. over the wire as pickled dicts to a socket listener. Thus, the logging
  539. verbosity of a long-running application can be altered over time with
  540. no need to stop and restart the application.</p>
  541. </section>
  542. <section id="object-connections">
  543. <span id="logging-config-dict-connections"></span><h3>Object connections<a class="headerlink" href="#object-connections" title="Link to this heading">¶</a></h3>
  544. <p>The schema describes a set of logging objects - loggers,
  545. handlers, formatters, filters - which are connected to each other in
  546. an object graph. Thus, the schema needs to represent connections
  547. between the objects. For example, say that, once configured, a
  548. particular logger has attached to it a particular handler. For the
  549. purposes of this discussion, we can say that the logger represents the
  550. source, and the handler the destination, of a connection between the
  551. two. Of course in the configured objects this is represented by the
  552. logger holding a reference to the handler. In the configuration dict,
  553. this is done by giving each destination object an id which identifies
  554. it unambiguously, and then using the id in the source object’s
  555. configuration to indicate that a connection exists between the source
  556. and the destination object with that id.</p>
  557. <p>So, for example, consider the following YAML snippet:</p>
  558. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">formatters</span><span class="p">:</span>
  559. <span class="w"> </span><span class="nt">brief</span><span class="p">:</span>
  560. <span class="w"> </span><span class="c1"># configuration for formatter with id &#39;brief&#39; goes here</span>
  561. <span class="w"> </span><span class="nt">precise</span><span class="p">:</span>
  562. <span class="w"> </span><span class="c1"># configuration for formatter with id &#39;precise&#39; goes here</span>
  563. <span class="nt">handlers</span><span class="p">:</span>
  564. <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>
  565. <span class="w"> </span><span class="c1"># configuration of handler with id &#39;h1&#39; goes here</span>
  566. <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>
  567. <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>
  568. <span class="w"> </span><span class="c1"># configuration of handler with id &#39;h2&#39; goes here</span>
  569. <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>
  570. <span class="nt">loggers</span><span class="p">:</span>
  571. <span class="w"> </span><span class="nt">foo.bar.baz</span><span class="p">:</span>
  572. <span class="w"> </span><span class="c1"># other configuration for logger &#39;foo.bar.baz&#39;</span>
  573. <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>
  574. </pre></div>
  575. </div>
  576. <p>(Note: YAML used here because it’s a little more readable than the
  577. equivalent Python source form for the dictionary.)</p>
  578. <p>The ids for loggers are the logger names which would be used
  579. programmatically to obtain a reference to those loggers, e.g.
  580. <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>. The ids for Formatters and Filters can be any string
  581. value (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,
  582. in that they are only meaningful for processing the configuration
  583. dictionary and used to determine connections between objects, and are
  584. not persisted anywhere when the configuration call is complete.</p>
  585. <p>The above snippet indicates that logger named <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code> should
  586. have two handlers attached to it, which are described by the handler
  587. ids <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 id
  588. <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 id
  589. <code class="docutils literal notranslate"><span class="pre">precise</span></code>.</p>
  590. </section>
  591. <section id="user-defined-objects">
  592. <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>
  593. <p>The schema supports user-defined objects for handlers, filters and
  594. formatters. (Loggers do not need to have different types for
  595. different instances, so there is no support in this configuration
  596. schema for user-defined logger classes.)</p>
  597. <p>Objects to be configured are described by dictionaries
  598. which detail their configuration. In some places, the logging system
  599. will be able to infer from the context how an object is to be
  600. instantiated, but when a user-defined object is to be instantiated,
  601. the system will not know how to do this. In order to provide complete
  602. flexibility for user-defined object instantiation, the user needs
  603. to provide a ‘factory’ - a callable which is called with a
  604. configuration dictionary and which returns the instantiated object.
  605. This is signalled by an absolute import path to the factory being
  606. made available under the special key <code class="docutils literal notranslate"><span class="pre">'()'</span></code>. Here’s a concrete
  607. example:</p>
  608. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">formatters</span><span class="p">:</span>
  609. <span class="w"> </span><span class="nt">brief</span><span class="p">:</span>
  610. <span class="w"> </span><span class="nt">format</span><span class="p">:</span><span class="w"> </span><span class="s">&#39;%(message)s&#39;</span>
  611. <span class="w"> </span><span class="nt">default</span><span class="p">:</span>
  612. <span class="w"> </span><span class="nt">format</span><span class="p">:</span><span class="w"> </span><span class="s">&#39;%(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&#39;</span>
  613. <span class="w"> </span><span class="nt">datefmt</span><span class="p">:</span><span class="w"> </span><span class="s">&#39;%Y-%m-%d</span><span class="nv"> </span><span class="s">%H:%M:%S&#39;</span>
  614. <span class="w"> </span><span class="nt">custom</span><span class="p">:</span>
  615. <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>
  616. <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>
  617. <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>
  618. <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>
  619. </pre></div>
  620. </div>
  621. <p>The above YAML snippet defines three formatters. The first, with id
  622. <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 the
  623. specified format string. The second, with id <code class="docutils literal notranslate"><span class="pre">default</span></code>, has a
  624. longer format and also defines the time format explicitly, and will
  625. result in a <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> initialized with those two format
  626. strings. 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>
  627. formatters have configuration sub-dictionaries:</p>
  628. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
  629. <span class="s1">&#39;format&#39;</span> <span class="p">:</span> <span class="s1">&#39;</span><span class="si">%(message)s</span><span class="s1">&#39;</span>
  630. <span class="p">}</span>
  631. </pre></div>
  632. </div>
  633. <p>and:</p>
  634. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
  635. <span class="s1">&#39;format&#39;</span> <span class="p">:</span> <span class="s1">&#39;</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">&#39;</span><span class="p">,</span>
  636. <span class="s1">&#39;datefmt&#39;</span> <span class="p">:</span> <span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span>
  637. <span class="p">}</span>
  638. </pre></div>
  639. </div>
  640. <p>respectively, and as these dictionaries do not contain the special key
  641. <code class="docutils literal notranslate"><span class="pre">'()'</span></code>, the instantiation is inferred from the context: as a result,
  642. 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> instances are created. The
  643. configuration sub-dictionary for the third formatter, with id
  644. <code class="docutils literal notranslate"><span class="pre">custom</span></code>, is:</p>
  645. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
  646. <span class="s1">&#39;()&#39;</span> <span class="p">:</span> <span class="s1">&#39;my.package.customFormatterFactory&#39;</span><span class="p">,</span>
  647. <span class="s1">&#39;bar&#39;</span> <span class="p">:</span> <span class="s1">&#39;baz&#39;</span><span class="p">,</span>
  648. <span class="s1">&#39;spam&#39;</span> <span class="p">:</span> <span class="mf">99.9</span><span class="p">,</span>
  649. <span class="s1">&#39;answer&#39;</span> <span class="p">:</span> <span class="mi">42</span>
  650. <span class="p">}</span>
  651. </pre></div>
  652. </div>
  653. <p>and this contains the special key <code class="docutils literal notranslate"><span class="pre">'()'</span></code>, which means that
  654. user-defined instantiation is wanted. In this case, the specified
  655. factory callable will be used. If it is an actual callable it will be
  656. used directly - otherwise, if you specify a string (as in the example)
  657. the actual callable will be located using normal import mechanisms.
  658. The callable will be called with the <strong>remaining</strong> items in the
  659. configuration sub-dictionary as keyword arguments. In the above
  660. example, the formatter with id <code class="docutils literal notranslate"><span class="pre">custom</span></code> will be assumed to be
  661. returned by the call:</p>
  662. <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">&#39;baz&#39;</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>
  663. </pre></div>
  664. </div>
  665. <div class="admonition warning">
  666. <p class="admonition-title">Warning</p>
  667. <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> in
  668. the above example should not be configuration dictionaries or references such
  669. as <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 the
  670. configuration machinery, but passed to the callable as-is.</p>
  671. </div>
  672. <p>The key <code class="docutils literal notranslate"><span class="pre">'()'</span></code> has been used as the special key because it is not a
  673. valid keyword parameter name, and so will not clash with the names of
  674. the keyword arguments used in the call. The <code class="docutils literal notranslate"><span class="pre">'()'</span></code> also serves as a
  675. mnemonic that the corresponding value is a callable.</p>
  676. <div class="versionchanged">
  677. <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 take
  678. filter instances in addition to ids.</p>
  679. </div>
  680. <p>You can also specify a special key <code class="docutils literal notranslate"><span class="pre">'.'</span></code> whose value is a dictionary is a
  681. mapping of attribute names to values. If found, the specified attributes will
  682. be set on the user-defined object before it is returned. Thus, with the
  683. following configuration:</p>
  684. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
  685. <span class="s1">&#39;()&#39;</span> <span class="p">:</span> <span class="s1">&#39;my.package.customFormatterFactory&#39;</span><span class="p">,</span>
  686. <span class="s1">&#39;bar&#39;</span> <span class="p">:</span> <span class="s1">&#39;baz&#39;</span><span class="p">,</span>
  687. <span class="s1">&#39;spam&#39;</span> <span class="p">:</span> <span class="mf">99.9</span><span class="p">,</span>
  688. <span class="s1">&#39;answer&#39;</span> <span class="p">:</span> <span class="mi">42</span><span class="p">,</span>
  689. <span class="s1">&#39;.&#39;</span> <span class="p">{</span>
  690. <span class="s1">&#39;foo&#39;</span><span class="p">:</span> <span class="s1">&#39;bar&#39;</span><span class="p">,</span>
  691. <span class="s1">&#39;baz&#39;</span><span class="p">:</span> <span class="s1">&#39;bozz&#39;</span>
  692. <span class="p">}</span>
  693. <span class="p">}</span>
  694. </pre></div>
  695. </div>
  696. <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> and
  697. attribute <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>
  698. <div class="admonition warning">
  699. <p class="admonition-title">Warning</p>
  700. <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> in
  701. the above example should not be configuration dictionaries or references such
  702. as <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 the
  703. configuration machinery, but set as attribute values as-is.</p>
  704. </div>
  705. </section>
  706. <section id="handler-configuration-order">
  707. <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>
  708. <p>Handlers are configured in alphabetical order of their keys, and a configured
  709. handler replaces the configuration dictionary in (a working copy of) the
  710. <code class="docutils literal notranslate"><span class="pre">handlers</span></code> dictionary in the schema. If you use a construct such as
  711. <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 the
  712. configuration dictionary for the handler named <code class="docutils literal notranslate"><span class="pre">foo</span></code>, and later (once that
  713. handler has been configured) it points to the configured handler instance.
  714. Thus, <code class="docutils literal notranslate"><span class="pre">cfg://handlers.foo</span></code> could resolve to either a dictionary or a handler
  715. instance. In general, it is wise to name handlers in a way such that dependent
  716. handlers are configured _after_ any handlers they depend on; that allows
  717. something like <code class="docutils literal notranslate"><span class="pre">cfg://handlers.foo</span></code> to be used in configuring a handler that
  718. depends 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>,
  719. problems would result, because the configuration of <code class="docutils literal notranslate"><span class="pre">bar</span></code> would be attempted
  720. before 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.
  721. However, if the dependent handler were named <code class="docutils literal notranslate"><span class="pre">foobar</span></code>, it would be configured
  722. after <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 to
  723. configured handler <code class="docutils literal notranslate"><span class="pre">foo</span></code>, and not its configuration dictionary.</p>
  724. </section>
  725. <section id="access-to-external-objects">
  726. <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>
  727. <p>There are times where a configuration needs to refer to objects
  728. external to the configuration, for example <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>. If the
  729. configuration dict is constructed using Python code, this is
  730. straightforward, but a problem arises when the configuration is
  731. provided via a text file (e.g. JSON, YAML). In a text file, there is
  732. no standard way to distinguish <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> from the literal string
  733. <code class="docutils literal notranslate"><span class="pre">'sys.stderr'</span></code>. To facilitate this distinction, the configuration
  734. system looks for certain special prefixes in string values and
  735. treat them specially. For example, if the literal string
  736. <code class="docutils literal notranslate"><span class="pre">'ext://sys.stderr'</span></code> is provided as a value in the configuration,
  737. then the <code class="docutils literal notranslate"><span class="pre">ext://</span></code> will be stripped off and the remainder of the
  738. value processed using normal import mechanisms.</p>
  739. <p>The handling of such prefixes is done in a way analogous to protocol
  740. handling: there is a generic mechanism to look for prefixes which
  741. match the regular expression <code class="docutils literal notranslate"><span class="pre">^(?P&lt;prefix&gt;[a-z]+)://(?P&lt;suffix&gt;.*)$</span></code>
  742. whereby, 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 processed
  743. in a prefix-dependent manner and the result of the processing replaces
  744. the string value. If the prefix is not recognised, then the string
  745. value will be left as-is.</p>
  746. </section>
  747. <section id="access-to-internal-objects">
  748. <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>
  749. <p>As well as external objects, there is sometimes also a need to refer
  750. to objects in the configuration. This will be done implicitly by the
  751. configuration system for things that it knows about. For example, the
  752. string 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 will
  753. automatically be converted to the value <code class="docutils literal notranslate"><span class="pre">logging.DEBUG</span></code>, and the
  754. <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 an
  755. object id and resolve to the appropriate destination object.</p>
  756. <p>However, a more generic mechanism is needed for user-defined
  757. objects 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. For
  758. example, 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 takes
  759. a <code class="docutils literal notranslate"><span class="pre">target</span></code> argument which is another handler to delegate to. Since
  760. the system already knows about this class, then in the configuration,
  761. the given <code class="docutils literal notranslate"><span class="pre">target</span></code> just needs to be the object id of the relevant
  762. target handler, and the system will resolve to the handler from the
  763. id. If, however, a user defines a <code class="docutils literal notranslate"><span class="pre">my.package.MyHandler</span></code> which has
  764. an <code class="docutils literal notranslate"><span class="pre">alternate</span></code> handler, the configuration system would not know that
  765. the <code class="docutils literal notranslate"><span class="pre">alternate</span></code> referred to a handler. To cater for this, a generic
  766. resolution system allows the user to specify:</p>
  767. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">handlers</span><span class="p">:</span>
  768. <span class="w"> </span><span class="nt">file</span><span class="p">:</span>
  769. <span class="w"> </span><span class="c1"># configuration of file handler goes here</span>
  770. <span class="w"> </span><span class="nt">custom</span><span class="p">:</span>
  771. <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>
  772. <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>
  773. </pre></div>
  774. </div>
  775. <p>The literal string <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.file'</span></code> will be resolved in an
  776. analogous way to strings with the <code class="docutils literal notranslate"><span class="pre">ext://</span></code> prefix, but looking
  777. in the configuration itself rather than the import namespace. The
  778. mechanism allows access by dot or by index, in a similar way to
  779. that provided by <code class="docutils literal notranslate"><span class="pre">str.format</span></code>. Thus, given the following snippet:</p>
  780. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">handlers</span><span class="p">:</span>
  781. <span class="w"> </span><span class="nt">email</span><span class="p">:</span>
  782. <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>
  783. <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>
  784. <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>
  785. <span class="w"> </span><span class="nt">toaddrs</span><span class="p">:</span>
  786. <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>
  787. <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>
  788. <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>
  789. </pre></div>
  790. </div>
  791. <p>in the configuration, the string <code class="docutils literal notranslate"><span class="pre">'cfg://handlers'</span></code> would resolve to
  792. the 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>
  793. would 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,
  794. and so on. The string <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email.toaddrs[1]</span></code> would
  795. resolve to <code class="docutils literal notranslate"><span class="pre">'dev_team&#64;domain.tld'</span></code> and the string
  796. <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email.toaddrs[0]'</span></code> would resolve to the value
  797. <code class="docutils literal notranslate"><span class="pre">'support_team&#64;domain.tld'</span></code>. The <code class="docutils literal notranslate"><span class="pre">subject</span></code> value could be accessed
  798. using either <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email.subject'</span></code> or, equivalently,
  799. <code class="docutils literal notranslate"><span class="pre">'cfg://handlers.email[subject]'</span></code>. The latter form only needs to be
  800. used if the key contains spaces or non-alphanumeric characters. If an
  801. index value consists only of decimal digits, access will be attempted
  802. using the corresponding integer value, falling back to the string
  803. value if needed.</p>
  804. <p>Given a string <code class="docutils literal notranslate"><span class="pre">cfg://handlers.myhandler.mykey.123</span></code>, this will
  805. resolve to <code class="docutils literal notranslate"><span class="pre">config_dict['handlers']['myhandler']['mykey']['123']</span></code>.
  806. If the string is specified as <code class="docutils literal notranslate"><span class="pre">cfg://handlers.myhandler.mykey[123]</span></code>,
  807. the system will attempt to retrieve the value from
  808. <code class="docutils literal notranslate"><span class="pre">config_dict['handlers']['myhandler']['mykey'][123]</span></code>, and fall back
  809. to <code class="docutils literal notranslate"><span class="pre">config_dict['handlers']['myhandler']['mykey']['123']</span></code> if that
  810. fails.</p>
  811. </section>
  812. <section id="import-resolution-and-custom-importers">
  813. <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>
  814. <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> function
  815. to do its importing. You may want to replace this with your own importing
  816. mechanism: if so, you can replace the <code class="xref py py-attr docutils literal notranslate"><span class="pre">importer</span></code> attribute of the
  817. <code class="xref py py-class docutils literal notranslate"><span class="pre">DictConfigurator</span></code> or its superclass, the
  818. <code class="xref py py-class docutils literal notranslate"><span class="pre">BaseConfigurator</span></code> class. However, you need to be
  819. careful because of the way functions are accessed from classes via
  820. descriptors. If you are using a Python callable to do your imports, and you
  821. want to define it at class level rather than instance level, you need to wrap
  822. it 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>
  823. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">importlib</span> <span class="kn">import</span> <span class="n">import_module</span>
  824. <span class="kn">from</span> <span class="nn">logging.config</span> <span class="kn">import</span> <span class="n">BaseConfigurator</span>
  825. <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>
  826. </pre></div>
  827. </div>
  828. <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 import
  829. callable on a configurator <em>instance</em>.</p>
  830. </section>
  831. <section id="configuring-queuehandler-and-queuelistener">
  832. <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>
  833. <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 this
  834. is 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>, you
  835. can configure both together. After the configuration, the <code class="docutils literal notranslate"><span class="pre">QueueListener</span></code> instance
  836. will 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 of
  837. the created handler, and that in turn will be available to you using
  838. <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 the
  839. <code class="docutils literal notranslate"><span class="pre">QueueHandler</span></code> in your configuration. The dictionary schema for configuring the pair
  840. is shown in the example YAML snippet below.</p>
  841. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">handlers</span><span class="p">:</span>
  842. <span class="w"> </span><span class="nt">qhand</span><span class="p">:</span>
  843. <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>
  844. <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>
  845. <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>
  846. <span class="w"> </span><span class="nt">handlers</span><span class="p">:</span>
  847. <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>
  848. <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>
  849. <span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">...</span>
  850. </pre></div>
  851. </div>
  852. <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>
  853. <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>
  854. <ul class="simple">
  855. <li><p>An 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. This is of course
  856. only possible if you are constructing or modifying the configuration dictionary in
  857. code.</p></li>
  858. <li><p>A string that resolves to a callable which, when called with no arguments, returns
  859. the <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 to use. That callable could be a
  860. <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> subclass or a function which returns a suitable queue instance,
  861. such as <code class="docutils literal notranslate"><span class="pre">my.module.queue_factory()</span></code>.</p></li>
  862. <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 in
  863. <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 a
  864. <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>
  865. </ul>
  866. <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 is
  867. created and used.</p>
  868. <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>
  869. <ul class="simple">
  870. <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 only
  871. possible if you are constructing or modifying the configuration dictionary in
  872. code.</p></li>
  873. <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 as
  874. <code class="docutils literal notranslate"><span class="pre">'my.package.CustomListener'</span></code>.</p></li>
  875. <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 in
  876. <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 a
  877. callable with the same signature as the <code class="docutils literal notranslate"><span class="pre">QueueListener</span></code> initializer.</p></li>
  878. </ul>
  879. <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>
  880. <p>The values under the <code class="docutils literal notranslate"><span class="pre">handlers</span></code> key are the names of other handlers in the
  881. configuration (not shown in the above snippet) which will be passed to the queue
  882. listener.</p>
  883. <p>Any custom queue handler and listener classes will need to be defined with the same
  884. initialization 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> and
  885. <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>
  886. <div class="versionadded">
  887. <p><span class="versionmodified added">New in version 3.12.</span></p>
  888. </div>
  889. </section>
  890. </section>
  891. <section id="configuration-file-format">
  892. <span id="logging-config-fileformat"></span><h2>Configuration file format<a class="headerlink" href="#configuration-file-format" title="Link to this heading">¶</a></h2>
  893. <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 on
  894. <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 called
  895. <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 the
  896. entities of each type which are defined in the file. For each such entity, there
  897. is a separate section which identifies how that entity is configured. Thus, for
  898. a 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 relevant
  899. configuration details are held in a section <code class="docutils literal notranslate"><span class="pre">[logger_log01]</span></code>. Similarly, a
  900. handler 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 its
  901. configuration held in a section called <code class="docutils literal notranslate"><span class="pre">[handler_hand01]</span></code>, while a formatter
  902. called <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 configuration
  903. specified in a section called <code class="docutils literal notranslate"><span class="pre">[formatter_form01]</span></code>. The root logger
  904. configuration must be specified in a section called <code class="docutils literal notranslate"><span class="pre">[logger_root]</span></code>.</p>
  905. <div class="admonition note">
  906. <p class="admonition-title">Note</p>
  907. <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 does
  908. not provide functionality to cover certain aspects of logging. For example,
  909. you 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 for
  910. filtering of messages beyond simple integer levels, using <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>.
  911. If you need to have instances of <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> in your logging
  912. configuration, you will need to 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>. Note that future
  913. enhancements to configuration functionality will be added to
  914. <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>, so it’s worth considering transitioning to this newer
  915. API when it’s convenient to do so.</p>
  916. </div>
  917. <p>Examples of these sections in the file are given below.</p>
  918. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[loggers]</span>
  919. <span class="na">keys</span><span class="o">=</span><span class="s">root,log02,log03,log04,log05,log06,log07</span>
  920. <span class="k">[handlers]</span>
  921. <span class="na">keys</span><span class="o">=</span><span class="s">hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09</span>
  922. <span class="k">[formatters]</span>
  923. <span class="na">keys</span><span class="o">=</span><span class="s">form01,form02,form03,form04,form05,form06,form07,form08,form09</span>
  924. </pre></div>
  925. </div>
  926. <p>The root logger must specify a level and a list of handlers. An example of a
  927. root logger section is given below.</p>
  928. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[logger_root]</span>
  929. <span class="na">level</span><span class="o">=</span><span class="s">NOTSET</span>
  930. <span class="na">handlers</span><span class="o">=</span><span class="s">hand01</span>
  931. </pre></div>
  932. </div>
  933. <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> or
  934. <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 be
  935. logged. 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>
  936. package’s namespace.</p>
  937. <p>The <code class="docutils literal notranslate"><span class="pre">handlers</span></code> entry is a comma-separated list of handler names, which must
  938. appear in the <code class="docutils literal notranslate"><span class="pre">[handlers]</span></code> section. These names must appear in the
  939. <code class="docutils literal notranslate"><span class="pre">[handlers]</span></code> section and have corresponding sections in the configuration
  940. file.</p>
  941. <p>For loggers other than the root logger, some additional information is required.
  942. This is illustrated by the following example.</p>
  943. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[logger_parser]</span>
  944. <span class="na">level</span><span class="o">=</span><span class="s">DEBUG</span>
  945. <span class="na">handlers</span><span class="o">=</span><span class="s">hand01</span>
  946. <span class="na">propagate</span><span class="o">=</span><span class="s">1</span>
  947. <span class="na">qualname</span><span class="o">=</span><span class="s">compiler.parser</span>
  948. </pre></div>
  949. </div>
  950. <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,
  951. except that if a non-root logger’s level is specified as <code class="docutils literal notranslate"><span class="pre">NOTSET</span></code>, the system
  952. consults loggers higher up the hierarchy to determine the effective level of the
  953. logger. The <code class="docutils literal notranslate"><span class="pre">propagate</span></code> entry is set to 1 to indicate that messages must
  954. propagate to handlers higher up the logger hierarchy from this logger, or 0 to
  955. indicate that messages are <strong>not</strong> propagated to handlers up the hierarchy. The
  956. <code class="docutils literal notranslate"><span class="pre">qualname</span></code> entry is the hierarchical channel name of the logger, that is to
  957. say the name used by the application to get the logger.</p>
  958. <p>Sections which specify handler configuration are exemplified by the following.</p>
  959. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[handler_hand01]</span>
  960. <span class="na">class</span><span class="o">=</span><span class="s">StreamHandler</span>
  961. <span class="na">level</span><span class="o">=</span><span class="s">NOTSET</span>
  962. <span class="na">formatter</span><span class="o">=</span><span class="s">form01</span>
  963. <span class="na">args</span><span class="o">=</span><span class="s">(sys.stdout,)</span>
  964. </pre></div>
  965. </div>
  966. <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>
  967. in 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 for
  968. loggers, and <code class="docutils literal notranslate"><span class="pre">NOTSET</span></code> is taken to mean ‘log everything’.</p>
  969. <p>The <code class="docutils literal notranslate"><span class="pre">formatter</span></code> entry indicates the key name of the formatter for this
  970. handler. If blank, a default formatter (<code class="docutils literal notranslate"><span class="pre">logging._defaultFormatter</span></code>) is used.
  971. If a name is specified, it must appear in the <code class="docutils literal notranslate"><span class="pre">[formatters]</span></code> section and have
  972. a corresponding section in the configuration file.</p>
  973. <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>
  974. package’s namespace, is the list of arguments to the constructor for the handler
  975. class. Refer to the constructors for the relevant handlers, or to the examples
  976. below, to see how typical entries are constructed. If not provided, it defaults
  977. to <code class="docutils literal notranslate"><span class="pre">()</span></code>.</p>
  978. <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 the
  979. <code class="docutils literal notranslate"><span class="pre">logging</span></code> package’s namespace, is the keyword argument dict to the constructor
  980. for the handler class. If not provided, it defaults to <code class="docutils literal notranslate"><span class="pre">{}</span></code>.</p>
  981. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[handler_hand02]</span>
  982. <span class="na">class</span><span class="o">=</span><span class="s">FileHandler</span>
  983. <span class="na">level</span><span class="o">=</span><span class="s">DEBUG</span>
  984. <span class="na">formatter</span><span class="o">=</span><span class="s">form02</span>
  985. <span class="na">args</span><span class="o">=</span><span class="s">(&#39;python.log&#39;, &#39;w&#39;)</span>
  986. <span class="k">[handler_hand03]</span>
  987. <span class="na">class</span><span class="o">=</span><span class="s">handlers.SocketHandler</span>
  988. <span class="na">level</span><span class="o">=</span><span class="s">INFO</span>
  989. <span class="na">formatter</span><span class="o">=</span><span class="s">form03</span>
  990. <span class="na">args</span><span class="o">=</span><span class="s">(&#39;localhost&#39;, handlers.DEFAULT_TCP_LOGGING_PORT)</span>
  991. <span class="k">[handler_hand04]</span>
  992. <span class="na">class</span><span class="o">=</span><span class="s">handlers.DatagramHandler</span>
  993. <span class="na">level</span><span class="o">=</span><span class="s">WARN</span>
  994. <span class="na">formatter</span><span class="o">=</span><span class="s">form04</span>
  995. <span class="na">args</span><span class="o">=</span><span class="s">(&#39;localhost&#39;, handlers.DEFAULT_UDP_LOGGING_PORT)</span>
  996. <span class="k">[handler_hand05]</span>
  997. <span class="na">class</span><span class="o">=</span><span class="s">handlers.SysLogHandler</span>
  998. <span class="na">level</span><span class="o">=</span><span class="s">ERROR</span>
  999. <span class="na">formatter</span><span class="o">=</span><span class="s">form05</span>
  1000. <span class="na">args</span><span class="o">=</span><span class="s">((&#39;localhost&#39;, handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)</span>
  1001. <span class="k">[handler_hand06]</span>
  1002. <span class="na">class</span><span class="o">=</span><span class="s">handlers.NTEventLogHandler</span>
  1003. <span class="na">level</span><span class="o">=</span><span class="s">CRITICAL</span>
  1004. <span class="na">formatter</span><span class="o">=</span><span class="s">form06</span>
  1005. <span class="na">args</span><span class="o">=</span><span class="s">(&#39;Python Application&#39;, &#39;&#39;, &#39;Application&#39;)</span>
  1006. <span class="k">[handler_hand07]</span>
  1007. <span class="na">class</span><span class="o">=</span><span class="s">handlers.SMTPHandler</span>
  1008. <span class="na">level</span><span class="o">=</span><span class="s">WARN</span>
  1009. <span class="na">formatter</span><span class="o">=</span><span class="s">form07</span>
  1010. <span class="na">args</span><span class="o">=</span><span class="s">(&#39;localhost&#39;, &#39;from@abc&#39;, [&#39;user1@abc&#39;, &#39;user2@xyz&#39;], &#39;Logger Subject&#39;)</span>
  1011. <span class="na">kwargs</span><span class="o">=</span><span class="s">{&#39;timeout&#39;: 10.0}</span>
  1012. <span class="k">[handler_hand08]</span>
  1013. <span class="na">class</span><span class="o">=</span><span class="s">handlers.MemoryHandler</span>
  1014. <span class="na">level</span><span class="o">=</span><span class="s">NOTSET</span>
  1015. <span class="na">formatter</span><span class="o">=</span><span class="s">form08</span>
  1016. <span class="na">target</span><span class="o">=</span>
  1017. <span class="na">args</span><span class="o">=</span><span class="s">(10, ERROR)</span>
  1018. <span class="k">[handler_hand09]</span>
  1019. <span class="na">class</span><span class="o">=</span><span class="s">handlers.HTTPHandler</span>
  1020. <span class="na">level</span><span class="o">=</span><span class="s">NOTSET</span>
  1021. <span class="na">formatter</span><span class="o">=</span><span class="s">form09</span>
  1022. <span class="na">args</span><span class="o">=</span><span class="s">(&#39;localhost:9022&#39;, &#39;/log&#39;, &#39;GET&#39;)</span>
  1023. <span class="na">kwargs</span><span class="o">=</span><span class="s">{&#39;secure&#39;: True}</span>
  1024. </pre></div>
  1025. </div>
  1026. <p>Sections which specify formatter configuration are typified by the following.</p>
  1027. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[formatter_form01]</span>
  1028. <span class="na">format</span><span class="o">=</span><span class="s">F1 %(asctime)s %(levelname)s %(message)s %(customfield)s</span>
  1029. <span class="na">datefmt</span><span class="o">=</span>
  1030. <span class="na">style</span><span class="o">=</span><span class="s">%</span>
  1031. <span class="na">validate</span><span class="o">=</span><span class="s">True</span>
  1032. <span class="na">defaults</span><span class="o">=</span><span class="s">{&#39;customfield&#39;: &#39;defaultvalue&#39;}</span>
  1033. <span class="na">class</span><span class="o">=</span><span class="s">logging.Formatter</span>
  1034. </pre></div>
  1035. </div>
  1036. <p>The arguments for the formatter configuration are the same as the keys
  1037. in the dictionary schema <a class="reference internal" href="#logging-config-dictschema-formatters"><span class="std std-ref">formatters section</span></a>.</p>
  1038. <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 of
  1039. the <code class="docutils literal notranslate"><span class="pre">logging</span></code> package’s namespace, is a dictionary of default values for
  1040. custom formatting fields. If not provided, it defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1041. <div class="admonition note">
  1042. <p class="admonition-title">Note</p>
  1043. <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 are
  1044. potential 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 send
  1045. and receive configurations via sockets. The risks are limited to where
  1046. multiple users with no mutual trust run code on the same machine; see the
  1047. <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> documentation for more information.</p>
  1048. </div>
  1049. <div class="admonition seealso">
  1050. <p class="admonition-title">See also</p>
  1051. <dl class="simple">
  1052. <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>
  1053. </dd>
  1054. <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>
  1055. </dd>
  1056. </dl>
  1057. </div>
  1058. </section>
  1059. </section>
  1060. <div class="clearer"></div>
  1061. </div>
  1062. </div>
  1063. </div>
  1064. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1065. <div class="sphinxsidebarwrapper">
  1066. <div>
  1067. <h3><a href="../contents.html">Table of Contents</a></h3>
  1068. <ul>
  1069. <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>
  1070. <li><a class="reference internal" href="#configuration-functions">Configuration functions</a></li>
  1071. <li><a class="reference internal" href="#security-considerations">Security considerations</a></li>
  1072. <li><a class="reference internal" href="#configuration-dictionary-schema">Configuration dictionary schema</a><ul>
  1073. <li><a class="reference internal" href="#dictionary-schema-details">Dictionary Schema Details</a></li>
  1074. <li><a class="reference internal" href="#incremental-configuration">Incremental Configuration</a></li>
  1075. <li><a class="reference internal" href="#object-connections">Object connections</a></li>
  1076. <li><a class="reference internal" href="#user-defined-objects">User-defined objects</a></li>
  1077. <li><a class="reference internal" href="#handler-configuration-order">Handler configuration order</a></li>
  1078. <li><a class="reference internal" href="#access-to-external-objects">Access to external objects</a></li>
  1079. <li><a class="reference internal" href="#access-to-internal-objects">Access to internal objects</a></li>
  1080. <li><a class="reference internal" href="#import-resolution-and-custom-importers">Import resolution and custom importers</a></li>
  1081. <li><a class="reference internal" href="#configuring-queuehandler-and-queuelistener">Configuring QueueHandler and QueueListener</a></li>
  1082. </ul>
  1083. </li>
  1084. <li><a class="reference internal" href="#configuration-file-format">Configuration file format</a></li>
  1085. </ul>
  1086. </li>
  1087. </ul>
  1088. </div>
  1089. <div>
  1090. <h4>Previous topic</h4>
  1091. <p class="topless"><a href="logging.html"
  1092. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></p>
  1093. </div>
  1094. <div>
  1095. <h4>Next topic</h4>
  1096. <p class="topless"><a href="logging.handlers.html"
  1097. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.handlers</span></code> — Logging handlers</a></p>
  1098. </div>
  1099. <div role="note" aria-label="source link">
  1100. <h3>This Page</h3>
  1101. <ul class="this-page-menu">
  1102. <li><a href="../bugs.html">Report a Bug</a></li>
  1103. <li>
  1104. <a href="https://github.com/python/cpython/blob/main/Doc/library/logging.config.rst"
  1105. rel="nofollow">Show Source
  1106. </a>
  1107. </li>
  1108. </ul>
  1109. </div>
  1110. </div>
  1111. <div id="sidebarbutton" title="Collapse sidebar">
  1112. <span>«</span>
  1113. </div>
  1114. </div>
  1115. <div class="clearer"></div>
  1116. </div>
  1117. <div class="related" role="navigation" aria-label="related navigation">
  1118. <h3>Navigation</h3>
  1119. <ul>
  1120. <li class="right" style="margin-right: 10px">
  1121. <a href="../genindex.html" title="General Index"
  1122. >index</a></li>
  1123. <li class="right" >
  1124. <a href="../py-modindex.html" title="Python Module Index"
  1125. >modules</a> |</li>
  1126. <li class="right" >
  1127. <a href="logging.handlers.html" title="logging.handlers — Logging handlers"
  1128. >next</a> |</li>
  1129. <li class="right" >
  1130. <a href="logging.html" title="logging — Logging facility for Python"
  1131. >previous</a> |</li>
  1132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1134. <li class="switchers">
  1135. <div class="language_switcher_placeholder"></div>
  1136. <div class="version_switcher_placeholder"></div>
  1137. </li>
  1138. <li>
  1139. </li>
  1140. <li id="cpython-language-and-version">
  1141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1142. </li>
  1143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1144. <li class="nav-item nav-item-2"><a href="allos.html" >Generic Operating System Services</a> &#187;</li>
  1145. <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>
  1146. <li class="right">
  1147. <div class="inline-search" role="search">
  1148. <form class="inline-search" action="../search.html" method="get">
  1149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1150. <input type="submit" value="Go" />
  1151. </form>
  1152. </div>
  1153. |
  1154. </li>
  1155. <li class="right">
  1156. <label class="theme-selector-label">
  1157. Theme
  1158. <select class="theme-selector" oninput="activateTheme(this.value)">
  1159. <option value="auto" selected>Auto</option>
  1160. <option value="light">Light</option>
  1161. <option value="dark">Dark</option>
  1162. </select>
  1163. </label> |</li>
  1164. </ul>
  1165. </div>
  1166. <div class="footer">
  1167. &copy;
  1168. <a href="../copyright.html">
  1169. Copyright
  1170. </a>
  1171. 2001-2024, Python Software Foundation.
  1172. <br />
  1173. This page is licensed under the Python Software Foundation License Version 2.
  1174. <br />
  1175. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1176. <br />
  1177. See <a href="/license.html">History and License</a> for more information.<br />
  1178. <br />
  1179. The Python Software Foundation is a non-profit corporation.
  1180. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1181. <br />
  1182. <br />
  1183. Last updated on Apr 09, 2024 (13:47 UTC).
  1184. <a href="/bugs.html">Found a bug</a>?
  1185. <br />
  1186. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1187. </div>
  1188. </body>
  1189. </html>
上海开阖软件有限公司 沪ICP备12045867号-1