1
0
wiki/Technology/ProgrammingLanguage/Python/模块/系统交互/logging-日志记录工具.html

540 lines
527 KiB
HTML
Raw Permalink Normal View History

2024-10-10 16:56:32 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>logging 日志记录工具</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="logging 日志记录工具"/><meta property="og:description" content="全面介绍了Python标准库logging模块的用法包括理论知识和代码示例."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../../static/icon.png"/><meta name="description" content="全面介绍了Python标准库logging模块的用法包括理论知识和代码示例."/><meta name="generator" content="Quartz"/><link href="../../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/ProgrammingLanguage/Python/模块/系统交互/logging-日志记录工具"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4
<p>**logging 模块是 Python 标准库里提供的日志功能组件,它是在开发过程中实现日志记录的推荐方式。**logging 模块主要包含四个组件Logger、Handler、Filter 和 Formatter。通过它们的协作可以快速实现对日志的控制和处理。</p>
<h2 id="组件">组件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#组件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="logger">Logger<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#logger" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>**Logger 是日志模块中最基本的组件,它负责生成和分类日志。**在应用代码中,我们可以通过 getLogger 方法获取 Logger 对象,主要有两种获取方式:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 1. 不指定 Logger 名称,返回 root Logger </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger1 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.getLogger()</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 2. 指定名称获取 Logger </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger2 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.getLogger(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'test'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<h3 id="handler">Handler<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#handler" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p><strong>Handler 用于指定 Logger 生成的日志消息应发送到哪个目标输出</strong>,常用的 Handler 有两种:</p>
<ul>
<li><strong>StreamHandler</strong>:输出到流,如 sys.stdout、sys.stderr</li>
<li><strong>FileHandler</strong>:输出到文件</li>
</ul>
<p>我们可以通过 Logger 对象的 addHandler 方法将 Handler 与 Logger 关联起来:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.getLogger(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'test'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建一个 Handler用于写入日志文件</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">fh </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.FileHandler(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'test.log'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 给 logger 添加 Handler</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.addHandler(fh)</span></span></code></pre></figure>
<h3 id="filter">Filter<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#filter" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>**Filter 用于提供复杂的日志过滤功能,可以用来过滤 INFO、DEBUG 等不同级别的日志。**根据需求,我们可以通过 logger.addFilter 将过滤器加到 logger 或者 handler 里。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 只记录 ERROR 及以上级别的日志</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ErrorFilter</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">logging</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Filter</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> filter</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, record):</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> record.levelno </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">>=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ERROR</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.addFilter(ErrorFilter())</span></span></code></pre></figure>
<h3 id="formatter">Formatter<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#formatter" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>**Formatter 用于控制日志信息的最终输出格式。**我们可以指定日志不同部分的显示格式,如时间、代码位置、日志消息等。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 指定日志格式</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">formatter </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.Formatter(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(levelname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(asctime)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(message)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">handler.setFormatter(formatter)</span></span></code></pre></figure>
<h2 id="基础配置">基础配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基础配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>我们可以通过字典或文件的方式配置 logging 模块。</p>
<h3 id="dictconfig-配置">dictConfig 配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#dictconfig-配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.config</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">config_dict </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'version'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'formatters'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'simple'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'format'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(levelname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(asctime)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(message)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> },</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'handlers'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'console'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'class'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'logging.StreamHandler'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'level'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'DEBUG'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'formatter'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'simple'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> },</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'file'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'class'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'logging.FileHandler'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'filename'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'test.log'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'formatter'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'simple'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> },</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'root'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'level'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'DEBUG'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'handlers'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'console'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'file'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logging.config.dictConfig(config_dict)</span></span></code></pre></figure>
<h3 id="fileconfig-配置">fileConfig 配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#fileconfig-配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>将配置字典写入 json 或 yaml 文件,通过 fileConfig 函数加载:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.config</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logging.config.fileConfig(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'config.yaml'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<h3 id="配置参数">配置参数<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置参数" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>logging 模块的配置参数较多,主要包括以下方面:</p>
<div class="table-container"><table><thead><tr><th style="text-align:left;">参数</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">version</td><td style="text-align:left;">配置字典的版本,目前为 1</td></tr><tr><td style="text-align:left;">disable_existing_loggers</td><td style="text-align:left;">是否禁用已存在的其他日志器,默认 False</td></tr><tr><td style="text-align:left;">formatters</td><td style="text-align:left;">定义日志信息的格式化样式</td></tr><tr><td style="text-align:left;">filters</td><td style="text-align:left;">定义过滤日志信息的过滤器</td></tr><tr><td style="text-align:left;">handlers</td><td style="text-align:left;">定义日志的处理方法以及目标位置</td></tr><tr><td style="text-align:left;">loggers</td><td style="text-align:left;">定义具体的日志器并绑定处理器和过滤器</td></tr><tr><td style="text-align:left;">root</td><td style="text-align:left;">根日志器配置,其配置会应用到所有日志器上</td></tr></tbody></table></div>
<p>loggers 配置参数:</p>
<div class="table-container"><table><thead><tr><th style="text-align:left;">参数</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">level</td><td style="text-align:left;">日志器接收的日志级别</td></tr><tr><td style="text-align:left;">handlers</td><td style="text-align:left;">绑定的日志处理器</td></tr><tr><td style="text-align:left;">filters</td><td style="text-align:left;">绑定的日志过滤器</td></tr><tr><td style="text-align:left;">propagate</td><td style="text-align:left;">是否传递给父日志器,默认 True</td></tr></tbody></table></div>
<p>handlers 常用参数:</p>
<div class="table-container"><table><thead><tr><th style="text-align:left;">参数</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">class</td><td style="text-align:left;">指定处理器类,如 FileHandler、StreamHandler 等</td></tr><tr><td style="text-align:left;">level</td><td style="text-align:left;">处理器的日志级别</td></tr><tr><td style="text-align:left;">formatter</td><td style="text-align:left;">用于格式化日志的 Formatter</td></tr><tr><td style="text-align:left;">args</td><td style="text-align:left;">处理器初始化参数,如文件名等</td></tr></tbody></table></div>
<p>formatters 参数:</p>
<div class="table-container"><table><thead><tr><th style="text-align:left;">参数</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">format</td><td style="text-align:left;">指定日志输出格式的模板</td></tr><tr><td style="text-align:left;">datefmt</td><td style="text-align:left;">格式化时间的格式字符串</td></tr><tr><td style="text-align:left;">style</td><td style="text-align:left;">输出样式,如 {$</td></tr></tbody></table></div>
<h2 id="模块用法">模块用法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#模块用法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<pre><code class="mermaid">graph TB
START(开始)-->Choose_Logger[选择Logger]
Choose_Logger-->|内置Logger|Use_Builtin[使用Python内置Logger]
Choose_Logger-->|自定义Logger|Define_Logger[定义自定义Logger]
Define_Logger-->Set_Level[设置日志级别&lt;br>如DEBUG,INFO等]
Define_Logger-->Create_Handler[创建Handler&lt;br>如FileHandler,&lt;br>StreamHandler等]
Set_Level-->Create_Handler
Create_Handler-->Set_Format[设置日志格式]
Set_Format-->Add_Handler[添加Handler到Logger]
Use_Builtin-->Set_Level
Use_Builtin-->Create_Handler
Add_Handler--> USE[使用Logger记录日志]
USE-->END(结束)
</code></pre>
<h3 id="日志级别">日志级别<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#日志级别" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>logging 模块总共定义了 6 个级别的日志,按照严重程度从低到高排列如下:</p>
<div class="table-container"><table><thead><tr><th style="text-align:left;">级别</th><th style="text-align:left;">数值</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">DEBUG</td><td style="text-align:left;">10</td><td style="text-align:left;">最详细的调试信息</td></tr><tr><td style="text-align:left;">INFO</td><td style="text-align:left;">20</td><td style="text-align:left;">确认一切运行正常</td></tr><tr><td style="text-align:left;">WARNING</td><td style="text-align:left;">30</td><td style="text-align:left;">表示出现异常,但尚未影响程序运行</td></tr><tr><td style="text-align:left;">ERROR</td><td style="text-align:left;">40</td><td style="text-align:left;">由于更严重的问题,程序的某些功能已经不能正常运行了</td></tr><tr><td style="text-align:left;">CRITICAL</td><td style="text-align:left;">50</td><td style="text-align:left;">严重错误,程序可能无法继续运行</td></tr></tbody></table></div>
<p>在代码中记录日志时,我们可以根据需要的日志级别选择合适的函数,比如:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logging.debug(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'detailed debug info'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logging.info(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'program running info'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logging.warning(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'warning occurred'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logging.error(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'error occurred'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logging.critical(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'critical error'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<p>日志级别可用于控制日志的输出粒度,通过设置级别参数可以只输出大于或等于该级别的日志。</p>
<h3 id="日志输出">日志输出<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#日志输出" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>logging 模块支持通过多种 Handler 将日志输出到不同的位置,常见的有两种:</p>
<h4 id="输出到控制台">输出到控制台<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#输出到控制台" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># create logger</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.getLogger(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'simple_example'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.setLevel(logging.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">DEBUG</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># create console handler and set level to debug</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ch </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.StreamHandler()</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ch.setLevel(logging.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">DEBUG</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># create formatter</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">formatter </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.Formatter(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(asctime)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(name)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(levelname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(message)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># add formatter to ch</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ch.setFormatter(formatter)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># add ch to logger</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.addHandler(ch)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 'application' code</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.debug(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'debug message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.info(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'info message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.warning(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'warn message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.error(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'error message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.critical(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'critical message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<h4 id="输出到文件">输出到文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#输出到文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建 logger</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.getLogger(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'simple_example'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.setLevel(logging.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">DEBUG</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建一个向文件输出的 handler</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">fh </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.FileHandler(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'test.log'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">fh.setLevel(logging.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">DEBUG</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建 formatter</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">formatter </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.Formatter(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(asctime)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(name)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(levelname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(message)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 添加 formatter 到 fh</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">fh.setFormatter(formatter) </span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 添加 fh 到 logger</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.addHandler(fh)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 日志输出</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.debug(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'debug message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.info(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'info message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.warning(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'warn message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.error(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'error message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.critical(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'critical message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<p>此外,还可以通过 SocketHandler 进行网络日志,通过 MemoryHandler 将日志输出到内存等。</p>
<h3 id="日志格式化">日志格式化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#日志格式化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>logging 模块允许自定义日志的输出格式,方便阅读和分析。主要通过 Formatter 实现。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 自定义格式</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 2023-02-28 155007431 - myapp.utils - INFO - User signed in successfully [in /path/to/utils.py456] </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">formatter </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.Formatter(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(asctime)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(name)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(levelname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(message)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> [in </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(pathname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"></span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(lineno)d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">]'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">handler.setFormatter(formatter)</span></span></code></pre></figure>
<p>Formatter 使用的格式字符串类似于 C 语言的 printf 格式,可以包含以下字段:</p>
<div class="table-container"><table><thead><tr><th style="text-align:left;">字段</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">asctime</td><td style="text-align:left;">日志事件发生时间,如 2023-02-28 154923895</td></tr><tr><td style="text-align:left;">levelname</td><td style="text-align:left;">日志级别的文本表示,如 INFO、ERROR 等</td></tr><tr><td style="text-align:left;">levelno</td><td style="text-align:left;">日志级别的数值,如 20、40 等</td></tr><tr><td style="text-align:left;">message</td><td style="text-align:left;">日志消息文本</td></tr><tr><td style="text-align:left;">pathname</td><td style="text-align:left;">调用日志输出函数的源代码文件路径</td></tr><tr><td style="text-align:left;">filename</td><td style="text-align:left;">pathname 的文件名部分</td></tr><tr><td style="text-align:left;">module</td><td style="text-align:left;">文件名的模块名部分</td></tr><tr><td style="text-align:left;">funcName</td><td style="text-align:left;">调用日志输出函数的函数名</td></tr><tr><td style="text-align:left;">lineno</td><td style="text-align:left;">调用日志输出函数的源代码行号</td></tr><tr><td style="text-align:left;">thread</td><td style="text-align:left;">线程 ID</td></tr><tr><td style="text-align:left;">process</td><td style="text-align:left;">进程 ID</td></tr></tbody></table></div>
<h3 id="日志高级功能">日志高级功能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#日志高级功能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="日志回滚">日志回滚<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#日志回滚" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>通过 RotatingFileHandler 可以实现日志文件回滚,即当日志文件达到一定大小时自动生成新文件。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">handler </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> RotatingFileHandler(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'test.log'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">maxBytes</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1024</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">backupCount</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<h4 id="日志分割">日志分割<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#日志分割" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>它可以设置最大字节数和备份文件的数量,超过这些限制之后就会生成新的日志文件。通过 TimedRotatingFileHandler 可以实现日志文件按时间分割。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">handler </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> TimedRotatingFileHandler(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'test.log'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">when</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'D'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">interval</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span></span></code></pre></figure>
<p>通过 when 参数可以设置按日、周、月等时间间隔进行切割interval 参数设置时间间隔大小。</p>
<h2 id="最佳实践">最佳实践<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#最佳实践" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="根据应用场景配置-logging-模块">根据应用场景配置 Logging 模块<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#根据应用场景配置-logging-模块" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>不同的 Python 应用场景需要不同的日志方案:</p>
<ul>
<li>小型命令行程序:直接输出到 stdout</li>
<li>大型项目:输出到日志文件,同时进行错误提醒、日志分割等</li>
<li>分布式系统:实现集中式日志,结合 ELK 等日志分析系统</li>
</ul>
<p>选择合适的 logging 功能组合。</p>
<h3 id="与项目架构相结合">与项目架构相结合<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#与项目架构相结合" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>logging 模块要与项目的架构设计相结合,比如:</p>
<ul>
<li>对 Django 项目进行专门的 Logging 设置</li>
<li>对 Flask 项目使用 dictsConfig 进行配置</li>
<li>对于大型项目,创建一个 log.py 统一处理日志</li>
</ul>
<h3 id="可选的日志收集工具">可选的日志收集工具<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#可选的日志收集工具" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>可以结合日志收集系统,实现可视化分析,如 ELK(ElasticSearch + Logstash + Kibana) 组合。</p>
<h3 id="日志的性能优化">日志的性能优化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#日志的性能优化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<ul>
<li>通过配置只记录必要的日志级别,不记录无用日志</li>
<li>将调试日志单独记录到一个文件</li>
<li>避免频繁字符串拼接,使用参数化格式</li>
<li>控制日志文件大小和滚动频率</li>
</ul>
<h2 id="实战练习">实战练习<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#实战练习" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="标准日志模块的配置示范">标准日志模块的配置示范<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#标准日志模块的配置示范" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># log_config.py</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.config</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 日志配置字典</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">config_dict </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 版本配置</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'version'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 配置日志格式化器</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'formatters'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 简单日志格式</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'simple'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'format'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(asctime)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(levelname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(message)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> },</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 复杂日志格式 </span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'complex'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'format'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(asctime)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(levelname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(name)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(filename)s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %(funcName)s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %(lineno)d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(message)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> },</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 配置日志处理器</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'handlers'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 控制台日志</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'console'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'class'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'logging.StreamHandler'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'level'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'DEBUG'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'formatter'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'simple'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> },</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 文件日志</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'file'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'class'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'logging.FileHandler'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'filename'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'app.log'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'mode'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'a'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'formatter'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'complex'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> },</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 配置日志器</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'loggers'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 应用日志器</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'app'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'handlers'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'console'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'file'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">],</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'level'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'DEBUG'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">if</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __name__</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ==</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> '__main__'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 加载配置字典</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.config.dictConfig(config_dict)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 获取日志器并输出日志</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.getLogger(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'app'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logger.debug(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'This is a debug message'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<p>以上实现了一个标准的日志配置,输出到控制台和文件,并使用不同的格式器,包含日志实践中的常见操作。</p>
<h3 id="小型项目中添加日志">小型项目中添加日志<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#小型项目中添加日志" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>以一个小型的 Web 项目为例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建日志器 </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.getLogger(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'myweb'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建控制台处理器并设置级别</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ch </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.StreamHandler()</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ch.setLevel(logging.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">INFO</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 添加格式器 </span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">formatter </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> logging.Formatter(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(asctime)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(name)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(levelname)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%(message)s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ch.setFormatter(formatter)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将处理器添加到日志器</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.addHandler(ch)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 记录一条日志</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">logger.info(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Starting myweb server...'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span></span></code></pre></figure>
<p>代码中创建日志器、添加处理器、设置格式后,就可以在项目代码任何地方使用 logger.info、logger.debug 等记录日志,从而方便地添加日志功能。</p>
<h3 id="尝试优化项目日志的性能">尝试优化项目日志的性能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#尝试优化项目日志的性能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>优化日志性能的常见做法:</p>
<ul>
<li>通过配置文件配置日志,不要在代码里反复配置</li>
<li>设置生产环境只记录必要的日志级别,如 WARNING 以上</li>
<li>ERROR 日志单独记录到文件,便于定位问题</li>
<li>DEBUG 日志可以使用内存 Handler避免 IO 消耗</li>
<li>使用异步日志,避免影响程序性能</li>
<li>合理设置日志滚动策略,避免文件过大</li>
</ul>
<h2 id="扩展学习">扩展学习<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#扩展学习" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="其他日志框架的比较-logurulog4j-等">其他日志框架的比较 (Loguru、log4j 等)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#其他日志框架的比较-logurulog4j-等" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>logging 模块作为标准库,用法简单,但需要自行处理诸如异步、日志分割等额外功能。一些第三方日志库如 loguru、log4j 等提供了更多实用功能的封装,可以根据需求选择。</p>
<h3 id="分布式系统日志收集方案">分布式系统日志收集方案<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#分布式系统日志收集方案" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>在分布式系统中,可以通过将日志发送到消息队列如 Kafka然后由 Logstash 收集存储到 ElasticSearch 中,最后结合 Kibana 进行日志分析和可视化。也可以直接使用 ELK 提供的 Filebeat 代替 Logstash 从日志文件中收集日志。</p></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#简介" data-for="简介">简介</a></li><li class="depth-0"><a href="#组件" data-for="组件">组件</a></li><li class="depth-1"><a href="#logger" data-for="logger">Logger</a></li><li class="depth-1"><a href="#handler" data-for="handler">Handler</a></li><li class="depth-1"><a href="#filter" data-for="filter">Filter</a></li><li class="depth-1"><a href="#formatter" data-for="formatter">Formatter</a></li><li class="depth-0"><a href="#基础配置" data-for="基础配置">基础配置</a></li><li class="depth-1"><a href="#dictconfig-配置" data-for="dictconfig-配置">dictConfig 配置</a></li><li class="depth-1"><a href="#fileconfig-配置" data-for="fileconfig-配置">fileConfig 配置</a></li><li class="depth-1"><a href="#配置参数" data-for="配置参数">配置参数</a></li><li class="depth-0"><a href="#模块用法" data-for="模块用法">模块用法</a></li><li class="depth-1"><a href="#日志级别" data-for="日志级别">日志级别</a></li><li class="depth-1"><a href="#日志输出" data-for="日志输出">日志输出</a></li><li class="depth-1"><a href="#日志格式化" data-for="日志格式化">日志格式化</a></li><li class="depth-1"><a href="#日志高级功能" data-for="日志高级功能">日志高级功能</a></li><li class="depth-0"><a href="#最佳实践" data-for="最佳实践">最佳实践</a></li><li class="depth-1"><a href="#根据应用场景配置-logging-模块" data-for="根据应用场景配置-logging-模块">根据应用场景配置 Logging 模块</a></li><li class="depth-1"><a href="#与项目架构相结合" data-for="与项目架构相结合">与项目架构相结合</a></li><li class="depth-1"><a href="#可选的日志收集工具" data-for="可选的日志收集工具">可选的日志收集工具</a></li><li class="depth-1"><a href="#日志的性能优化" data-for="日志的性能优化">日志的性能优化</a></li><li class="depth-0"><a href="#实战练习" data-for="实战练习">实战练习</a></li><li class="depth-1"><a href="#标准日志模块的配置示范" data-for="标准日志模块的配置示范">标准日志模块的配置示范</a></li><li class="depth-1"><a href="#小型项目中添加日志" data-for="小型项目中添加日志">小型项目中添加日志</a></li><li class="depth-1"><a href="#尝试优化项目日志的性能" data-for="尝试优化项目日志的性能">尝试优化项目日志的性能</a></li><li class="depth-0"><a href="#扩展学习" data-for="扩展学习">扩展学习</a></li><li class="depth-1"><a href="#其他日志框架的比较-logurulog4j-等" data-for="其他日志框架的比较-logurulog4j-等">其他日志框架的比较 (Loguru、log4j 等)</a></li><li class="depth-1"><a href="#分布式系统日志收集方案" data-for="分布式系统日志收集方案">分布式系统日志收集方案</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&quot;path&quot;:&quot;Personal&quot;,&quot;coll
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../../postscript.js" type="module"></script></html>