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.

2068 lines
185KB

  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 — Logging facility for Python" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/logging.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/logging/__init__.py Important: This page contains the API reference information. For tutorial information and discussion of more advanced topics, see Basic Tutorial, Advanced Tutor..." />
  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/__init__.py Important: This page contains the API reference information. For tutorial information and discussion of more advanced topics, see Basic Tutorial, Advanced Tutor..." />
  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 — Logging facility for Python &#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.config — Logging configuration" href="logging.config.html" />
  33. <link rel="prev" title="getopt — C-style parser for command line options" href="getopt.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/logging.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</span></code> — Logging facility for Python</a><ul>
  86. <li><a class="reference internal" href="#logger-objects">Logger Objects</a></li>
  87. <li><a class="reference internal" href="#logging-levels">Logging Levels</a></li>
  88. <li><a class="reference internal" href="#handler-objects">Handler Objects</a></li>
  89. <li><a class="reference internal" href="#formatter-objects">Formatter Objects</a></li>
  90. <li><a class="reference internal" href="#filter-objects">Filter Objects</a></li>
  91. <li><a class="reference internal" href="#logrecord-objects">LogRecord Objects</a></li>
  92. <li><a class="reference internal" href="#logrecord-attributes">LogRecord attributes</a></li>
  93. <li><a class="reference internal" href="#loggeradapter-objects">LoggerAdapter Objects</a></li>
  94. <li><a class="reference internal" href="#thread-safety">Thread Safety</a></li>
  95. <li><a class="reference internal" href="#module-level-functions">Module-Level Functions</a></li>
  96. <li><a class="reference internal" href="#module-level-attributes">Module-Level Attributes</a></li>
  97. <li><a class="reference internal" href="#integration-with-the-warnings-module">Integration with the warnings module</a></li>
  98. </ul>
  99. </li>
  100. </ul>
  101. </div>
  102. <div>
  103. <h4>Previous topic</h4>
  104. <p class="topless"><a href="getopt.html"
  105. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">getopt</span></code> — C-style parser for command line options</a></p>
  106. </div>
  107. <div>
  108. <h4>Next topic</h4>
  109. <p class="topless"><a href="logging.config.html"
  110. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code> — Logging configuration</a></p>
  111. </div>
  112. <div role="note" aria-label="source link">
  113. <h3>This Page</h3>
  114. <ul class="this-page-menu">
  115. <li><a href="../bugs.html">Report a Bug</a></li>
  116. <li>
  117. <a href="https://github.com/python/cpython/blob/main/Doc/library/logging.rst"
  118. rel="nofollow">Show Source
  119. </a>
  120. </li>
  121. </ul>
  122. </div>
  123. </nav>
  124. </div>
  125. </div>
  126. <div class="related" role="navigation" aria-label="related navigation">
  127. <h3>Navigation</h3>
  128. <ul>
  129. <li class="right" style="margin-right: 10px">
  130. <a href="../genindex.html" title="General Index"
  131. accesskey="I">index</a></li>
  132. <li class="right" >
  133. <a href="../py-modindex.html" title="Python Module Index"
  134. >modules</a> |</li>
  135. <li class="right" >
  136. <a href="logging.config.html" title="logging.config — Logging configuration"
  137. accesskey="N">next</a> |</li>
  138. <li class="right" >
  139. <a href="getopt.html" title="getopt — C-style parser for command line options"
  140. accesskey="P">previous</a> |</li>
  141. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  142. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  143. <li class="switchers">
  144. <div class="language_switcher_placeholder"></div>
  145. <div class="version_switcher_placeholder"></div>
  146. </li>
  147. <li>
  148. </li>
  149. <li id="cpython-language-and-version">
  150. <a href="../index.html">3.12.3 Documentation</a> &#187;
  151. </li>
  152. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  153. <li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">Generic Operating System Services</a> &#187;</li>
  154. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></li>
  155. <li class="right">
  156. <div class="inline-search" role="search">
  157. <form class="inline-search" action="../search.html" method="get">
  158. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  159. <input type="submit" value="Go" />
  160. </form>
  161. </div>
  162. |
  163. </li>
  164. <li class="right">
  165. <label class="theme-selector-label">
  166. Theme
  167. <select class="theme-selector" oninput="activateTheme(this.value)">
  168. <option value="auto" selected>Auto</option>
  169. <option value="light">Light</option>
  170. <option value="dark">Dark</option>
  171. </select>
  172. </label> |</li>
  173. </ul>
  174. </div>
  175. <div class="document">
  176. <div class="documentwrapper">
  177. <div class="bodywrapper">
  178. <div class="body" role="main">
  179. <section id="module-logging">
  180. <span id="logging-logging-facility-for-python"></span><h1><a class="reference internal" href="#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> — Logging facility for Python<a class="headerlink" href="#module-logging" title="Link to this heading">¶</a></h1>
  181. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/logging/__init__.py">Lib/logging/__init__.py</a></p>
  182. <aside class="sidebar" id="index-0">
  183. <p class="sidebar-title">Important</p>
  184. <p>This page contains the API reference information. For tutorial
  185. information and discussion of more advanced topics, see</p>
  186. <ul class="simple">
  187. <li><p><a class="reference internal" href="../howto/logging.html#logging-basic-tutorial"><span class="std std-ref">Basic Tutorial</span></a></p></li>
  188. <li><p><a class="reference internal" href="../howto/logging.html#logging-advanced-tutorial"><span class="std std-ref">Advanced Tutorial</span></a></p></li>
  189. <li><p><a class="reference internal" href="../howto/logging-cookbook.html#logging-cookbook"><span class="std std-ref">Logging Cookbook</span></a></p></li>
  190. </ul>
  191. </aside>
  192. <hr class="docutils" />
  193. <p>This module defines functions and classes which implement a flexible event
  194. logging system for applications and libraries.</p>
  195. <p>The key benefit of having the logging API provided by a standard library module
  196. is that all Python modules can participate in logging, so your application log
  197. can include your own messages integrated with messages from third-party
  198. modules.</p>
  199. <p>Here’s a simple example of idiomatic usage:</p>
  200. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># myapp.py</span>
  201. <span class="kn">import</span> <span class="nn">logging</span>
  202. <span class="kn">import</span> <span class="nn">mylib</span>
  203. <span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
  204. <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  205. <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s1">&#39;myapp.log&#39;</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
  206. <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Started&#39;</span><span class="p">)</span>
  207. <span class="n">mylib</span><span class="o">.</span><span class="n">do_something</span><span class="p">()</span>
  208. <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Finished&#39;</span><span class="p">)</span>
  209. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  210. <span class="n">main</span><span class="p">()</span>
  211. </pre></div>
  212. </div>
  213. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># mylib.py</span>
  214. <span class="kn">import</span> <span class="nn">logging</span>
  215. <span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
  216. <span class="k">def</span> <span class="nf">do_something</span><span class="p">():</span>
  217. <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Doing something&#39;</span><span class="p">)</span>
  218. </pre></div>
  219. </div>
  220. <p>If you run <em>myapp.py</em>, you should see this in <em>myapp.log</em>:</p>
  221. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>INFO:__main__:Started
  222. INFO:mylib:Doing something
  223. INFO:__main__:Finished
  224. </pre></div>
  225. </div>
  226. <p>The key feature of this idiomatic usage is that the majority of code is simply
  227. creating a module level logger with <code class="docutils literal notranslate"><span class="pre">getLogger(__name__)</span></code>, and using that
  228. logger to do any needed logging. This is concise, while allowing downstream
  229. code fine-grained control if needed. Logged messages to the module-level logger
  230. get forwarded to handlers of loggers in higher-level modules, all the way up to
  231. the highest-level logger known as the root logger; this approach is known as
  232. hierarchical logging.</p>
  233. <p>For logging to be useful, it needs to be configured: setting the levels and
  234. destinations for each logger, potentially changing how specific modules log,
  235. often based on command-line arguments or application configuration. In most
  236. cases, like the one above, only the root logger needs to be so configured, since
  237. all the lower level loggers at module level eventually forward their messages to
  238. its handlers. <a class="reference internal" href="#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> provides a quick way to configure
  239. the root logger that handles many use cases.</p>
  240. <p>The module provides a lot of functionality and flexibility. If you are
  241. unfamiliar with logging, the best way to get to grips with it is to view the
  242. tutorials (<strong>see the links above and on the right</strong>).</p>
  243. <p>The basic classes defined by the module, together with their attributes and
  244. methods, are listed in the sections below.</p>
  245. <ul class="simple">
  246. <li><p>Loggers expose the interface that application code directly uses.</p></li>
  247. <li><p>Handlers send the log records (created by loggers) to the appropriate
  248. destination.</p></li>
  249. <li><p>Filters provide a finer grained facility for determining which log records
  250. to output.</p></li>
  251. <li><p>Formatters specify the layout of log records in the final output.</p></li>
  252. </ul>
  253. <section id="logger-objects">
  254. <span id="logger"></span><h2>Logger Objects<a class="headerlink" href="#logger-objects" title="Link to this heading">¶</a></h2>
  255. <p>Loggers have the following attributes and methods. Note that Loggers should
  256. <em>NEVER</em> be instantiated directly, but always through the module-level function
  257. <code class="docutils literal notranslate"><span class="pre">logging.getLogger(name)</span></code>. Multiple calls to <a class="reference internal" href="#logging.getLogger" title="logging.getLogger"><code class="xref py py-func docutils literal notranslate"><span class="pre">getLogger()</span></code></a> with the same
  258. name will always return a reference to the same Logger object.</p>
  259. <p>The <code class="docutils literal notranslate"><span class="pre">name</span></code> is potentially a period-separated hierarchical value, like
  260. <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code> (though it could also be just plain <code class="docutils literal notranslate"><span class="pre">foo</span></code>, for example).
  261. Loggers that are further down in the hierarchical list are children of loggers
  262. higher up in the list. For example, given a logger with a name of <code class="docutils literal notranslate"><span class="pre">foo</span></code>,
  263. loggers with names of <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code>, <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>, and <code class="docutils literal notranslate"><span class="pre">foo.bam</span></code> are all
  264. descendants of <code class="docutils literal notranslate"><span class="pre">foo</span></code>. The logger name hierarchy is analogous to the Python
  265. package hierarchy, and identical to it if you organise your loggers on a
  266. per-module basis using the recommended construction
  267. <code class="docutils literal notranslate"><span class="pre">logging.getLogger(__name__)</span></code>. That’s because in a module, <code class="docutils literal notranslate"><span class="pre">__name__</span></code>
  268. is the module’s name in the Python package namespace.</p>
  269. <dl class="py class">
  270. <dt class="sig sig-object py" id="logging.Logger">
  271. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">Logger</span></span><a class="headerlink" href="#logging.Logger" title="Link to this definition">¶</a></dt>
  272. <dd><dl class="py attribute">
  273. <dt class="sig sig-object py" id="logging.Logger.name">
  274. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#logging.Logger.name" title="Link to this definition">¶</a></dt>
  275. <dd><p>This is the logger’s name, and is the value that was passed to <a class="reference internal" href="#logging.getLogger" title="logging.getLogger"><code class="xref py py-func docutils literal notranslate"><span class="pre">getLogger()</span></code></a>
  276. to obtain the logger.</p>
  277. <div class="admonition note">
  278. <p class="admonition-title">Note</p>
  279. <p>This attribute should be treated as read-only.</p>
  280. </div>
  281. </dd></dl>
  282. <dl class="py attribute">
  283. <dt class="sig sig-object py" id="logging.Logger.level">
  284. <span class="sig-name descname"><span class="pre">level</span></span><a class="headerlink" href="#logging.Logger.level" title="Link to this definition">¶</a></dt>
  285. <dd><p>The threshold of this logger, as set by the <a class="reference internal" href="#logging.Logger.setLevel" title="logging.Logger.setLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setLevel()</span></code></a> method.</p>
  286. <div class="admonition note">
  287. <p class="admonition-title">Note</p>
  288. <p>Do not set this attribute directly - always use <a class="reference internal" href="#logging.Logger.setLevel" title="logging.Logger.setLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setLevel()</span></code></a>,
  289. which has checks for the level passed to it.</p>
  290. </div>
  291. </dd></dl>
  292. <dl class="py attribute">
  293. <dt class="sig sig-object py" id="logging.Logger.parent">
  294. <span class="sig-name descname"><span class="pre">parent</span></span><a class="headerlink" href="#logging.Logger.parent" title="Link to this definition">¶</a></dt>
  295. <dd><p>The parent logger of this logger. It may change based on later instantiation
  296. of loggers which are higher up in the namespace hierarchy.</p>
  297. <div class="admonition note">
  298. <p class="admonition-title">Note</p>
  299. <p>This value should be treated as read-only.</p>
  300. </div>
  301. </dd></dl>
  302. <dl class="py attribute">
  303. <dt class="sig sig-object py" id="logging.Logger.propagate">
  304. <span class="sig-name descname"><span class="pre">propagate</span></span><a class="headerlink" href="#logging.Logger.propagate" title="Link to this definition">¶</a></dt>
  305. <dd><p>If this attribute evaluates to true, events logged to this logger will be
  306. passed to the handlers of higher level (ancestor) loggers, in addition to
  307. any handlers attached to this logger. Messages are passed directly to the
  308. ancestor loggers’ handlers - neither the level nor filters of the ancestor
  309. loggers in question are considered.</p>
  310. <p>If this evaluates to false, logging messages are not passed to the handlers
  311. of ancestor loggers.</p>
  312. <p>Spelling it out with an example: If the propagate attribute of the logger named
  313. <code class="docutils literal notranslate"><span class="pre">A.B.C</span></code> evaluates to true, any event logged to <code class="docutils literal notranslate"><span class="pre">A.B.C</span></code> via a method call such as
  314. <code class="docutils literal notranslate"><span class="pre">logging.getLogger('A.B.C').error(...)</span></code> will [subject to passing that logger’s
  315. level and filter settings] be passed in turn to any handlers attached to loggers
  316. named <code class="docutils literal notranslate"><span class="pre">A.B</span></code>, <code class="docutils literal notranslate"><span class="pre">A</span></code> and the root logger, after first being passed to any handlers
  317. attached to <code class="docutils literal notranslate"><span class="pre">A.B.C</span></code>. If any logger in the chain <code class="docutils literal notranslate"><span class="pre">A.B.C</span></code>, <code class="docutils literal notranslate"><span class="pre">A.B</span></code>, <code class="docutils literal notranslate"><span class="pre">A</span></code> has its
  318. <code class="docutils literal notranslate"><span class="pre">propagate</span></code> attribute set to false, then that is the last logger whose handlers
  319. are offered the event to handle, and propagation stops at that point.</p>
  320. <p>The constructor sets this attribute to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  321. <div class="admonition note">
  322. <p class="admonition-title">Note</p>
  323. <p>If you attach a handler to a logger <em>and</em> one or more of its
  324. ancestors, it may emit the same record multiple times. In general, you
  325. should not need to attach a handler to more than one logger - if you just
  326. attach it to the appropriate logger which is highest in the logger
  327. hierarchy, then it will see all events logged by all descendant loggers,
  328. provided that their propagate setting is left set to <code class="docutils literal notranslate"><span class="pre">True</span></code>. A common
  329. scenario is to attach handlers only to the root logger, and to let
  330. propagation take care of the rest.</p>
  331. </div>
  332. </dd></dl>
  333. <dl class="py attribute">
  334. <dt class="sig sig-object py" id="logging.Logger.handlers">
  335. <span class="sig-name descname"><span class="pre">handlers</span></span><a class="headerlink" href="#logging.Logger.handlers" title="Link to this definition">¶</a></dt>
  336. <dd><p>The list of handlers directly attached to this logger instance.</p>
  337. <div class="admonition note">
  338. <p class="admonition-title">Note</p>
  339. <p>This attribute should be treated as read-only; it is normally changed via
  340. the <a class="reference internal" href="#logging.Logger.addHandler" title="logging.Logger.addHandler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addHandler()</span></code></a> and <a class="reference internal" href="#logging.Logger.removeHandler" title="logging.Logger.removeHandler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">removeHandler()</span></code></a> methods, which use locks to ensure
  341. thread-safe operation.</p>
  342. </div>
  343. </dd></dl>
  344. <dl class="py attribute">
  345. <dt class="sig sig-object py" id="logging.Logger.disabled">
  346. <span class="sig-name descname"><span class="pre">disabled</span></span><a class="headerlink" href="#logging.Logger.disabled" title="Link to this definition">¶</a></dt>
  347. <dd><p>This attribute disables handling of any events. It is set to <code class="docutils literal notranslate"><span class="pre">False</span></code> in the
  348. initializer, and only changed by logging configuration code.</p>
  349. <div class="admonition note">
  350. <p class="admonition-title">Note</p>
  351. <p>This attribute should be treated as read-only.</p>
  352. </div>
  353. </dd></dl>
  354. <dl class="py method">
  355. <dt class="sig sig-object py" id="logging.Logger.setLevel">
  356. <span class="sig-name descname"><span class="pre">setLevel</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.setLevel" title="Link to this definition">¶</a></dt>
  357. <dd><p>Sets the threshold for this logger to <em>level</em>. Logging messages which are less
  358. severe than <em>level</em> will be ignored; logging messages which have severity <em>level</em>
  359. or higher will be emitted by whichever handler or handlers service this logger,
  360. unless a handler’s level has been set to a higher severity level than <em>level</em>.</p>
  361. <p>When a logger is created, the level is set to <a class="reference internal" href="#logging.NOTSET" title="logging.NOTSET"><code class="xref py py-const docutils literal notranslate"><span class="pre">NOTSET</span></code></a> (which causes
  362. all messages to be processed when the logger is the root logger, or delegation
  363. to the parent when the logger is a non-root logger). Note that the root logger
  364. is created with level <a class="reference internal" href="#logging.WARNING" title="logging.WARNING"><code class="xref py py-const docutils literal notranslate"><span class="pre">WARNING</span></code></a>.</p>
  365. <p>The term ‘delegation to the parent’ means that if a logger has a level of
  366. NOTSET, its chain of ancestor loggers is traversed until either an ancestor with
  367. a level other than NOTSET is found, or the root is reached.</p>
  368. <p>If an ancestor is found with a level other than NOTSET, then that ancestor’s
  369. level is treated as the effective level of the logger where the ancestor search
  370. began, and is used to determine how a logging event is handled.</p>
  371. <p>If the root is reached, and it has a level of NOTSET, then all messages will be
  372. processed. Otherwise, the root’s level will be used as the effective level.</p>
  373. <p>See <a class="reference internal" href="#levels"><span class="std std-ref">Logging Levels</span></a> for a list of levels.</p>
  374. <div class="versionchanged">
  375. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>level</em> parameter now accepts a string representation of the
  376. level such as ‘INFO’ as an alternative to the integer constants
  377. such as <a class="reference internal" href="#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">INFO</span></code></a>. Note, however, that levels are internally stored
  378. as integers, and methods such as e.g. <a class="reference internal" href="#logging.Logger.getEffectiveLevel" title="logging.Logger.getEffectiveLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getEffectiveLevel()</span></code></a> and
  379. <a class="reference internal" href="#logging.Logger.isEnabledFor" title="logging.Logger.isEnabledFor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isEnabledFor()</span></code></a> will return/expect to be passed integers.</p>
  380. </div>
  381. </dd></dl>
  382. <dl class="py method">
  383. <dt class="sig sig-object py" id="logging.Logger.isEnabledFor">
  384. <span class="sig-name descname"><span class="pre">isEnabledFor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.isEnabledFor" title="Link to this definition">¶</a></dt>
  385. <dd><p>Indicates if a message of severity <em>level</em> would be processed by this logger.
  386. This method checks first the module-level level set by
  387. <code class="docutils literal notranslate"><span class="pre">logging.disable(level)</span></code> and then the logger’s effective level as determined
  388. by <a class="reference internal" href="#logging.Logger.getEffectiveLevel" title="logging.Logger.getEffectiveLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getEffectiveLevel()</span></code></a>.</p>
  389. </dd></dl>
  390. <dl class="py method">
  391. <dt class="sig sig-object py" id="logging.Logger.getEffectiveLevel">
  392. <span class="sig-name descname"><span class="pre">getEffectiveLevel</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.getEffectiveLevel" title="Link to this definition">¶</a></dt>
  393. <dd><p>Indicates the effective level for this logger. If a value other than
  394. <a class="reference internal" href="#logging.NOTSET" title="logging.NOTSET"><code class="xref py py-const docutils literal notranslate"><span class="pre">NOTSET</span></code></a> has been set using <a class="reference internal" href="#logging.Logger.setLevel" title="logging.Logger.setLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setLevel()</span></code></a>, it is returned. Otherwise,
  395. the hierarchy is traversed towards the root until a value other than
  396. <a class="reference internal" href="#logging.NOTSET" title="logging.NOTSET"><code class="xref py py-const docutils literal notranslate"><span class="pre">NOTSET</span></code></a> is found, and that value is returned. The value returned is
  397. an integer, typically one of <a class="reference internal" href="#logging.DEBUG" title="logging.DEBUG"><code class="xref py py-const docutils literal notranslate"><span class="pre">logging.DEBUG</span></code></a>, <a class="reference internal" href="#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">logging.INFO</span></code></a>
  398. etc.</p>
  399. </dd></dl>
  400. <dl class="py method">
  401. <dt class="sig sig-object py" id="logging.Logger.getChild">
  402. <span class="sig-name descname"><span class="pre">getChild</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">suffix</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.getChild" title="Link to this definition">¶</a></dt>
  403. <dd><p>Returns a logger which is a descendant to this logger, as determined by the suffix.
  404. Thus, <code class="docutils literal notranslate"><span class="pre">logging.getLogger('abc').getChild('def.ghi')</span></code> would return the same
  405. logger as would be returned by <code class="docutils literal notranslate"><span class="pre">logging.getLogger('abc.def.ghi')</span></code>. This is a
  406. convenience method, useful when the parent logger is named using e.g. <code class="docutils literal notranslate"><span class="pre">__name__</span></code>
  407. rather than a literal string.</p>
  408. <div class="versionadded">
  409. <p><span class="versionmodified added">New in version 3.2.</span></p>
  410. </div>
  411. </dd></dl>
  412. <dl class="py method">
  413. <dt class="sig sig-object py" id="logging.Logger.getChildren">
  414. <span class="sig-name descname"><span class="pre">getChildren</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.getChildren" title="Link to this definition">¶</a></dt>
  415. <dd><p>Returns a set of loggers which are immediate children of this logger. So for
  416. example <code class="docutils literal notranslate"><span class="pre">logging.getLogger().getChildren()</span></code> might return a set containing
  417. loggers named <code class="docutils literal notranslate"><span class="pre">foo</span></code> and <code class="docutils literal notranslate"><span class="pre">bar</span></code>, but a logger named <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code> wouldn’t be
  418. included in the set. Likewise, <code class="docutils literal notranslate"><span class="pre">logging.getLogger('foo').getChildren()</span></code> might
  419. return a set including a logger named <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code>, but it wouldn’t include one
  420. named <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>.</p>
  421. <div class="versionadded">
  422. <p><span class="versionmodified added">New in version 3.12.</span></p>
  423. </div>
  424. </dd></dl>
  425. <dl class="py method">
  426. <dt class="sig sig-object py" id="logging.Logger.debug">
  427. <span class="sig-name descname"><span class="pre">debug</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.debug" title="Link to this definition">¶</a></dt>
  428. <dd><p>Logs a message with level <a class="reference internal" href="#logging.DEBUG" title="logging.DEBUG"><code class="xref py py-const docutils literal notranslate"><span class="pre">DEBUG</span></code></a> on this logger. The <em>msg</em> is the
  429. message format string, and the <em>args</em> are the arguments which are merged into
  430. <em>msg</em> using the string formatting operator. (Note that this means that you can
  431. use keywords in the format string, together with a single dictionary argument.)
  432. No % formatting operation is performed on <em>msg</em> when no <em>args</em> are supplied.</p>
  433. <p>There are four keyword arguments in <em>kwargs</em> which are inspected:
  434. <em>exc_info</em>, <em>stack_info</em>, <em>stacklevel</em> and <em>extra</em>.</p>
  435. <p>If <em>exc_info</em> does not evaluate as false, it causes exception information to be
  436. added to the logging message. If an exception tuple (in the format returned by
  437. <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>) or an exception instance is provided, it is used;
  438. otherwise, <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a> is called to get the exception information.</p>
  439. <p>The second optional keyword argument is <em>stack_info</em>, which defaults to
  440. <code class="docutils literal notranslate"><span class="pre">False</span></code>. If true, stack information is added to the logging
  441. message, including the actual logging call. Note that this is not the same
  442. stack information as that displayed through specifying <em>exc_info</em>: The
  443. former is stack frames from the bottom of the stack up to the logging call
  444. in the current thread, whereas the latter is information about stack frames
  445. which have been unwound, following an exception, while searching for
  446. exception handlers.</p>
  447. <p>You can specify <em>stack_info</em> independently of <em>exc_info</em>, e.g. to just show
  448. how you got to a certain point in your code, even when no exceptions were
  449. raised. The stack frames are printed following a header line which says:</p>
  450. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Stack (most recent call last):
  451. </pre></div>
  452. </div>
  453. <p>This mimics the <code class="docutils literal notranslate"><span class="pre">Traceback</span> <span class="pre">(most</span> <span class="pre">recent</span> <span class="pre">call</span> <span class="pre">last):</span></code> which is used when
  454. displaying exception frames.</p>
  455. <p>The third optional keyword argument is <em>stacklevel</em>, which defaults to <code class="docutils literal notranslate"><span class="pre">1</span></code>.
  456. If greater than 1, the corresponding number of stack frames are skipped
  457. when computing the line number and function name set in the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a>
  458. created for the logging event. This can be used in logging helpers so that
  459. the function name, filename and line number recorded are not the information
  460. for the helper function/method, but rather its caller. The name of this
  461. parameter mirrors the equivalent one in the <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> module.</p>
  462. <p>The fourth keyword argument is <em>extra</em> which can be used to pass a
  463. dictionary which is used to populate the __dict__ of the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a>
  464. created for the logging event with user-defined attributes. These custom
  465. attributes can then be used as you like. For example, they could be
  466. incorporated into logged messages. For example:</p>
  467. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">FORMAT</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(clientip)-15s</span><span class="s1"> </span><span class="si">%(user)-8s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">&#39;</span>
  468. <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="n">FORMAT</span><span class="p">)</span>
  469. <span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;clientip&#39;</span><span class="p">:</span> <span class="s1">&#39;192.168.0.1&#39;</span><span class="p">,</span> <span class="s1">&#39;user&#39;</span><span class="p">:</span> <span class="s1">&#39;fbloggs&#39;</span><span class="p">}</span>
  470. <span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;tcpserver&#39;</span><span class="p">)</span>
  471. <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;Protocol problem: </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;connection reset&#39;</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="n">d</span><span class="p">)</span>
  472. </pre></div>
  473. </div>
  474. <p>would print something like</p>
  475. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
  476. </pre></div>
  477. </div>
  478. <p>The keys in the dictionary passed in <em>extra</em> should not clash with the keys used
  479. by the logging system. (See the section on <a class="reference internal" href="#logrecord-attributes"><span class="std std-ref">LogRecord attributes</span></a> for more
  480. information on which keys are used by the logging system.)</p>
  481. <p>If you choose to use these attributes in logged messages, you need to exercise
  482. some care. In the above example, for instance, the <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> has been
  483. set up with a format string which expects ‘clientip’ and ‘user’ in the attribute
  484. dictionary of the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a>. If these are missing, the message will
  485. not be logged because a string formatting exception will occur. So in this case,
  486. you always need to pass the <em>extra</em> dictionary with these keys.</p>
  487. <p>While this might be annoying, this feature is intended for use in specialized
  488. circumstances, such as multi-threaded servers where the same code executes in
  489. many contexts, and interesting conditions which arise are dependent on this
  490. context (such as remote client IP address and authenticated user name, in the
  491. above example). In such circumstances, it is likely that specialized
  492. <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a>s would be used with particular <a class="reference internal" href="#logging.Handler" title="logging.Handler"><code class="xref py py-class docutils literal notranslate"><span class="pre">Handler</span></code></a>s.</p>
  493. <p>If no handler is attached to this logger (or any of its ancestors,
  494. taking into account the relevant <a class="reference internal" href="#logging.Logger.propagate" title="logging.Logger.propagate"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Logger.propagate</span></code></a> attributes),
  495. the message will be sent to the handler set on <a class="reference internal" href="#logging.lastResort" title="logging.lastResort"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastResort</span></code></a>.</p>
  496. <div class="versionchanged">
  497. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>stack_info</em> parameter was added.</p>
  498. </div>
  499. <div class="versionchanged">
  500. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <em>exc_info</em> parameter can now accept exception instances.</p>
  501. </div>
  502. <div class="versionchanged">
  503. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>stacklevel</em> parameter was added.</p>
  504. </div>
  505. </dd></dl>
  506. <dl class="py method">
  507. <dt class="sig sig-object py" id="logging.Logger.info">
  508. <span class="sig-name descname"><span class="pre">info</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.info" title="Link to this definition">¶</a></dt>
  509. <dd><p>Logs a message with level <a class="reference internal" href="#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">INFO</span></code></a> on this logger. The arguments are
  510. interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  511. </dd></dl>
  512. <dl class="py method">
  513. <dt class="sig sig-object py" id="logging.Logger.warning">
  514. <span class="sig-name descname"><span class="pre">warning</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.warning" title="Link to this definition">¶</a></dt>
  515. <dd><p>Logs a message with level <a class="reference internal" href="#logging.WARNING" title="logging.WARNING"><code class="xref py py-const docutils literal notranslate"><span class="pre">WARNING</span></code></a> on this logger. The arguments are
  516. interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  517. <div class="admonition note">
  518. <p class="admonition-title">Note</p>
  519. <p>There is an obsolete method <code class="docutils literal notranslate"><span class="pre">warn</span></code> which is functionally
  520. identical to <code class="docutils literal notranslate"><span class="pre">warning</span></code>. As <code class="docutils literal notranslate"><span class="pre">warn</span></code> is deprecated, please do not use
  521. it - use <code class="docutils literal notranslate"><span class="pre">warning</span></code> instead.</p>
  522. </div>
  523. </dd></dl>
  524. <dl class="py method">
  525. <dt class="sig sig-object py" id="logging.Logger.error">
  526. <span class="sig-name descname"><span class="pre">error</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.error" title="Link to this definition">¶</a></dt>
  527. <dd><p>Logs a message with level <a class="reference internal" href="#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ERROR</span></code></a> on this logger. The arguments are
  528. interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  529. </dd></dl>
  530. <dl class="py method">
  531. <dt class="sig sig-object py" id="logging.Logger.critical">
  532. <span class="sig-name descname"><span class="pre">critical</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.critical" title="Link to this definition">¶</a></dt>
  533. <dd><p>Logs a message with level <a class="reference internal" href="#logging.CRITICAL" title="logging.CRITICAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">CRITICAL</span></code></a> on this logger. The arguments are
  534. interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  535. </dd></dl>
  536. <dl class="py method">
  537. <dt class="sig sig-object py" id="logging.Logger.log">
  538. <span class="sig-name descname"><span class="pre">log</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.log" title="Link to this definition">¶</a></dt>
  539. <dd><p>Logs a message with integer level <em>level</em> on this logger. The other arguments are
  540. interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  541. </dd></dl>
  542. <dl class="py method">
  543. <dt class="sig sig-object py" id="logging.Logger.exception">
  544. <span class="sig-name descname"><span class="pre">exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.exception" title="Link to this definition">¶</a></dt>
  545. <dd><p>Logs a message with level <a class="reference internal" href="#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ERROR</span></code></a> on this logger. The arguments are
  546. interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>. Exception info is added to the logging
  547. message. This method should only be called from an exception handler.</p>
  548. </dd></dl>
  549. <dl class="py method">
  550. <dt class="sig sig-object py" id="logging.Logger.addFilter">
  551. <span class="sig-name descname"><span class="pre">addFilter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filter</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.addFilter" title="Link to this definition">¶</a></dt>
  552. <dd><p>Adds the specified filter <em>filter</em> to this logger.</p>
  553. </dd></dl>
  554. <dl class="py method">
  555. <dt class="sig sig-object py" id="logging.Logger.removeFilter">
  556. <span class="sig-name descname"><span class="pre">removeFilter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filter</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.removeFilter" title="Link to this definition">¶</a></dt>
  557. <dd><p>Removes the specified filter <em>filter</em> from this logger.</p>
  558. </dd></dl>
  559. <dl class="py method">
  560. <dt class="sig sig-object py" id="logging.Logger.filter">
  561. <span class="sig-name descname"><span class="pre">filter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.filter" title="Link to this definition">¶</a></dt>
  562. <dd><p>Apply this logger’s filters to the record and return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the
  563. record is to be processed. The filters are consulted in turn, until one of
  564. them returns a false value. If none of them return a false value, the record
  565. will be processed (passed to handlers). If one returns a false value, no
  566. further processing of the record occurs.</p>
  567. </dd></dl>
  568. <dl class="py method">
  569. <dt class="sig sig-object py" id="logging.Logger.addHandler">
  570. <span class="sig-name descname"><span class="pre">addHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hdlr</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.addHandler" title="Link to this definition">¶</a></dt>
  571. <dd><p>Adds the specified handler <em>hdlr</em> to this logger.</p>
  572. </dd></dl>
  573. <dl class="py method">
  574. <dt class="sig sig-object py" id="logging.Logger.removeHandler">
  575. <span class="sig-name descname"><span class="pre">removeHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hdlr</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.removeHandler" title="Link to this definition">¶</a></dt>
  576. <dd><p>Removes the specified handler <em>hdlr</em> from this logger.</p>
  577. </dd></dl>
  578. <dl class="py method">
  579. <dt class="sig sig-object py" id="logging.Logger.findCaller">
  580. <span class="sig-name descname"><span class="pre">findCaller</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stack_info</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stacklevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.findCaller" title="Link to this definition">¶</a></dt>
  581. <dd><p>Finds the caller’s source filename and line number. Returns the filename, line
  582. number, function name and stack information as a 4-element tuple. The stack
  583. information is returned as <code class="docutils literal notranslate"><span class="pre">None</span></code> unless <em>stack_info</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  584. <p>The <em>stacklevel</em> parameter is passed from code calling the <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>
  585. and other APIs. If greater than 1, the excess is used to skip stack frames
  586. before determining the values to be returned. This will generally be useful
  587. when calling logging APIs from helper/wrapper code, so that the information
  588. in the event log refers not to the helper/wrapper code, but to the code that
  589. calls it.</p>
  590. </dd></dl>
  591. <dl class="py method">
  592. <dt class="sig sig-object py" id="logging.Logger.handle">
  593. <span class="sig-name descname"><span class="pre">handle</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.handle" title="Link to this definition">¶</a></dt>
  594. <dd><p>Handles a record by passing it to all handlers associated with this logger and
  595. its ancestors (until a false value of <em>propagate</em> is found). This method is used
  596. for unpickled records received from a socket, as well as those created locally.
  597. Logger-level filtering is applied using <a class="reference internal" href="#logging.Logger.filter" title="logging.Logger.filter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">filter()</span></code></a>.</p>
  598. </dd></dl>
  599. <dl class="py method">
  600. <dt class="sig sig-object py" id="logging.Logger.makeRecord">
  601. <span class="sig-name descname"><span class="pre">makeRecord</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fn</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lno</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_info</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</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">extra</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">sinfo</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.Logger.makeRecord" title="Link to this definition">¶</a></dt>
  602. <dd><p>This is a factory method which can be overridden in subclasses to create
  603. specialized <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> instances.</p>
  604. </dd></dl>
  605. <dl class="py method">
  606. <dt class="sig sig-object py" id="logging.Logger.hasHandlers">
  607. <span class="sig-name descname"><span class="pre">hasHandlers</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.Logger.hasHandlers" title="Link to this definition">¶</a></dt>
  608. <dd><p>Checks to see if this logger has any handlers configured. This is done by
  609. looking for handlers in this logger and its parents in the logger hierarchy.
  610. Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if a handler was found, else <code class="docutils literal notranslate"><span class="pre">False</span></code>. The method stops searching
  611. up the hierarchy whenever a logger with the ‘propagate’ attribute set to
  612. false is found - that will be the last logger which is checked for the
  613. existence of handlers.</p>
  614. <div class="versionadded">
  615. <p><span class="versionmodified added">New in version 3.2.</span></p>
  616. </div>
  617. </dd></dl>
  618. <div class="versionchanged">
  619. <p><span class="versionmodified changed">Changed in version 3.7: </span>Loggers can now be pickled and unpickled.</p>
  620. </div>
  621. </dd></dl>
  622. </section>
  623. <section id="logging-levels">
  624. <span id="levels"></span><h2>Logging Levels<a class="headerlink" href="#logging-levels" title="Link to this heading">¶</a></h2>
  625. <p>The numeric values of logging levels are given in the following table. These are
  626. primarily of interest if you want to define your own levels, and need them to
  627. have specific values relative to the predefined levels. If you define a level
  628. with the same numeric value, it overwrites the predefined value; the predefined
  629. name is lost.</p>
  630. <table class="docutils align-default">
  631. <thead>
  632. <tr class="row-odd"><th class="head"><p>Level</p></th>
  633. <th class="head"><p>Numeric value</p></th>
  634. <th class="head"><p>What it means / When to use it</p></th>
  635. </tr>
  636. </thead>
  637. <tbody>
  638. <tr class="row-even"><td><dl class="py data">
  639. <dt class="sig sig-object py" id="logging.NOTSET">
  640. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">NOTSET</span></span><a class="headerlink" href="#logging.NOTSET" title="Link to this definition">¶</a></dt>
  641. <dd></dd></dl>
  642. </td>
  643. <td><p>0</p></td>
  644. <td><p>When set on a logger, indicates that
  645. ancestor loggers are to be consulted
  646. to determine the effective level.
  647. If that still resolves to
  648. <code class="xref py py-const docutils literal notranslate"><span class="pre">NOTSET</span></code>, then all events
  649. are logged. When set on a handler,
  650. all events are handled.</p></td>
  651. </tr>
  652. <tr class="row-odd"><td><dl class="py data">
  653. <dt class="sig sig-object py" id="logging.DEBUG">
  654. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">DEBUG</span></span><a class="headerlink" href="#logging.DEBUG" title="Link to this definition">¶</a></dt>
  655. <dd></dd></dl>
  656. </td>
  657. <td><p>10</p></td>
  658. <td><p>Detailed information, typically only
  659. of interest to a developer trying to
  660. diagnose a problem.</p></td>
  661. </tr>
  662. <tr class="row-even"><td><dl class="py data">
  663. <dt class="sig sig-object py" id="logging.INFO">
  664. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">INFO</span></span><a class="headerlink" href="#logging.INFO" title="Link to this definition">¶</a></dt>
  665. <dd></dd></dl>
  666. </td>
  667. <td><p>20</p></td>
  668. <td><p>Confirmation that things are working
  669. as expected.</p></td>
  670. </tr>
  671. <tr class="row-odd"><td><dl class="py data">
  672. <dt class="sig sig-object py" id="logging.WARNING">
  673. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">WARNING</span></span><a class="headerlink" href="#logging.WARNING" title="Link to this definition">¶</a></dt>
  674. <dd></dd></dl>
  675. </td>
  676. <td><p>30</p></td>
  677. <td><p>An indication that something
  678. unexpected happened, or that a
  679. problem might occur in the near
  680. future (e.g. ‘disk space low’). The
  681. software is still working as
  682. expected.</p></td>
  683. </tr>
  684. <tr class="row-even"><td><dl class="py data">
  685. <dt class="sig sig-object py" id="logging.ERROR">
  686. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">ERROR</span></span><a class="headerlink" href="#logging.ERROR" title="Link to this definition">¶</a></dt>
  687. <dd></dd></dl>
  688. </td>
  689. <td><p>40</p></td>
  690. <td><p>Due to a more serious problem, the
  691. software has not been able to
  692. perform some function.</p></td>
  693. </tr>
  694. <tr class="row-odd"><td><dl class="py data">
  695. <dt class="sig sig-object py" id="logging.CRITICAL">
  696. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">CRITICAL</span></span><a class="headerlink" href="#logging.CRITICAL" title="Link to this definition">¶</a></dt>
  697. <dd></dd></dl>
  698. </td>
  699. <td><p>50</p></td>
  700. <td><p>A serious error, indicating that the
  701. program itself may be unable to
  702. continue running.</p></td>
  703. </tr>
  704. </tbody>
  705. </table>
  706. </section>
  707. <section id="handler-objects">
  708. <span id="handler"></span><h2>Handler Objects<a class="headerlink" href="#handler-objects" title="Link to this heading">¶</a></h2>
  709. <p>Handlers have the following attributes and methods. Note that <a class="reference internal" href="#logging.Handler" title="logging.Handler"><code class="xref py py-class docutils literal notranslate"><span class="pre">Handler</span></code></a>
  710. is never instantiated directly; this class acts as a base for more useful
  711. subclasses. However, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method in subclasses needs to call
  712. <a class="reference internal" href="#logging.Handler.__init__" title="logging.Handler.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Handler.__init__()</span></code></a>.</p>
  713. <dl class="py class">
  714. <dt class="sig sig-object py" id="logging.Handler">
  715. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">Handler</span></span><a class="headerlink" href="#logging.Handler" title="Link to this definition">¶</a></dt>
  716. <dd><dl class="py method">
  717. <dt class="sig sig-object py" id="logging.Handler.__init__">
  718. <span class="sig-name descname"><span class="pre">__init__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">NOTSET</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.__init__" title="Link to this definition">¶</a></dt>
  719. <dd><p>Initializes the <a class="reference internal" href="#logging.Handler" title="logging.Handler"><code class="xref py py-class docutils literal notranslate"><span class="pre">Handler</span></code></a> instance by setting its level, setting the list
  720. of filters to the empty list and creating a lock (using <a class="reference internal" href="#logging.Handler.createLock" title="logging.Handler.createLock"><code class="xref py py-meth docutils literal notranslate"><span class="pre">createLock()</span></code></a>) for
  721. serializing access to an I/O mechanism.</p>
  722. </dd></dl>
  723. <dl class="py method">
  724. <dt class="sig sig-object py" id="logging.Handler.createLock">
  725. <span class="sig-name descname"><span class="pre">createLock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.createLock" title="Link to this definition">¶</a></dt>
  726. <dd><p>Initializes a thread lock which can be used to serialize access to underlying
  727. I/O functionality which may not be threadsafe.</p>
  728. </dd></dl>
  729. <dl class="py method">
  730. <dt class="sig sig-object py" id="logging.Handler.acquire">
  731. <span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.acquire" title="Link to this definition">¶</a></dt>
  732. <dd><p>Acquires the thread lock created with <a class="reference internal" href="#logging.Handler.createLock" title="logging.Handler.createLock"><code class="xref py py-meth docutils literal notranslate"><span class="pre">createLock()</span></code></a>.</p>
  733. </dd></dl>
  734. <dl class="py method">
  735. <dt class="sig sig-object py" id="logging.Handler.release">
  736. <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.release" title="Link to this definition">¶</a></dt>
  737. <dd><p>Releases the thread lock acquired with <a class="reference internal" href="#logging.Handler.acquire" title="logging.Handler.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a>.</p>
  738. </dd></dl>
  739. <dl class="py method">
  740. <dt class="sig sig-object py" id="logging.Handler.setLevel">
  741. <span class="sig-name descname"><span class="pre">setLevel</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.setLevel" title="Link to this definition">¶</a></dt>
  742. <dd><p>Sets the threshold for this handler to <em>level</em>. Logging messages which are
  743. less severe than <em>level</em> will be ignored. When a handler is created, the
  744. level is set to <a class="reference internal" href="#logging.NOTSET" title="logging.NOTSET"><code class="xref py py-const docutils literal notranslate"><span class="pre">NOTSET</span></code></a> (which causes all messages to be
  745. processed).</p>
  746. <p>See <a class="reference internal" href="#levels"><span class="std std-ref">Logging Levels</span></a> for a list of levels.</p>
  747. <div class="versionchanged">
  748. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>level</em> parameter now accepts a string representation of the
  749. level such as ‘INFO’ as an alternative to the integer constants
  750. such as <a class="reference internal" href="#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">INFO</span></code></a>.</p>
  751. </div>
  752. </dd></dl>
  753. <dl class="py method">
  754. <dt class="sig sig-object py" id="logging.Handler.setFormatter">
  755. <span class="sig-name descname"><span class="pre">setFormatter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fmt</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.setFormatter" title="Link to this definition">¶</a></dt>
  756. <dd><p>Sets the <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> for this handler to <em>fmt</em>.</p>
  757. </dd></dl>
  758. <dl class="py method">
  759. <dt class="sig sig-object py" id="logging.Handler.addFilter">
  760. <span class="sig-name descname"><span class="pre">addFilter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filter</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.addFilter" title="Link to this definition">¶</a></dt>
  761. <dd><p>Adds the specified filter <em>filter</em> to this handler.</p>
  762. </dd></dl>
  763. <dl class="py method">
  764. <dt class="sig sig-object py" id="logging.Handler.removeFilter">
  765. <span class="sig-name descname"><span class="pre">removeFilter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filter</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.removeFilter" title="Link to this definition">¶</a></dt>
  766. <dd><p>Removes the specified filter <em>filter</em> from this handler.</p>
  767. </dd></dl>
  768. <dl class="py method">
  769. <dt class="sig sig-object py" id="logging.Handler.filter">
  770. <span class="sig-name descname"><span class="pre">filter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.filter" title="Link to this definition">¶</a></dt>
  771. <dd><p>Apply this handler’s filters to the record and return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the
  772. record is to be processed. The filters are consulted in turn, until one of
  773. them returns a false value. If none of them return a false value, the record
  774. will be emitted. If one returns a false value, the handler will not emit the
  775. record.</p>
  776. </dd></dl>
  777. <dl class="py method">
  778. <dt class="sig sig-object py" id="logging.Handler.flush">
  779. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.flush" title="Link to this definition">¶</a></dt>
  780. <dd><p>Ensure all logging output has been flushed. This version does nothing and is
  781. intended to be implemented by subclasses.</p>
  782. </dd></dl>
  783. <dl class="py method">
  784. <dt class="sig sig-object py" id="logging.Handler.close">
  785. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.close" title="Link to this definition">¶</a></dt>
  786. <dd><p>Tidy up any resources used by the handler. This version does no output but
  787. removes the handler from an internal list of handlers which is closed when
  788. <a class="reference internal" href="#logging.shutdown" title="logging.shutdown"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutdown()</span></code></a> is called. Subclasses should ensure that this gets called
  789. from overridden <a class="reference internal" href="#logging.Handler.close" title="logging.Handler.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> methods.</p>
  790. </dd></dl>
  791. <dl class="py method">
  792. <dt class="sig sig-object py" id="logging.Handler.handle">
  793. <span class="sig-name descname"><span class="pre">handle</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.handle" title="Link to this definition">¶</a></dt>
  794. <dd><p>Conditionally emits the specified logging record, depending on filters which may
  795. have been added to the handler. Wraps the actual emission of the record with
  796. acquisition/release of the I/O thread lock.</p>
  797. </dd></dl>
  798. <dl class="py method">
  799. <dt class="sig sig-object py" id="logging.Handler.handleError">
  800. <span class="sig-name descname"><span class="pre">handleError</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.handleError" title="Link to this definition">¶</a></dt>
  801. <dd><p>This method should be called from handlers when an exception is encountered
  802. during an <a class="reference internal" href="#logging.Handler.emit" title="logging.Handler.emit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">emit()</span></code></a> call. If the module-level attribute
  803. <a class="reference internal" href="#logging.raiseExceptions" title="logging.raiseExceptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">raiseExceptions</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, exceptions get silently ignored. This is
  804. what is mostly wanted for a logging system - most users will not care about
  805. errors in the logging system, they are more interested in application
  806. errors. You could, however, replace this with a custom handler if you wish.
  807. The specified record is the one which was being processed when the exception
  808. occurred. (The default value of <a class="reference internal" href="#logging.raiseExceptions" title="logging.raiseExceptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">raiseExceptions</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, as that is
  809. more useful during development).</p>
  810. </dd></dl>
  811. <dl class="py method">
  812. <dt class="sig sig-object py" id="logging.Handler.format">
  813. <span class="sig-name descname"><span class="pre">format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.format" title="Link to this definition">¶</a></dt>
  814. <dd><p>Do formatting for a record - if a formatter is set, use it. Otherwise, use the
  815. default formatter for the module.</p>
  816. </dd></dl>
  817. <dl class="py method">
  818. <dt class="sig sig-object py" id="logging.Handler.emit">
  819. <span class="sig-name descname"><span class="pre">emit</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Handler.emit" title="Link to this definition">¶</a></dt>
  820. <dd><p>Do whatever it takes to actually log the specified logging record. This version
  821. is intended to be implemented by subclasses and so raises a
  822. <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  823. <div class="admonition warning">
  824. <p class="admonition-title">Warning</p>
  825. <p>This method is called after a handler-level lock is acquired, which
  826. is released after this method returns. When you override this method, note
  827. that you should be careful when calling anything that invokes other parts of
  828. the logging API which might do locking, because that might result in a
  829. deadlock. Specifically:</p>
  830. <ul class="simple">
  831. <li><p>Logging configuration APIs acquire the module-level lock, and then
  832. individual handler-level locks as those handlers are configured.</p></li>
  833. <li><p>Many logging APIs lock the module-level lock. If such an API is called
  834. from this method, it could cause a deadlock if a configuration call is
  835. made on another thread, because that thread will try to acquire the
  836. module-level lock <em>before</em> the handler-level lock, whereas this thread
  837. tries to acquire the module-level lock <em>after</em> the handler-level lock
  838. (because in this method, the handler-level lock has already been acquired).</p></li>
  839. </ul>
  840. </div>
  841. </dd></dl>
  842. </dd></dl>
  843. <p>For a list of handlers included as standard, see <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>
  844. </section>
  845. <section id="formatter-objects">
  846. <span id="id1"></span><h2>Formatter Objects<a class="headerlink" href="#formatter-objects" title="Link to this heading">¶</a></h2>
  847. <dl class="py class">
  848. <dt class="sig sig-object py" id="logging.Formatter">
  849. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">Formatter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fmt</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">datefmt</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">style</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'%'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">validate</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="o"><span class="pre">*</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><span class="sig-paren">)</span><a class="headerlink" href="#logging.Formatter" title="Link to this definition">¶</a></dt>
  850. <dd><p>Responsible for converting a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> to an output string
  851. to be interpreted by a human or external system.</p>
  852. <dl class="field-list simple">
  853. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  854. <dd class="field-odd"><ul class="simple">
  855. <li><p><strong>fmt</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – A format string in the given <em>style</em> for
  856. the logged output as a whole.
  857. The possible mapping keys are drawn from the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> object’s
  858. <a class="reference internal" href="#logrecord-attributes"><span class="std std-ref">LogRecord attributes</span></a>.
  859. If not specified, <code class="docutils literal notranslate"><span class="pre">'%(message)s'</span></code> is used,
  860. which is just the logged message.</p></li>
  861. <li><p><strong>datefmt</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – A format string in the given <em>style</em> for
  862. the date/time portion of the logged output.
  863. If not specified, the default described in <a class="reference internal" href="#logging.Formatter.formatTime" title="logging.Formatter.formatTime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formatTime()</span></code></a> is used.</p></li>
  864. <li><p><strong>style</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – Can be one of <code class="docutils literal notranslate"><span class="pre">'%'</span></code>, <code class="docutils literal notranslate"><span class="pre">'{'</span></code> or <code class="docutils literal notranslate"><span class="pre">'$'</span></code> and determines
  865. how the format string will be merged with its data: using one of
  866. <a class="reference internal" href="stdtypes.html#old-string-formatting"><span class="std std-ref">printf-style String Formatting</span></a> (<code class="docutils literal notranslate"><span class="pre">%</span></code>), <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> (<code class="docutils literal notranslate"><span class="pre">{</span></code>)
  867. or <a class="reference internal" href="string.html#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">string.Template</span></code></a> (<code class="docutils literal notranslate"><span class="pre">$</span></code>). This only applies to
  868. <em>fmt</em> and <em>datefmt</em> (e.g. <code class="docutils literal notranslate"><span class="pre">'%(message)s'</span></code> versus <code class="docutils literal notranslate"><span class="pre">'{message}'</span></code>),
  869. not to the actual log messages passed to the logging methods.
  870. However, there are <a class="reference internal" href="../howto/logging-cookbook.html#formatting-styles"><span class="std std-ref">other ways</span></a>
  871. to use <code class="docutils literal notranslate"><span class="pre">{</span></code>- and <code class="docutils literal notranslate"><span class="pre">$</span></code>-formatting for log messages.</p></li>
  872. <li><p><strong>validate</strong> (<a class="reference internal" href="functions.html#bool" title="bool"><em>bool</em></a>) – If <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), incorrect or mismatched
  873. <em>fmt</em> and <em>style</em> will 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>; for example,
  874. <code class="docutils literal notranslate"><span class="pre">logging.Formatter('%(asctime)s</span> <span class="pre">-</span> <span class="pre">%(message)s',</span> <span class="pre">style='{')</span></code>.</p></li>
  875. <li><p><strong>defaults</strong> (<a class="reference internal" href="stdtypes.html#dict" title="dict"><em>dict</em></a><em>[</em><a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a><em>, </em><em>Any</em><em>]</em>) – A dictionary with default values to use in custom fields.
  876. For example,
  877. <code class="docutils literal notranslate"><span class="pre">logging.Formatter('%(ip)s</span> <span class="pre">%(message)s',</span> <span class="pre">defaults={&quot;ip&quot;:</span> <span class="pre">None})</span></code></p></li>
  878. </ul>
  879. </dd>
  880. </dl>
  881. <div class="versionchanged">
  882. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>style</em> parameter.</p>
  883. </div>
  884. <div class="versionchanged">
  885. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>validate</em> parameter.</p>
  886. </div>
  887. <div class="versionchanged">
  888. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>defaults</em> parameter.</p>
  889. </div>
  890. <dl class="py method">
  891. <dt class="sig sig-object py" id="logging.Formatter.format">
  892. <span class="sig-name descname"><span class="pre">format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Formatter.format" title="Link to this definition">¶</a></dt>
  893. <dd><p>The record’s attribute dictionary is used as the operand to a string
  894. formatting operation. Returns the resulting string. Before formatting the
  895. dictionary, a couple of preparatory steps are carried out. The <em>message</em>
  896. attribute of the record is computed using <em>msg</em> % <em>args</em>. If the
  897. formatting string contains <code class="docutils literal notranslate"><span class="pre">'(asctime)'</span></code>, <a class="reference internal" href="#logging.Formatter.formatTime" title="logging.Formatter.formatTime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formatTime()</span></code></a> is called
  898. to format the event time. If there is exception information, it is
  899. formatted using <a class="reference internal" href="#logging.Formatter.formatException" title="logging.Formatter.formatException"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formatException()</span></code></a> and appended to the message. Note
  900. that the formatted exception information is cached in attribute
  901. <em>exc_text</em>. This is useful because the exception information can be
  902. pickled and sent across the wire, but you should be careful if you have
  903. more than one <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> subclass which customizes the formatting
  904. of exception information. In this case, you will have to clear the cached
  905. value (by setting the <em>exc_text</em> attribute to <code class="docutils literal notranslate"><span class="pre">None</span></code>) after a formatter
  906. has done its formatting, so that the next formatter to handle the event
  907. doesn’t use the cached value, but recalculates it afresh.</p>
  908. <p>If stack information is available, it’s appended after the exception
  909. information, using <a class="reference internal" href="#logging.Formatter.formatStack" title="logging.Formatter.formatStack"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formatStack()</span></code></a> to transform it if necessary.</p>
  910. </dd></dl>
  911. <dl class="py method">
  912. <dt class="sig sig-object py" id="logging.Formatter.formatTime">
  913. <span class="sig-name descname"><span class="pre">formatTime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">datefmt</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.Formatter.formatTime" title="Link to this definition">¶</a></dt>
  914. <dd><p>This method should be called from <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> by a formatter which
  915. wants to make use of a formatted time. This method can be overridden in
  916. formatters to provide for any specific requirement, but the basic behavior
  917. is as follows: if <em>datefmt</em> (a string) is specified, it is used with
  918. <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to format the creation time of the
  919. record. Otherwise, the format ‘%Y-%m-%d %H:%M:%S,uuu’ is used, where the
  920. uuu part is a millisecond value and the other letters are as per the
  921. <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> documentation. An example time in this format is
  922. <code class="docutils literal notranslate"><span class="pre">2003-01-23</span> <span class="pre">00:29:50,411</span></code>. The resulting string is returned.</p>
  923. <p>This function uses a user-configurable function to convert the creation
  924. time to a tuple. By default, <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a> is used; to change
  925. this for a particular formatter instance, set the <code class="docutils literal notranslate"><span class="pre">converter</span></code> attribute
  926. to a function with the same signature as <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a> or
  927. <a class="reference internal" href="time.html#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.gmtime()</span></code></a>. To change it for all formatters, for example if you
  928. want all logging times to be shown in GMT, set the <code class="docutils literal notranslate"><span class="pre">converter</span></code>
  929. attribute in the <code class="docutils literal notranslate"><span class="pre">Formatter</span></code> class.</p>
  930. <div class="versionchanged">
  931. <p><span class="versionmodified changed">Changed in version 3.3: </span>Previously, the default format was hard-coded as in this example:
  932. <code class="docutils literal notranslate"><span class="pre">2010-09-06</span> <span class="pre">22:38:15,292</span></code> where the part before the comma is
  933. handled by a strptime format string (<code class="docutils literal notranslate"><span class="pre">'%Y-%m-%d</span> <span class="pre">%H:%M:%S'</span></code>), and the
  934. part after the comma is a millisecond value. Because strptime does not
  935. have a format placeholder for milliseconds, the millisecond value is
  936. appended using another format string, <code class="docutils literal notranslate"><span class="pre">'%s,%03d'</span></code> — and both of these
  937. format strings have been hardcoded into this method. With the change,
  938. these strings are defined as class-level attributes which can be
  939. overridden at the instance level when desired. The names of the
  940. attributes are <code class="docutils literal notranslate"><span class="pre">default_time_format</span></code> (for the strptime format string)
  941. and <code class="docutils literal notranslate"><span class="pre">default_msec_format</span></code> (for appending the millisecond value).</p>
  942. </div>
  943. <div class="versionchanged">
  944. <p><span class="versionmodified changed">Changed in version 3.9: </span>The <code class="docutils literal notranslate"><span class="pre">default_msec_format</span></code> can be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  945. </div>
  946. </dd></dl>
  947. <dl class="py method">
  948. <dt class="sig sig-object py" id="logging.Formatter.formatException">
  949. <span class="sig-name descname"><span class="pre">formatException</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exc_info</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Formatter.formatException" title="Link to this definition">¶</a></dt>
  950. <dd><p>Formats the specified exception information (a standard exception tuple as
  951. returned by <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>) as a string. This default implementation
  952. just uses <a class="reference internal" href="traceback.html#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal notranslate"><span class="pre">traceback.print_exception()</span></code></a>. The resulting string is
  953. returned.</p>
  954. </dd></dl>
  955. <dl class="py method">
  956. <dt class="sig sig-object py" id="logging.Formatter.formatStack">
  957. <span class="sig-name descname"><span class="pre">formatStack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stack_info</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Formatter.formatStack" title="Link to this definition">¶</a></dt>
  958. <dd><p>Formats the specified stack information (a string as returned by
  959. <a class="reference internal" href="traceback.html#traceback.print_stack" title="traceback.print_stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">traceback.print_stack()</span></code></a>, but with the last newline removed) as a
  960. string. This default implementation just returns the input value.</p>
  961. </dd></dl>
  962. </dd></dl>
  963. <dl class="py class">
  964. <dt class="sig sig-object py" id="logging.BufferingFormatter">
  965. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">BufferingFormatter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">linefmt</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.BufferingFormatter" title="Link to this definition">¶</a></dt>
  966. <dd><p>A base formatter class suitable for subclassing when you want to format a
  967. number of records. You can pass a <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> instance which you want
  968. to use to format each line (that corresponds to a single record). If not
  969. specified, the default formatter (which just outputs the event message) is
  970. used as the line formatter.</p>
  971. <dl class="py method">
  972. <dt class="sig sig-object py" id="logging.BufferingFormatter.formatHeader">
  973. <span class="sig-name descname"><span class="pre">formatHeader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">records</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.BufferingFormatter.formatHeader" title="Link to this definition">¶</a></dt>
  974. <dd><p>Return a header for a list of <em>records</em>. The base implementation just
  975. returns the empty string. You will need to override this method if you
  976. want specific behaviour, e.g. to show the count of records, a title or a
  977. separator line.</p>
  978. </dd></dl>
  979. <dl class="py method">
  980. <dt class="sig sig-object py" id="logging.BufferingFormatter.formatFooter">
  981. <span class="sig-name descname"><span class="pre">formatFooter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">records</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.BufferingFormatter.formatFooter" title="Link to this definition">¶</a></dt>
  982. <dd><p>Return a footer for a list of <em>records</em>. The base implementation just
  983. returns the empty string. You will need to override this method if you
  984. want specific behaviour, e.g. to show the count of records or a separator
  985. line.</p>
  986. </dd></dl>
  987. <dl class="py method">
  988. <dt class="sig sig-object py" id="logging.BufferingFormatter.format">
  989. <span class="sig-name descname"><span class="pre">format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">records</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.BufferingFormatter.format" title="Link to this definition">¶</a></dt>
  990. <dd><p>Return formatted text for a list of <em>records</em>. The base implementation
  991. just returns the empty string if there are no records; otherwise, it
  992. returns the concatenation of the header, each record formatted with the
  993. line formatter, and the footer.</p>
  994. </dd></dl>
  995. </dd></dl>
  996. </section>
  997. <section id="filter-objects">
  998. <span id="filter"></span><h2>Filter Objects<a class="headerlink" href="#filter-objects" title="Link to this heading">¶</a></h2>
  999. <p><code class="docutils literal notranslate"><span class="pre">Filters</span></code> can be used by <code class="docutils literal notranslate"><span class="pre">Handlers</span></code> and <code class="docutils literal notranslate"><span class="pre">Loggers</span></code> for more sophisticated
  1000. filtering than is provided by levels. The base filter class only allows events
  1001. which are below a certain point in the logger hierarchy. For example, a filter
  1002. initialized with ‘A.B’ will allow events logged by loggers ‘A.B’, ‘A.B.C’,
  1003. ‘A.B.C.D’, ‘A.B.D’ etc. but not ‘A.BB’, ‘B.A.B’ etc. If initialized with the
  1004. empty string, all events are passed.</p>
  1005. <dl class="py class">
  1006. <dt class="sig sig-object py" id="logging.Filter">
  1007. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">Filter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Filter" title="Link to this definition">¶</a></dt>
  1008. <dd><p>Returns an instance of the <a class="reference internal" href="#logging.Filter" title="logging.Filter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Filter</span></code></a> class. If <em>name</em> is specified, it
  1009. names a logger which, together with its children, will have its events allowed
  1010. through the filter. If <em>name</em> is the empty string, allows every event.</p>
  1011. <dl class="py method">
  1012. <dt class="sig sig-object py" id="logging.Filter.filter">
  1013. <span class="sig-name descname"><span class="pre">filter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">record</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.Filter.filter" title="Link to this definition">¶</a></dt>
  1014. <dd><p>Is the specified record to be logged? Returns false for no, true for
  1015. yes. Filters can either modify log records in-place or return a completely
  1016. different record instance which will replace the original
  1017. log record in any future processing of the event.</p>
  1018. </dd></dl>
  1019. </dd></dl>
  1020. <p>Note that filters attached to handlers are consulted before an event is
  1021. emitted by the handler, whereas filters attached to loggers are consulted
  1022. whenever an event is logged (using <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>, <a class="reference internal" href="#logging.info" title="logging.info"><code class="xref py py-meth docutils literal notranslate"><span class="pre">info()</span></code></a>,
  1023. etc.), before sending an event to handlers. This means that events which have
  1024. been generated by descendant loggers will not be filtered by a logger’s filter
  1025. setting, unless the filter has also been applied to those descendant loggers.</p>
  1026. <p>You don’t actually need to subclass <code class="docutils literal notranslate"><span class="pre">Filter</span></code>: you can pass any instance
  1027. which has a <code class="docutils literal notranslate"><span class="pre">filter</span></code> method with the same semantics.</p>
  1028. <div class="versionchanged">
  1029. <p><span class="versionmodified changed">Changed in version 3.2: </span>You don’t need to create specialized <code class="docutils literal notranslate"><span class="pre">Filter</span></code> classes, or use other
  1030. classes with a <code class="docutils literal notranslate"><span class="pre">filter</span></code> method: you can use a function (or other
  1031. callable) as a filter. The filtering logic will check to see if the filter
  1032. object has a <code class="docutils literal notranslate"><span class="pre">filter</span></code> attribute: if it does, it’s assumed to be a
  1033. <code class="docutils literal notranslate"><span class="pre">Filter</span></code> and its <a class="reference internal" href="#logging.Filter.filter" title="logging.Filter.filter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">filter()</span></code></a> method is called. Otherwise, it’s
  1034. assumed to be a callable and called with the record as the single
  1035. parameter. The returned value should conform to that returned by
  1036. <a class="reference internal" href="#logging.Filter.filter" title="logging.Filter.filter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">filter()</span></code></a>.</p>
  1037. </div>
  1038. <div class="versionchanged">
  1039. <p><span class="versionmodified changed">Changed in version 3.12: </span>You can now return a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> instance from filters to replace
  1040. the log record rather than modifying it in place. This allows filters attached to
  1041. a <a class="reference internal" href="#logging.Handler" title="logging.Handler"><code class="xref py py-class docutils literal notranslate"><span class="pre">Handler</span></code></a> to modify the log record before it is emitted, without
  1042. having side effects on other handlers.</p>
  1043. </div>
  1044. <p>Although filters are used primarily to filter records based on more
  1045. sophisticated criteria than levels, they get to see every record which is
  1046. processed by the handler or logger they’re attached to: this can be useful if
  1047. you want to do things like counting how many records were processed by a
  1048. particular logger or handler, or adding, changing or removing attributes in
  1049. the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> being processed. Obviously changing the LogRecord needs
  1050. to be done with some care, but it does allow the injection of contextual
  1051. information into logs (see <a class="reference internal" href="../howto/logging-cookbook.html#filters-contextual"><span class="std std-ref">Using Filters to impart contextual information</span></a>).</p>
  1052. </section>
  1053. <section id="logrecord-objects">
  1054. <span id="log-record"></span><h2>LogRecord Objects<a class="headerlink" href="#logrecord-objects" title="Link to this heading">¶</a></h2>
  1055. <p><a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> instances are created automatically by the <a class="reference internal" href="#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a>
  1056. every time something is logged, and can be created manually via
  1057. <a class="reference internal" href="#logging.makeLogRecord" title="logging.makeLogRecord"><code class="xref py py-func docutils literal notranslate"><span class="pre">makeLogRecord()</span></code></a> (for example, from a pickled event received over the
  1058. wire).</p>
  1059. <dl class="py class">
  1060. <dt class="sig sig-object py" id="logging.LogRecord">
  1061. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">LogRecord</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pathname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_info</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</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">sinfo</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.LogRecord" title="Link to this definition">¶</a></dt>
  1062. <dd><p>Contains all the information pertinent to the event being logged.</p>
  1063. <p>The primary information is passed in <em>msg</em> and <em>args</em>,
  1064. which are combined using <code class="docutils literal notranslate"><span class="pre">msg</span> <span class="pre">%</span> <span class="pre">args</span></code> to create
  1065. the <code class="xref py py-attr docutils literal notranslate"><span class="pre">message</span></code> attribute of the record.</p>
  1066. <dl class="field-list simple">
  1067. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1068. <dd class="field-odd"><ul class="simple">
  1069. <li><p><strong>name</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the logger used to log the event
  1070. represented by this <code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code>.
  1071. Note that the logger name in the <code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code>
  1072. will always have this value,
  1073. even though it may be emitted by a handler
  1074. attached to a different (ancestor) logger.</p></li>
  1075. <li><p><strong>level</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The <a class="reference internal" href="#levels"><span class="std std-ref">numeric level</span></a> of the logging event
  1076. (such as <code class="docutils literal notranslate"><span class="pre">10</span></code> for <code class="docutils literal notranslate"><span class="pre">DEBUG</span></code>, <code class="docutils literal notranslate"><span class="pre">20</span></code> for <code class="docutils literal notranslate"><span class="pre">INFO</span></code>, etc).
  1077. Note that this is converted to <em>two</em> attributes of the LogRecord:
  1078. <code class="xref py py-attr docutils literal notranslate"><span class="pre">levelno</span></code> for the numeric value
  1079. and <code class="xref py py-attr docutils literal notranslate"><span class="pre">levelname</span></code> for the corresponding level name.</p></li>
  1080. <li><p><strong>pathname</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The full string path of the source file
  1081. where the logging call was made.</p></li>
  1082. <li><p><strong>lineno</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The line number in the source file
  1083. where the logging call was made.</p></li>
  1084. <li><p><strong>msg</strong> (<a class="reference internal" href="typing.html#typing.Any" title="typing.Any"><em>Any</em></a>) – The event description message,
  1085. which can be a %-format string with placeholders for variable data,
  1086. or an arbitrary object (see <a class="reference internal" href="../howto/logging.html#arbitrary-object-messages"><span class="std std-ref">Using arbitrary objects as messages</span></a>).</p></li>
  1087. <li><p><strong>args</strong> (<a class="reference internal" href="stdtypes.html#tuple" title="tuple"><em>tuple</em></a><em> | </em><a class="reference internal" href="stdtypes.html#dict" title="dict"><em>dict</em></a><em>[</em><a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a><em>, </em><a class="reference internal" href="typing.html#typing.Any" title="typing.Any"><em>Any</em></a><em>]</em>) – Variable data to merge into the <em>msg</em> argument
  1088. to obtain the event description.</p></li>
  1089. <li><p><strong>exc_info</strong> (<a class="reference internal" href="stdtypes.html#tuple" title="tuple"><em>tuple</em></a><em>[</em><a class="reference internal" href="functions.html#type" title="type"><em>type</em></a><em>[</em><a class="reference internal" href="exceptions.html#BaseException" title="BaseException"><em>BaseException</em></a><em>]</em><em>, </em><a class="reference internal" href="exceptions.html#BaseException" title="BaseException"><em>BaseException</em></a><em>, </em><a class="reference internal" href="types.html#types.TracebackType" title="types.TracebackType"><em>types.TracebackType</em></a><em>] </em><em>| </em><em>None</em>) – An exception tuple with the current exception information,
  1090. as returned by <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>,
  1091. or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no exception information is available.</p></li>
  1092. <li><p><strong>func</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a><em> | </em><em>None</em>) – The name of the function or method
  1093. from which the logging call was invoked.</p></li>
  1094. <li><p><strong>sinfo</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a><em> | </em><em>None</em>) – A text string representing stack information
  1095. from the base of the stack in the current thread,
  1096. up to the logging call.</p></li>
  1097. </ul>
  1098. </dd>
  1099. </dl>
  1100. <dl class="py method">
  1101. <dt class="sig sig-object py" id="logging.LogRecord.getMessage">
  1102. <span class="sig-name descname"><span class="pre">getMessage</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.LogRecord.getMessage" title="Link to this definition">¶</a></dt>
  1103. <dd><p>Returns the message for this <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> instance after merging any
  1104. user-supplied arguments with the message. If the user-supplied message
  1105. argument to the logging call is not a string, <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> is called on it to
  1106. convert it to a string. This allows use of user-defined classes as
  1107. messages, whose <code class="docutils literal notranslate"><span class="pre">__str__</span></code> method can return the actual format string to
  1108. be used.</p>
  1109. </dd></dl>
  1110. <div class="versionchanged">
  1111. <p><span class="versionmodified changed">Changed in version 3.2: </span>The creation of a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> has been made more configurable by
  1112. providing a factory which is used to create the record. The factory can be
  1113. set using <a class="reference internal" href="#logging.getLogRecordFactory" title="logging.getLogRecordFactory"><code class="xref py py-func docutils literal notranslate"><span class="pre">getLogRecordFactory()</span></code></a> and <a class="reference internal" href="#logging.setLogRecordFactory" title="logging.setLogRecordFactory"><code class="xref py py-func docutils literal notranslate"><span class="pre">setLogRecordFactory()</span></code></a>
  1114. (see this for the factory’s signature).</p>
  1115. </div>
  1116. <p>This functionality can be used to inject your own values into a
  1117. <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> at creation time. You can use the following pattern:</p>
  1118. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">old_factory</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogRecordFactory</span><span class="p">()</span>
  1119. <span class="k">def</span> <span class="nf">record_factory</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  1120. <span class="n">record</span> <span class="o">=</span> <span class="n">old_factory</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  1121. <span class="n">record</span><span class="o">.</span><span class="n">custom_attribute</span> <span class="o">=</span> <span class="mh">0xdecafbad</span>
  1122. <span class="k">return</span> <span class="n">record</span>
  1123. <span class="n">logging</span><span class="o">.</span><span class="n">setLogRecordFactory</span><span class="p">(</span><span class="n">record_factory</span><span class="p">)</span>
  1124. </pre></div>
  1125. </div>
  1126. <p>With this pattern, multiple factories could be chained, and as long
  1127. as they don’t overwrite each other’s attributes or unintentionally
  1128. overwrite the standard attributes listed above, there should be no
  1129. surprises.</p>
  1130. </dd></dl>
  1131. </section>
  1132. <section id="logrecord-attributes">
  1133. <span id="id2"></span><h2>LogRecord attributes<a class="headerlink" href="#logrecord-attributes" title="Link to this heading">¶</a></h2>
  1134. <p>The LogRecord has a number of attributes, most of which are derived from the
  1135. parameters to the constructor. (Note that the names do not always correspond
  1136. exactly between the LogRecord constructor parameters and the LogRecord
  1137. attributes.) These attributes can be used to merge data from the record into
  1138. the format string. The following table lists (in alphabetical order) the
  1139. attribute names, their meanings and the corresponding placeholder in a %-style
  1140. format string.</p>
  1141. <p>If you are using {}-formatting (<a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-func docutils literal notranslate"><span class="pre">str.format()</span></code></a>), you can use
  1142. <code class="docutils literal notranslate"><span class="pre">{attrname}</span></code> as the placeholder in the format string. If you are using
  1143. $-formatting (<a class="reference internal" href="string.html#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">string.Template</span></code></a>), use the form <code class="docutils literal notranslate"><span class="pre">${attrname}</span></code>. In
  1144. both cases, of course, replace <code class="docutils literal notranslate"><span class="pre">attrname</span></code> with the actual attribute name
  1145. you want to use.</p>
  1146. <p>In the case of {}-formatting, you can specify formatting flags by placing them
  1147. after the attribute name, separated from it with a colon. For example: a
  1148. placeholder of <code class="docutils literal notranslate"><span class="pre">{msecs:03.0f}</span></code> would format a millisecond value of <code class="docutils literal notranslate"><span class="pre">4</span></code> as
  1149. <code class="docutils literal notranslate"><span class="pre">004</span></code>. Refer to the <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> documentation for full details on
  1150. the options available to you.</p>
  1151. <table class="docutils align-default">
  1152. <thead>
  1153. <tr class="row-odd"><th class="head"><p>Attribute name</p></th>
  1154. <th class="head"><p>Format</p></th>
  1155. <th class="head"><p>Description</p></th>
  1156. </tr>
  1157. </thead>
  1158. <tbody>
  1159. <tr class="row-even"><td><p>args</p></td>
  1160. <td><p>You shouldn’t need to
  1161. format this yourself.</p></td>
  1162. <td><p>The tuple of arguments merged into <code class="docutils literal notranslate"><span class="pre">msg</span></code> to
  1163. produce <code class="docutils literal notranslate"><span class="pre">message</span></code>, or a dict whose values
  1164. are used for the merge (when there is only one
  1165. argument, and it is a dictionary).</p></td>
  1166. </tr>
  1167. <tr class="row-odd"><td><p>asctime</p></td>
  1168. <td><p><code class="docutils literal notranslate"><span class="pre">%(asctime)s</span></code></p></td>
  1169. <td><p>Human-readable time when the
  1170. <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> was created. By default
  1171. this is of the form ‘2003-07-08 16:49:45,896’
  1172. (the numbers after the comma are millisecond
  1173. portion of the time).</p></td>
  1174. </tr>
  1175. <tr class="row-even"><td><p>created</p></td>
  1176. <td><p><code class="docutils literal notranslate"><span class="pre">%(created)f</span></code></p></td>
  1177. <td><p>Time when the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> was created
  1178. (as returned by <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a>).</p></td>
  1179. </tr>
  1180. <tr class="row-odd"><td><p>exc_info</p></td>
  1181. <td><p>You shouldn’t need to
  1182. format this yourself.</p></td>
  1183. <td><p>Exception tuple (à la <code class="docutils literal notranslate"><span class="pre">sys.exc_info</span></code>) or,
  1184. if no exception has occurred, <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></td>
  1185. </tr>
  1186. <tr class="row-even"><td><p>filename</p></td>
  1187. <td><p><code class="docutils literal notranslate"><span class="pre">%(filename)s</span></code></p></td>
  1188. <td><p>Filename portion of <code class="docutils literal notranslate"><span class="pre">pathname</span></code>.</p></td>
  1189. </tr>
  1190. <tr class="row-odd"><td><p>funcName</p></td>
  1191. <td><p><code class="docutils literal notranslate"><span class="pre">%(funcName)s</span></code></p></td>
  1192. <td><p>Name of function containing the logging call.</p></td>
  1193. </tr>
  1194. <tr class="row-even"><td><p>levelname</p></td>
  1195. <td><p><code class="docutils literal notranslate"><span class="pre">%(levelname)s</span></code></p></td>
  1196. <td><p>Text logging level for the message
  1197. (<code class="docutils literal notranslate"><span class="pre">'DEBUG'</span></code>, <code class="docutils literal notranslate"><span class="pre">'INFO'</span></code>, <code class="docutils literal notranslate"><span class="pre">'WARNING'</span></code>,
  1198. <code class="docutils literal notranslate"><span class="pre">'ERROR'</span></code>, <code class="docutils literal notranslate"><span class="pre">'CRITICAL'</span></code>).</p></td>
  1199. </tr>
  1200. <tr class="row-odd"><td><p>levelno</p></td>
  1201. <td><p><code class="docutils literal notranslate"><span class="pre">%(levelno)s</span></code></p></td>
  1202. <td><p>Numeric logging level for the message
  1203. (<a class="reference internal" href="#logging.DEBUG" title="logging.DEBUG"><code class="xref py py-const docutils literal notranslate"><span class="pre">DEBUG</span></code></a>, <a class="reference internal" href="#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">INFO</span></code></a>,
  1204. <a class="reference internal" href="#logging.WARNING" title="logging.WARNING"><code class="xref py py-const docutils literal notranslate"><span class="pre">WARNING</span></code></a>, <a class="reference internal" href="#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ERROR</span></code></a>,
  1205. <a class="reference internal" href="#logging.CRITICAL" title="logging.CRITICAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">CRITICAL</span></code></a>).</p></td>
  1206. </tr>
  1207. <tr class="row-even"><td><p>lineno</p></td>
  1208. <td><p><code class="docutils literal notranslate"><span class="pre">%(lineno)d</span></code></p></td>
  1209. <td><p>Source line number where the logging call was
  1210. issued (if available).</p></td>
  1211. </tr>
  1212. <tr class="row-odd"><td><p>message</p></td>
  1213. <td><p><code class="docutils literal notranslate"><span class="pre">%(message)s</span></code></p></td>
  1214. <td><p>The logged message, computed as <code class="docutils literal notranslate"><span class="pre">msg</span> <span class="pre">%</span>
  1215. <span class="pre">args</span></code>. This is set when
  1216. <a class="reference internal" href="#logging.Formatter.format" title="logging.Formatter.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Formatter.format()</span></code></a> is invoked.</p></td>
  1217. </tr>
  1218. <tr class="row-even"><td><p>module</p></td>
  1219. <td><p><code class="docutils literal notranslate"><span class="pre">%(module)s</span></code></p></td>
  1220. <td><p>Module (name portion of <code class="docutils literal notranslate"><span class="pre">filename</span></code>).</p></td>
  1221. </tr>
  1222. <tr class="row-odd"><td><p>msecs</p></td>
  1223. <td><p><code class="docutils literal notranslate"><span class="pre">%(msecs)d</span></code></p></td>
  1224. <td><p>Millisecond portion of the time when the
  1225. <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> was created.</p></td>
  1226. </tr>
  1227. <tr class="row-even"><td><p>msg</p></td>
  1228. <td><p>You shouldn’t need to
  1229. format this yourself.</p></td>
  1230. <td><p>The format string passed in the original
  1231. logging call. Merged with <code class="docutils literal notranslate"><span class="pre">args</span></code> to
  1232. produce <code class="docutils literal notranslate"><span class="pre">message</span></code>, or an arbitrary object
  1233. (see <a class="reference internal" href="../howto/logging.html#arbitrary-object-messages"><span class="std std-ref">Using arbitrary objects as messages</span></a>).</p></td>
  1234. </tr>
  1235. <tr class="row-odd"><td><p>name</p></td>
  1236. <td><p><code class="docutils literal notranslate"><span class="pre">%(name)s</span></code></p></td>
  1237. <td><p>Name of the logger used to log the call.</p></td>
  1238. </tr>
  1239. <tr class="row-even"><td><p>pathname</p></td>
  1240. <td><p><code class="docutils literal notranslate"><span class="pre">%(pathname)s</span></code></p></td>
  1241. <td><p>Full pathname of the source file where the
  1242. logging call was issued (if available).</p></td>
  1243. </tr>
  1244. <tr class="row-odd"><td><p>process</p></td>
  1245. <td><p><code class="docutils literal notranslate"><span class="pre">%(process)d</span></code></p></td>
  1246. <td><p>Process ID (if available).</p></td>
  1247. </tr>
  1248. <tr class="row-even"><td><p>processName</p></td>
  1249. <td><p><code class="docutils literal notranslate"><span class="pre">%(processName)s</span></code></p></td>
  1250. <td><p>Process name (if available).</p></td>
  1251. </tr>
  1252. <tr class="row-odd"><td><p>relativeCreated</p></td>
  1253. <td><p><code class="docutils literal notranslate"><span class="pre">%(relativeCreated)d</span></code></p></td>
  1254. <td><p>Time in milliseconds when the LogRecord was
  1255. created, relative to the time the logging
  1256. module was loaded.</p></td>
  1257. </tr>
  1258. <tr class="row-even"><td><p>stack_info</p></td>
  1259. <td><p>You shouldn’t need to
  1260. format this yourself.</p></td>
  1261. <td><p>Stack frame information (where available)
  1262. from the bottom of the stack in the current
  1263. thread, up to and including the stack frame
  1264. of the logging call which resulted in the
  1265. creation of this record.</p></td>
  1266. </tr>
  1267. <tr class="row-odd"><td><p>thread</p></td>
  1268. <td><p><code class="docutils literal notranslate"><span class="pre">%(thread)d</span></code></p></td>
  1269. <td><p>Thread ID (if available).</p></td>
  1270. </tr>
  1271. <tr class="row-even"><td><p>threadName</p></td>
  1272. <td><p><code class="docutils literal notranslate"><span class="pre">%(threadName)s</span></code></p></td>
  1273. <td><p>Thread name (if available).</p></td>
  1274. </tr>
  1275. <tr class="row-odd"><td><p>taskName</p></td>
  1276. <td><p><code class="docutils literal notranslate"><span class="pre">%(taskName)s</span></code></p></td>
  1277. <td><p><a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Task</span></code></a> name (if available).</p></td>
  1278. </tr>
  1279. </tbody>
  1280. </table>
  1281. <div class="versionchanged">
  1282. <p><span class="versionmodified changed">Changed in version 3.1: </span><em>processName</em> was added.</p>
  1283. </div>
  1284. <div class="versionchanged">
  1285. <p><span class="versionmodified changed">Changed in version 3.12: </span><em>taskName</em> was added.</p>
  1286. </div>
  1287. </section>
  1288. <section id="loggeradapter-objects">
  1289. <span id="logger-adapter"></span><h2>LoggerAdapter Objects<a class="headerlink" href="#loggeradapter-objects" title="Link to this heading">¶</a></h2>
  1290. <p><a class="reference internal" href="#logging.LoggerAdapter" title="logging.LoggerAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">LoggerAdapter</span></code></a> instances are used to conveniently pass contextual
  1291. information into logging calls. For a usage example, see the section on
  1292. <a class="reference internal" href="../howto/logging-cookbook.html#context-info"><span class="std std-ref">adding contextual information to your logging output</span></a>.</p>
  1293. <dl class="py class">
  1294. <dt class="sig sig-object py" id="logging.LoggerAdapter">
  1295. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">LoggerAdapter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">logger</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">extra</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.LoggerAdapter" title="Link to this definition">¶</a></dt>
  1296. <dd><p>Returns an instance of <a class="reference internal" href="#logging.LoggerAdapter" title="logging.LoggerAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">LoggerAdapter</span></code></a> initialized with an
  1297. underlying <a class="reference internal" href="#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a> instance and a dict-like object.</p>
  1298. <dl class="py method">
  1299. <dt class="sig sig-object py" id="logging.LoggerAdapter.process">
  1300. <span class="sig-name descname"><span class="pre">process</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.LoggerAdapter.process" title="Link to this definition">¶</a></dt>
  1301. <dd><p>Modifies the message and/or keyword arguments passed to a logging call in
  1302. order to insert contextual information. This implementation takes the object
  1303. passed as <em>extra</em> to the constructor and adds it to <em>kwargs</em> using key
  1304. ‘extra’. The return value is a (<em>msg</em>, <em>kwargs</em>) tuple which has the
  1305. (possibly modified) versions of the arguments passed in.</p>
  1306. </dd></dl>
  1307. <dl class="py attribute">
  1308. <dt class="sig sig-object py" id="logging.LoggerAdapter.manager">
  1309. <span class="sig-name descname"><span class="pre">manager</span></span><a class="headerlink" href="#logging.LoggerAdapter.manager" title="Link to this definition">¶</a></dt>
  1310. <dd><p>Delegates to the underlying <code class="xref py py-attr docutils literal notranslate"><span class="pre">manager`</span></code> on <em>logger</em>.</p>
  1311. </dd></dl>
  1312. <dl class="py attribute">
  1313. <dt class="sig sig-object py" id="logging.LoggerAdapter._log">
  1314. <span class="sig-name descname"><span class="pre">_log</span></span><a class="headerlink" href="#logging.LoggerAdapter._log" title="Link to this definition">¶</a></dt>
  1315. <dd><p>Delegates to the underlying <code class="xref py py-meth docutils literal notranslate"><span class="pre">_log`()</span></code> method on <em>logger</em>.</p>
  1316. </dd></dl>
  1317. <p>In addition to the above, <a class="reference internal" href="#logging.LoggerAdapter" title="logging.LoggerAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">LoggerAdapter</span></code></a> supports the following
  1318. methods of <a class="reference internal" href="#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a>: <a class="reference internal" href="#logging.Logger.debug" title="logging.Logger.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>, <a class="reference internal" href="#logging.Logger.info" title="logging.Logger.info"><code class="xref py py-meth docutils literal notranslate"><span class="pre">info()</span></code></a>,
  1319. <a class="reference internal" href="#logging.Logger.warning" title="logging.Logger.warning"><code class="xref py py-meth docutils literal notranslate"><span class="pre">warning()</span></code></a>, <a class="reference internal" href="#logging.Logger.error" title="logging.Logger.error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">error()</span></code></a>, <a class="reference internal" href="#logging.Logger.exception" title="logging.Logger.exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exception()</span></code></a>,
  1320. <a class="reference internal" href="#logging.Logger.critical" title="logging.Logger.critical"><code class="xref py py-meth docutils literal notranslate"><span class="pre">critical()</span></code></a>, <a class="reference internal" href="#logging.Logger.log" title="logging.Logger.log"><code class="xref py py-meth docutils literal notranslate"><span class="pre">log()</span></code></a>, <a class="reference internal" href="#logging.Logger.isEnabledFor" title="logging.Logger.isEnabledFor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isEnabledFor()</span></code></a>,
  1321. <a class="reference internal" href="#logging.Logger.getEffectiveLevel" title="logging.Logger.getEffectiveLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getEffectiveLevel()</span></code></a>, <a class="reference internal" href="#logging.Logger.setLevel" title="logging.Logger.setLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setLevel()</span></code></a> and
  1322. <a class="reference internal" href="#logging.Logger.hasHandlers" title="logging.Logger.hasHandlers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">hasHandlers()</span></code></a>. These methods have the same signatures as their
  1323. counterparts in <a class="reference internal" href="#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a>, so you can use the two types of instances
  1324. interchangeably.</p>
  1325. <div class="versionchanged">
  1326. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <a class="reference internal" href="#logging.Logger.isEnabledFor" title="logging.Logger.isEnabledFor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isEnabledFor()</span></code></a>, <a class="reference internal" href="#logging.Logger.getEffectiveLevel" title="logging.Logger.getEffectiveLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getEffectiveLevel()</span></code></a>,
  1327. <a class="reference internal" href="#logging.Logger.setLevel" title="logging.Logger.setLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setLevel()</span></code></a> and <a class="reference internal" href="#logging.Logger.hasHandlers" title="logging.Logger.hasHandlers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">hasHandlers()</span></code></a> methods were added
  1328. to <a class="reference internal" href="#logging.LoggerAdapter" title="logging.LoggerAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">LoggerAdapter</span></code></a>. These methods delegate to the underlying logger.</p>
  1329. </div>
  1330. <div class="versionchanged">
  1331. <p><span class="versionmodified changed">Changed in version 3.6: </span>Attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">manager</span></code> and method <code class="xref py py-meth docutils literal notranslate"><span class="pre">_log()</span></code> were added, which
  1332. delegate to the underlying logger and allow adapters to be nested.</p>
  1333. </div>
  1334. </dd></dl>
  1335. </section>
  1336. <section id="thread-safety">
  1337. <h2>Thread Safety<a class="headerlink" href="#thread-safety" title="Link to this heading">¶</a></h2>
  1338. <p>The logging module is intended to be thread-safe without any special work
  1339. needing to be done by its clients. It achieves this though using threading
  1340. locks; there is one lock to serialize access to the module’s shared data, and
  1341. each handler also creates a lock to serialize access to its underlying I/O.</p>
  1342. <p>If you are implementing asynchronous signal handlers using the <a class="reference internal" href="signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code></a>
  1343. module, you may not be able to use logging from within such handlers. This is
  1344. because lock implementations in the <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module are not always
  1345. re-entrant, and so cannot be invoked from such signal handlers.</p>
  1346. </section>
  1347. <section id="module-level-functions">
  1348. <h2>Module-Level Functions<a class="headerlink" href="#module-level-functions" title="Link to this heading">¶</a></h2>
  1349. <p>In addition to the classes described above, there are a number of module-level
  1350. functions.</p>
  1351. <dl class="py function">
  1352. <dt class="sig sig-object py" id="logging.getLogger">
  1353. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">getLogger</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</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.getLogger" title="Link to this definition">¶</a></dt>
  1354. <dd><p>Return a logger with the specified name or, if name is <code class="docutils literal notranslate"><span class="pre">None</span></code>, return a
  1355. logger which is the root logger of the hierarchy. If specified, the name is
  1356. typically a dot-separated hierarchical name like <em>‘a’</em>, <em>‘a.b’</em> or <em>‘a.b.c.d’</em>.
  1357. Choice of these names is entirely up to the developer who is using logging.</p>
  1358. <p>All calls to this function with a given name return the same logger instance.
  1359. This means that logger instances never need to be passed between different parts
  1360. of an application.</p>
  1361. </dd></dl>
  1362. <dl class="py function">
  1363. <dt class="sig sig-object py" id="logging.getLoggerClass">
  1364. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">getLoggerClass</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.getLoggerClass" title="Link to this definition">¶</a></dt>
  1365. <dd><p>Return either the standard <a class="reference internal" href="#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a> class, or the last class passed to
  1366. <a class="reference internal" href="#logging.setLoggerClass" title="logging.setLoggerClass"><code class="xref py py-func docutils literal notranslate"><span class="pre">setLoggerClass()</span></code></a>. This function may be called from within a new class
  1367. definition, to ensure that installing a customized <a class="reference internal" href="#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a> class will
  1368. not undo customizations already applied by other code. For example:</p>
  1369. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyLogger</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">getLoggerClass</span><span class="p">()):</span>
  1370. <span class="c1"># ... override behaviour here</span>
  1371. </pre></div>
  1372. </div>
  1373. </dd></dl>
  1374. <dl class="py function">
  1375. <dt class="sig sig-object py" id="logging.getLogRecordFactory">
  1376. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">getLogRecordFactory</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.getLogRecordFactory" title="Link to this definition">¶</a></dt>
  1377. <dd><p>Return a callable which is used to create a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a>.</p>
  1378. <div class="versionadded">
  1379. <p><span class="versionmodified added">New in version 3.2: </span>This function has been provided, along with <a class="reference internal" href="#logging.setLogRecordFactory" title="logging.setLogRecordFactory"><code class="xref py py-func docutils literal notranslate"><span class="pre">setLogRecordFactory()</span></code></a>,
  1380. to allow developers more control over how the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a>
  1381. representing a logging event is constructed.</p>
  1382. </div>
  1383. <p>See <a class="reference internal" href="#logging.setLogRecordFactory" title="logging.setLogRecordFactory"><code class="xref py py-func docutils literal notranslate"><span class="pre">setLogRecordFactory()</span></code></a> for more information about the how the
  1384. factory is called.</p>
  1385. </dd></dl>
  1386. <dl class="py function">
  1387. <dt class="sig sig-object py" id="logging.debug">
  1388. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">debug</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.debug" title="Link to this definition">¶</a></dt>
  1389. <dd><p>This is a convenience function that calls <a class="reference internal" href="#logging.Logger.debug" title="logging.Logger.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.debug()</span></code></a>, on the root
  1390. logger. The handling of the arguments is in every way identical
  1391. to what is described in that method.</p>
  1392. <p>The only difference is that if the root logger has no handlers, then
  1393. <a class="reference internal" href="#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> is called, prior to calling <code class="docutils literal notranslate"><span class="pre">debug</span></code> on the root logger.</p>
  1394. <p>For very short scripts or quick demonstrations of <code class="docutils literal notranslate"><span class="pre">logging</span></code> facilities,
  1395. <code class="docutils literal notranslate"><span class="pre">debug</span></code> and the other module-level functions may be convenient. However,
  1396. most programs will want to carefully and explicitly control the logging
  1397. configuration, and should therefore prefer creating a module-level logger and
  1398. calling <a class="reference internal" href="#logging.Logger.debug" title="logging.Logger.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.debug()</span></code></a> (or other level-specific methods) on it, as
  1399. described at the beginnning of this documentation.</p>
  1400. </dd></dl>
  1401. <dl class="py function">
  1402. <dt class="sig sig-object py" id="logging.info">
  1403. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">info</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.info" title="Link to this definition">¶</a></dt>
  1404. <dd><p>Logs a message with level <a class="reference internal" href="#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">INFO</span></code></a> on the root logger. The arguments and behavior
  1405. are otherwise the same as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  1406. </dd></dl>
  1407. <dl class="py function">
  1408. <dt class="sig sig-object py" id="logging.warning">
  1409. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">warning</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.warning" title="Link to this definition">¶</a></dt>
  1410. <dd><p>Logs a message with level <a class="reference internal" href="#logging.WARNING" title="logging.WARNING"><code class="xref py py-const docutils literal notranslate"><span class="pre">WARNING</span></code></a> on the root logger. The arguments and behavior
  1411. are otherwise the same as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  1412. <div class="admonition note">
  1413. <p class="admonition-title">Note</p>
  1414. <p>There is an obsolete function <code class="docutils literal notranslate"><span class="pre">warn</span></code> which is functionally
  1415. identical to <code class="docutils literal notranslate"><span class="pre">warning</span></code>. As <code class="docutils literal notranslate"><span class="pre">warn</span></code> is deprecated, please do not use
  1416. it - use <code class="docutils literal notranslate"><span class="pre">warning</span></code> instead.</p>
  1417. </div>
  1418. </dd></dl>
  1419. <dl class="py function">
  1420. <dt class="sig sig-object py" id="logging.error">
  1421. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">error</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.error" title="Link to this definition">¶</a></dt>
  1422. <dd><p>Logs a message with level <a class="reference internal" href="#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ERROR</span></code></a> on the root logger. The arguments and behavior
  1423. are otherwise the same as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  1424. </dd></dl>
  1425. <dl class="py function">
  1426. <dt class="sig sig-object py" id="logging.critical">
  1427. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">critical</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.critical" title="Link to this definition">¶</a></dt>
  1428. <dd><p>Logs a message with level <a class="reference internal" href="#logging.CRITICAL" title="logging.CRITICAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">CRITICAL</span></code></a> on the root logger. The arguments and behavior
  1429. are otherwise the same as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  1430. </dd></dl>
  1431. <dl class="py function">
  1432. <dt class="sig sig-object py" id="logging.exception">
  1433. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.exception" title="Link to this definition">¶</a></dt>
  1434. <dd><p>Logs a message with level <a class="reference internal" href="#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ERROR</span></code></a> on the root logger. The arguments and behavior
  1435. are otherwise the same as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>. Exception info is added to the logging
  1436. message. This function should only be called from an exception handler.</p>
  1437. </dd></dl>
  1438. <dl class="py function">
  1439. <dt class="sig sig-object py" id="logging.log">
  1440. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">log</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.log" title="Link to this definition">¶</a></dt>
  1441. <dd><p>Logs a message with level <em>level</em> on the root logger. The arguments and behavior
  1442. are otherwise the same as for <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>.</p>
  1443. </dd></dl>
  1444. <dl class="py function">
  1445. <dt class="sig sig-object py" id="logging.disable">
  1446. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">disable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">CRITICAL</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.disable" title="Link to this definition">¶</a></dt>
  1447. <dd><p>Provides an overriding level <em>level</em> for all loggers which takes precedence over
  1448. the logger’s own level. When the need arises to temporarily throttle logging
  1449. output down across the whole application, this function can be useful. Its
  1450. effect is to disable all logging calls of severity <em>level</em> and below, so that
  1451. if you call it with a value of INFO, then all INFO and DEBUG events would be
  1452. discarded, whereas those of severity WARNING and above would be processed
  1453. according to the logger’s effective level. If
  1454. <code class="docutils literal notranslate"><span class="pre">logging.disable(logging.NOTSET)</span></code> is called, it effectively removes this
  1455. overriding level, so that logging output again depends on the effective
  1456. levels of individual loggers.</p>
  1457. <p>Note that if you have defined any custom logging level higher than
  1458. <code class="docutils literal notranslate"><span class="pre">CRITICAL</span></code> (this is not recommended), you won’t be able to rely on the
  1459. default value for the <em>level</em> parameter, but will have to explicitly supply a
  1460. suitable value.</p>
  1461. <div class="versionchanged">
  1462. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>level</em> parameter was defaulted to level <code class="docutils literal notranslate"><span class="pre">CRITICAL</span></code>. See
  1463. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=28524">bpo-28524</a> for more information about this change.</p>
  1464. </div>
  1465. </dd></dl>
  1466. <dl class="py function">
  1467. <dt class="sig sig-object py" id="logging.addLevelName">
  1468. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">addLevelName</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">levelName</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.addLevelName" title="Link to this definition">¶</a></dt>
  1469. <dd><p>Associates level <em>level</em> with text <em>levelName</em> in an internal dictionary, which is
  1470. used to map numeric levels to a textual representation, for example when a
  1471. <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> formats a message. This function can also be used to define
  1472. your own levels. The only constraints are that all levels used must be
  1473. registered using this function, levels should be positive integers and they
  1474. should increase in increasing order of severity.</p>
  1475. <div class="admonition note">
  1476. <p class="admonition-title">Note</p>
  1477. <p>If you are thinking of defining your own levels, please see the
  1478. section on <a class="reference internal" href="../howto/logging.html#custom-levels"><span class="std std-ref">Custom Levels</span></a>.</p>
  1479. </div>
  1480. </dd></dl>
  1481. <dl class="py function">
  1482. <dt class="sig sig-object py" id="logging.getLevelNamesMapping">
  1483. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">getLevelNamesMapping</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.getLevelNamesMapping" title="Link to this definition">¶</a></dt>
  1484. <dd><p>Returns a mapping from level names to their corresponding logging levels. For example, the
  1485. string “CRITICAL” maps to <a class="reference internal" href="#logging.CRITICAL" title="logging.CRITICAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">CRITICAL</span></code></a>. The returned mapping is copied from an internal
  1486. mapping on each call to this function.</p>
  1487. <div class="versionadded">
  1488. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1489. </div>
  1490. </dd></dl>
  1491. <dl class="py function">
  1492. <dt class="sig sig-object py" id="logging.getLevelName">
  1493. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">getLevelName</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.getLevelName" title="Link to this definition">¶</a></dt>
  1494. <dd><p>Returns the textual or numeric representation of logging level <em>level</em>.</p>
  1495. <p>If <em>level</em> is one of the predefined levels <a class="reference internal" href="#logging.CRITICAL" title="logging.CRITICAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">CRITICAL</span></code></a>, <a class="reference internal" href="#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ERROR</span></code></a>,
  1496. <a class="reference internal" href="#logging.WARNING" title="logging.WARNING"><code class="xref py py-const docutils literal notranslate"><span class="pre">WARNING</span></code></a>, <a class="reference internal" href="#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">INFO</span></code></a> or <a class="reference internal" href="#logging.DEBUG" title="logging.DEBUG"><code class="xref py py-const docutils literal notranslate"><span class="pre">DEBUG</span></code></a> then you get the
  1497. corresponding string. If you have associated levels with names using
  1498. <a class="reference internal" href="#logging.addLevelName" title="logging.addLevelName"><code class="xref py py-func docutils literal notranslate"><span class="pre">addLevelName()</span></code></a> then the name you have associated with <em>level</em> is
  1499. returned. If a numeric value corresponding to one of the defined levels is
  1500. passed in, the corresponding string representation is returned.</p>
  1501. <p>The <em>level</em> parameter also accepts a string representation of the level such
  1502. as ‘INFO’. In such cases, this functions returns the corresponding numeric
  1503. value of the level.</p>
  1504. <p>If no matching numeric or string value is passed in, the string
  1505. ‘Level %s’ % level is returned.</p>
  1506. <div class="admonition note">
  1507. <p class="admonition-title">Note</p>
  1508. <p>Levels are internally integers (as they need to be compared in the
  1509. logging logic). This function is used to convert between an integer level
  1510. and the level name displayed in the formatted log output by means of the
  1511. <code class="docutils literal notranslate"><span class="pre">%(levelname)s</span></code> format specifier (see <a class="reference internal" href="#logrecord-attributes"><span class="std std-ref">LogRecord attributes</span></a>), and
  1512. vice versa.</p>
  1513. </div>
  1514. <div class="versionchanged">
  1515. <p><span class="versionmodified changed">Changed in version 3.4: </span>In Python versions earlier than 3.4, this function could also be passed a
  1516. text level, and would return the corresponding numeric value of the level.
  1517. This undocumented behaviour was considered a mistake, and was removed in
  1518. Python 3.4, but reinstated in 3.4.2 due to retain backward compatibility.</p>
  1519. </div>
  1520. </dd></dl>
  1521. <dl class="py function">
  1522. <dt class="sig sig-object py" id="logging.getHandlerByName">
  1523. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">getHandlerByName</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.getHandlerByName" title="Link to this definition">¶</a></dt>
  1524. <dd><p>Returns a handler with the specified <em>name</em>, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if there is no handler
  1525. with that name.</p>
  1526. <div class="versionadded">
  1527. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1528. </div>
  1529. </dd></dl>
  1530. <dl class="py function">
  1531. <dt class="sig sig-object py" id="logging.getHandlerNames">
  1532. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">getHandlerNames</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.getHandlerNames" title="Link to this definition">¶</a></dt>
  1533. <dd><p>Returns an immutable set of all known handler names.</p>
  1534. <div class="versionadded">
  1535. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1536. </div>
  1537. </dd></dl>
  1538. <dl class="py function">
  1539. <dt class="sig sig-object py" id="logging.makeLogRecord">
  1540. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">makeLogRecord</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">attrdict</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.makeLogRecord" title="Link to this definition">¶</a></dt>
  1541. <dd><p>Creates and returns a new <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> instance whose attributes are
  1542. defined by <em>attrdict</em>. This function is useful for taking a pickled
  1543. <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> attribute dictionary, sent over a socket, and reconstituting
  1544. it as a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> instance at the receiving end.</p>
  1545. </dd></dl>
  1546. <dl class="py function">
  1547. <dt class="sig sig-object py" id="logging.basicConfig">
  1548. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">basicConfig</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.basicConfig" title="Link to this definition">¶</a></dt>
  1549. <dd><p>Does basic configuration for the logging system by creating a
  1550. <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">StreamHandler</span></code></a> with a default <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> and adding it to the
  1551. root logger. The functions <a class="reference internal" href="#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>, <a class="reference internal" href="#logging.info" title="logging.info"><code class="xref py py-func docutils literal notranslate"><span class="pre">info()</span></code></a>, <a class="reference internal" href="#logging.warning" title="logging.warning"><code class="xref py py-func docutils literal notranslate"><span class="pre">warning()</span></code></a>,
  1552. <a class="reference internal" href="#logging.error" title="logging.error"><code class="xref py py-func docutils literal notranslate"><span class="pre">error()</span></code></a> and <a class="reference internal" href="#logging.critical" title="logging.critical"><code class="xref py py-func docutils literal notranslate"><span class="pre">critical()</span></code></a> will call <a class="reference internal" href="#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> automatically
  1553. if no handlers are defined for the root logger.</p>
  1554. <p>This function does nothing if the root logger already has handlers
  1555. configured, unless the keyword argument <em>force</em> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  1556. <div class="admonition note">
  1557. <p class="admonition-title">Note</p>
  1558. <p>This function should be called from the main thread
  1559. before other threads are started. In versions of Python prior to
  1560. 2.7.1 and 3.2, if this function is called from multiple threads,
  1561. it is possible (in rare circumstances) that a handler will be added
  1562. to the root logger more than once, leading to unexpected results
  1563. such as messages being duplicated in the log.</p>
  1564. </div>
  1565. <p>The following keyword arguments are supported.</p>
  1566. <table class="docutils align-default">
  1567. <thead>
  1568. <tr class="row-odd"><th class="head"><p>Format</p></th>
  1569. <th class="head"><p>Description</p></th>
  1570. </tr>
  1571. </thead>
  1572. <tbody>
  1573. <tr class="row-even"><td><p><em>filename</em></p></td>
  1574. <td><p>Specifies that a <a class="reference internal" href="logging.handlers.html#logging.FileHandler" title="logging.FileHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileHandler</span></code></a> be
  1575. created, using the specified filename,
  1576. rather than a <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">StreamHandler</span></code></a>.</p></td>
  1577. </tr>
  1578. <tr class="row-odd"><td><p><em>filemode</em></p></td>
  1579. <td><p>If <em>filename</em> is specified, open the file
  1580. in this <a class="reference internal" href="functions.html#filemodes"><span class="std std-ref">mode</span></a>. Defaults
  1581. to <code class="docutils literal notranslate"><span class="pre">'a'</span></code>.</p></td>
  1582. </tr>
  1583. <tr class="row-even"><td><p><em>format</em></p></td>
  1584. <td><p>Use the specified format string for the
  1585. handler. Defaults to attributes
  1586. <code class="docutils literal notranslate"><span class="pre">levelname</span></code>, <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">message</span></code>
  1587. separated by colons.</p></td>
  1588. </tr>
  1589. <tr class="row-odd"><td><p><em>datefmt</em></p></td>
  1590. <td><p>Use the specified date/time format, as
  1591. accepted by <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>.</p></td>
  1592. </tr>
  1593. <tr class="row-even"><td><p><em>style</em></p></td>
  1594. <td><p>If <em>format</em> is specified, use this style
  1595. for the format string. One of <code class="docutils literal notranslate"><span class="pre">'%'</span></code>,
  1596. <code class="docutils literal notranslate"><span class="pre">'{'</span></code> or <code class="docutils literal notranslate"><span class="pre">'$'</span></code> for <a class="reference internal" href="stdtypes.html#old-string-formatting"><span class="std std-ref">printf-style</span></a>,
  1597. <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> or
  1598. <a class="reference internal" href="string.html#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">string.Template</span></code></a> respectively.
  1599. Defaults to <code class="docutils literal notranslate"><span class="pre">'%'</span></code>.</p></td>
  1600. </tr>
  1601. <tr class="row-odd"><td><p><em>level</em></p></td>
  1602. <td><p>Set the root logger level to the specified
  1603. <a class="reference internal" href="#levels"><span class="std std-ref">level</span></a>.</p></td>
  1604. </tr>
  1605. <tr class="row-even"><td><p><em>stream</em></p></td>
  1606. <td><p>Use the specified stream to initialize the
  1607. <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">StreamHandler</span></code></a>. Note that this
  1608. argument is incompatible with <em>filename</em> -
  1609. if both are present, a <code class="docutils literal notranslate"><span class="pre">ValueError</span></code> is
  1610. raised.</p></td>
  1611. </tr>
  1612. <tr class="row-odd"><td><p><em>handlers</em></p></td>
  1613. <td><p>If specified, this should be an iterable of
  1614. already created handlers to add to the root
  1615. logger. Any handlers which don’t already
  1616. have a formatter set will be assigned the
  1617. default formatter created in this function.
  1618. Note that this argument is incompatible
  1619. with <em>filename</em> or <em>stream</em> - if both
  1620. are present, a <code class="docutils literal notranslate"><span class="pre">ValueError</span></code> is raised.</p></td>
  1621. </tr>
  1622. <tr class="row-even"><td><p><em>force</em></p></td>
  1623. <td><p>If this keyword argument is specified as
  1624. true, any existing handlers attached to the
  1625. root logger are removed and closed, before
  1626. carrying out the configuration as specified
  1627. by the other arguments.</p></td>
  1628. </tr>
  1629. <tr class="row-odd"><td><p><em>encoding</em></p></td>
  1630. <td><p>If this keyword argument is specified along
  1631. with <em>filename</em>, its value is used when the
  1632. <a class="reference internal" href="logging.handlers.html#logging.FileHandler" title="logging.FileHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileHandler</span></code></a> is created, and thus
  1633. used when opening the output file.</p></td>
  1634. </tr>
  1635. <tr class="row-even"><td><p><em>errors</em></p></td>
  1636. <td><p>If this keyword argument is specified along
  1637. with <em>filename</em>, its value is used when the
  1638. <a class="reference internal" href="logging.handlers.html#logging.FileHandler" title="logging.FileHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileHandler</span></code></a> is created, and thus
  1639. used when opening the output file. If not
  1640. specified, the value ‘backslashreplace’ is
  1641. used. Note that if <code class="docutils literal notranslate"><span class="pre">None</span></code> is specified,
  1642. it will be passed as such to <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>,
  1643. which means that it will be treated the
  1644. same as passing ‘errors’.</p></td>
  1645. </tr>
  1646. </tbody>
  1647. </table>
  1648. <div class="versionchanged">
  1649. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>style</em> argument was added.</p>
  1650. </div>
  1651. <div class="versionchanged">
  1652. <p><span class="versionmodified changed">Changed in version 3.3: </span>The <em>handlers</em> argument was added. Additional checks were added to
  1653. catch situations where incompatible arguments are specified (e.g.
  1654. <em>handlers</em> together with <em>stream</em> or <em>filename</em>, or <em>stream</em>
  1655. together with <em>filename</em>).</p>
  1656. </div>
  1657. <div class="versionchanged">
  1658. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>force</em> argument was added.</p>
  1659. </div>
  1660. <div class="versionchanged">
  1661. <p><span class="versionmodified changed">Changed in version 3.9: </span>The <em>encoding</em> and <em>errors</em> arguments were added.</p>
  1662. </div>
  1663. </dd></dl>
  1664. <dl class="py function">
  1665. <dt class="sig sig-object py" id="logging.shutdown">
  1666. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">shutdown</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#logging.shutdown" title="Link to this definition">¶</a></dt>
  1667. <dd><p>Informs the logging system to perform an orderly shutdown by flushing and
  1668. closing all handlers. This should be called at application exit and no
  1669. further use of the logging system should be made after this call.</p>
  1670. <p>When the logging module is imported, it registers this function as an exit
  1671. handler (see <a class="reference internal" href="atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a>), so normally there’s no need to do that
  1672. manually.</p>
  1673. </dd></dl>
  1674. <dl class="py function">
  1675. <dt class="sig sig-object py" id="logging.setLoggerClass">
  1676. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">setLoggerClass</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">klass</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.setLoggerClass" title="Link to this definition">¶</a></dt>
  1677. <dd><p>Tells the logging system to use the class <em>klass</em> when instantiating a logger.
  1678. The class should define <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> such that only a name argument is
  1679. required, and the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> should call <code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.__init__()</span></code>. This
  1680. function is typically called before any loggers are instantiated by applications
  1681. which need to use custom logger behavior. After this call, as at any other
  1682. time, do not instantiate loggers directly using the subclass: continue to use
  1683. the <a class="reference internal" href="#logging.getLogger" title="logging.getLogger"><code class="xref py py-func docutils literal notranslate"><span class="pre">logging.getLogger()</span></code></a> API to get your loggers.</p>
  1684. </dd></dl>
  1685. <dl class="py function">
  1686. <dt class="sig sig-object py" id="logging.setLogRecordFactory">
  1687. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">setLogRecordFactory</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">factory</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.setLogRecordFactory" title="Link to this definition">¶</a></dt>
  1688. <dd><p>Set a callable which is used to create a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a>.</p>
  1689. <dl class="field-list simple">
  1690. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1691. <dd class="field-odd"><p><strong>factory</strong> – The factory callable to be used to instantiate a log record.</p>
  1692. </dd>
  1693. </dl>
  1694. <div class="versionadded">
  1695. <p><span class="versionmodified added">New in version 3.2: </span>This function has been provided, along with <a class="reference internal" href="#logging.getLogRecordFactory" title="logging.getLogRecordFactory"><code class="xref py py-func docutils literal notranslate"><span class="pre">getLogRecordFactory()</span></code></a>, to
  1696. allow developers more control over how the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> representing
  1697. a logging event is constructed.</p>
  1698. </div>
  1699. <p>The factory has the following signature:</p>
  1700. <p><code class="docutils literal notranslate"><span class="pre">factory(name,</span> <span class="pre">level,</span> <span class="pre">fn,</span> <span class="pre">lno,</span> <span class="pre">msg,</span> <span class="pre">args,</span> <span class="pre">exc_info,</span> <span class="pre">func=None,</span> <span class="pre">sinfo=None,</span> <span class="pre">**kwargs)</span></code></p>
  1701. <blockquote>
  1702. <div><dl class="field-list simple">
  1703. <dt class="field-odd">name<span class="colon">:</span></dt>
  1704. <dd class="field-odd"><p>The logger name.</p>
  1705. </dd>
  1706. <dt class="field-even">level<span class="colon">:</span></dt>
  1707. <dd class="field-even"><p>The logging level (numeric).</p>
  1708. </dd>
  1709. <dt class="field-odd">fn<span class="colon">:</span></dt>
  1710. <dd class="field-odd"><p>The full pathname of the file where the logging call was made.</p>
  1711. </dd>
  1712. <dt class="field-even">lno<span class="colon">:</span></dt>
  1713. <dd class="field-even"><p>The line number in the file where the logging call was made.</p>
  1714. </dd>
  1715. <dt class="field-odd">msg<span class="colon">:</span></dt>
  1716. <dd class="field-odd"><p>The logging message.</p>
  1717. </dd>
  1718. <dt class="field-even">args<span class="colon">:</span></dt>
  1719. <dd class="field-even"><p>The arguments for the logging message.</p>
  1720. </dd>
  1721. <dt class="field-odd">exc_info<span class="colon">:</span></dt>
  1722. <dd class="field-odd"><p>An exception tuple, or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1723. </dd>
  1724. <dt class="field-even">func<span class="colon">:</span></dt>
  1725. <dd class="field-even"><p>The name of the function or method which invoked the logging
  1726. call.</p>
  1727. </dd>
  1728. <dt class="field-odd">sinfo<span class="colon">:</span></dt>
  1729. <dd class="field-odd"><p>A stack traceback such as is provided by
  1730. <a class="reference internal" href="traceback.html#traceback.print_stack" title="traceback.print_stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">traceback.print_stack()</span></code></a>, showing the call hierarchy.</p>
  1731. </dd>
  1732. <dt class="field-even">kwargs<span class="colon">:</span></dt>
  1733. <dd class="field-even"><p>Additional keyword arguments.</p>
  1734. </dd>
  1735. </dl>
  1736. </div></blockquote>
  1737. </dd></dl>
  1738. </section>
  1739. <section id="module-level-attributes">
  1740. <h2>Module-Level Attributes<a class="headerlink" href="#module-level-attributes" title="Link to this heading">¶</a></h2>
  1741. <dl class="py attribute">
  1742. <dt class="sig sig-object py" id="logging.lastResort">
  1743. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">lastResort</span></span><a class="headerlink" href="#logging.lastResort" title="Link to this definition">¶</a></dt>
  1744. <dd><p>A “handler of last resort” is available through this attribute. This
  1745. is a <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">StreamHandler</span></code></a> writing to <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> with a level of
  1746. <code class="docutils literal notranslate"><span class="pre">WARNING</span></code>, and is used to handle logging events in the absence of any
  1747. logging configuration. The end result is to just print the message to
  1748. <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>. This replaces the earlier error message saying that
  1749. “no handlers could be found for logger XYZ”. If you need the earlier
  1750. behaviour for some reason, <code class="docutils literal notranslate"><span class="pre">lastResort</span></code> can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1751. <div class="versionadded">
  1752. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1753. </div>
  1754. </dd></dl>
  1755. <dl class="py attribute">
  1756. <dt class="sig sig-object py" id="logging.raiseExceptions">
  1757. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">raiseExceptions</span></span><a class="headerlink" href="#logging.raiseExceptions" title="Link to this definition">¶</a></dt>
  1758. <dd><p>Used to see if exceptions during handling should be propagated.</p>
  1759. <p>Default: <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  1760. <p>If <a class="reference internal" href="#logging.raiseExceptions" title="logging.raiseExceptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">raiseExceptions</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>,
  1761. exceptions get silently ignored. This is what is mostly wanted
  1762. for a logging system - most users will not care about errors in
  1763. the logging system, they are more interested in application errors.</p>
  1764. </dd></dl>
  1765. </section>
  1766. <section id="integration-with-the-warnings-module">
  1767. <h2>Integration with the warnings module<a class="headerlink" href="#integration-with-the-warnings-module" title="Link to this heading">¶</a></h2>
  1768. <p>The <a class="reference internal" href="#logging.captureWarnings" title="logging.captureWarnings"><code class="xref py py-func docutils literal notranslate"><span class="pre">captureWarnings()</span></code></a> function can be used to integrate <a class="reference internal" href="#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>
  1769. with the <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> module.</p>
  1770. <dl class="py function">
  1771. <dt class="sig sig-object py" id="logging.captureWarnings">
  1772. <span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">captureWarnings</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">capture</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#logging.captureWarnings" title="Link to this definition">¶</a></dt>
  1773. <dd><p>This function is used to turn the capture of warnings by logging on and
  1774. off.</p>
  1775. <p>If <em>capture</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, warnings issued by the <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> module will
  1776. be redirected to the logging system. Specifically, a warning will be
  1777. formatted using <a class="reference internal" href="warnings.html#warnings.formatwarning" title="warnings.formatwarning"><code class="xref py py-func docutils literal notranslate"><span class="pre">warnings.formatwarning()</span></code></a> and the resulting string
  1778. logged to a logger named <code class="docutils literal notranslate"><span class="pre">'py.warnings'</span></code> with a severity of <a class="reference internal" href="#logging.WARNING" title="logging.WARNING"><code class="xref py py-const docutils literal notranslate"><span class="pre">WARNING</span></code></a>.</p>
  1779. <p>If <em>capture</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the redirection of warnings to the logging system
  1780. will stop, and warnings will be redirected to their original destinations
  1781. (i.e. those in effect before <code class="docutils literal notranslate"><span class="pre">captureWarnings(True)</span></code> was called).</p>
  1782. </dd></dl>
  1783. <div class="admonition seealso">
  1784. <p class="admonition-title">See also</p>
  1785. <dl class="simple">
  1786. <dt>Module <a class="reference internal" href="logging.config.html#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></dt><dd><p>Configuration API for the logging module.</p>
  1787. </dd>
  1788. <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>
  1789. </dd>
  1790. <dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0282/"><strong>PEP 282</strong></a> - A Logging System</dt><dd><p>The proposal which described this feature for inclusion in the Python standard
  1791. library.</p>
  1792. </dd>
  1793. <dt><a class="reference external" href="https://old.red-dove.com/python_logging.html">Original Python logging package</a></dt><dd><p>This is the original source for the <a class="reference internal" href="#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> package. The version of the
  1794. package available from this site is suitable for use with Python 1.5.2, 2.1.x
  1795. and 2.2.x, which do not include the <a class="reference internal" href="#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> package in the standard
  1796. library.</p>
  1797. </dd>
  1798. </dl>
  1799. </div>
  1800. </section>
  1801. </section>
  1802. <div class="clearer"></div>
  1803. </div>
  1804. </div>
  1805. </div>
  1806. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1807. <div class="sphinxsidebarwrapper">
  1808. <div>
  1809. <h3><a href="../contents.html">Table of Contents</a></h3>
  1810. <ul>
  1811. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a><ul>
  1812. <li><a class="reference internal" href="#logger-objects">Logger Objects</a></li>
  1813. <li><a class="reference internal" href="#logging-levels">Logging Levels</a></li>
  1814. <li><a class="reference internal" href="#handler-objects">Handler Objects</a></li>
  1815. <li><a class="reference internal" href="#formatter-objects">Formatter Objects</a></li>
  1816. <li><a class="reference internal" href="#filter-objects">Filter Objects</a></li>
  1817. <li><a class="reference internal" href="#logrecord-objects">LogRecord Objects</a></li>
  1818. <li><a class="reference internal" href="#logrecord-attributes">LogRecord attributes</a></li>
  1819. <li><a class="reference internal" href="#loggeradapter-objects">LoggerAdapter Objects</a></li>
  1820. <li><a class="reference internal" href="#thread-safety">Thread Safety</a></li>
  1821. <li><a class="reference internal" href="#module-level-functions">Module-Level Functions</a></li>
  1822. <li><a class="reference internal" href="#module-level-attributes">Module-Level Attributes</a></li>
  1823. <li><a class="reference internal" href="#integration-with-the-warnings-module">Integration with the warnings module</a></li>
  1824. </ul>
  1825. </li>
  1826. </ul>
  1827. </div>
  1828. <div>
  1829. <h4>Previous topic</h4>
  1830. <p class="topless"><a href="getopt.html"
  1831. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">getopt</span></code> — C-style parser for command line options</a></p>
  1832. </div>
  1833. <div>
  1834. <h4>Next topic</h4>
  1835. <p class="topless"><a href="logging.config.html"
  1836. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.config</span></code> — Logging configuration</a></p>
  1837. </div>
  1838. <div role="note" aria-label="source link">
  1839. <h3>This Page</h3>
  1840. <ul class="this-page-menu">
  1841. <li><a href="../bugs.html">Report a Bug</a></li>
  1842. <li>
  1843. <a href="https://github.com/python/cpython/blob/main/Doc/library/logging.rst"
  1844. rel="nofollow">Show Source
  1845. </a>
  1846. </li>
  1847. </ul>
  1848. </div>
  1849. </div>
  1850. <div id="sidebarbutton" title="Collapse sidebar">
  1851. <span>«</span>
  1852. </div>
  1853. </div>
  1854. <div class="clearer"></div>
  1855. </div>
  1856. <div class="related" role="navigation" aria-label="related navigation">
  1857. <h3>Navigation</h3>
  1858. <ul>
  1859. <li class="right" style="margin-right: 10px">
  1860. <a href="../genindex.html" title="General Index"
  1861. >index</a></li>
  1862. <li class="right" >
  1863. <a href="../py-modindex.html" title="Python Module Index"
  1864. >modules</a> |</li>
  1865. <li class="right" >
  1866. <a href="logging.config.html" title="logging.config — Logging configuration"
  1867. >next</a> |</li>
  1868. <li class="right" >
  1869. <a href="getopt.html" title="getopt — C-style parser for command line options"
  1870. >previous</a> |</li>
  1871. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1872. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1873. <li class="switchers">
  1874. <div class="language_switcher_placeholder"></div>
  1875. <div class="version_switcher_placeholder"></div>
  1876. </li>
  1877. <li>
  1878. </li>
  1879. <li id="cpython-language-and-version">
  1880. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1881. </li>
  1882. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1883. <li class="nav-item nav-item-2"><a href="allos.html" >Generic Operating System Services</a> &#187;</li>
  1884. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></li>
  1885. <li class="right">
  1886. <div class="inline-search" role="search">
  1887. <form class="inline-search" action="../search.html" method="get">
  1888. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1889. <input type="submit" value="Go" />
  1890. </form>
  1891. </div>
  1892. |
  1893. </li>
  1894. <li class="right">
  1895. <label class="theme-selector-label">
  1896. Theme
  1897. <select class="theme-selector" oninput="activateTheme(this.value)">
  1898. <option value="auto" selected>Auto</option>
  1899. <option value="light">Light</option>
  1900. <option value="dark">Dark</option>
  1901. </select>
  1902. </label> |</li>
  1903. </ul>
  1904. </div>
  1905. <div class="footer">
  1906. &copy;
  1907. <a href="../copyright.html">
  1908. Copyright
  1909. </a>
  1910. 2001-2024, Python Software Foundation.
  1911. <br />
  1912. This page is licensed under the Python Software Foundation License Version 2.
  1913. <br />
  1914. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1915. <br />
  1916. See <a href="/license.html">History and License</a> for more information.<br />
  1917. <br />
  1918. The Python Software Foundation is a non-profit corporation.
  1919. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1920. <br />
  1921. <br />
  1922. Last updated on Apr 09, 2024 (13:47 UTC).
  1923. <a href="/bugs.html">Found a bug</a>?
  1924. <br />
  1925. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1926. </div>
  1927. </body>
  1928. </html>
上海开阖软件有限公司 沪ICP备12045867号-1