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

588 lines
532 KiB
HTML
Raw Normal View History

2024-09-03 21:23:28 +08:00
<!DOCTYPE html>
<html><head><title>logging 日志记录工具</title><meta charSet="utf-8"/><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 rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><script async src="https://umami.7wate.com/script.js" data-website-id="c061efdc-95dd-4d21-9d04-a1ffda0a85b9"></script><script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?94d8ccb156eb7c65abf317e6e01cdba9";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-MHMEL0F832"></script><script>
(function() {
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-MHMEL0F832');
})();
</script><link href="../../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Schibsted Grotesk:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap" 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"><h1 class="page-title "><a href="../../../../..">📚 X·Eden</a></h1><div class="spacer mobile-only"></div><div class="search "><div id="search-icon"><p>Search</p><div></div><svg tabIndex="0" aria-labelledby="title desc" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title id="title">Search</title><desc id="desc">Search</desc><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></div><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search for something" placeholder="Search for something"/><div id="results-container"></div></div></div></div><div class="darkmode "><input class="toggle" id="darkmode-toggle" type="checkbox" tabIndex="-1"/><label id="toggle-label-light" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="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;" xmlSpace="preserve"><title>Light mode</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.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg></label><label id="toggle-label-dark" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-background='new 0 0 100 100'" xmlSpace="preserve"><title>Dark mode</title><path d="M96.76,66.458c-0.853-0.852-2.15-1.064-3.23-0.534c-6.063,2.991-12.858,4.571-19.655,4.571 C62.022,70.495,50.88,65.88,42.5,57.5C29.043,44.043,25.658,23.536,34.076,6.47c0.532-1.08,0.318-2.379-0.534-3.23 c-0.851-0.852-2.15-1.064-3.23-0.534c-4.918,2.427-9.375,5.619-
<p>**logging 模块是 Python 标准库里提供的日志功能组件,它是在开发过程中实现日志记录的推荐方式。**logging 模块主要包含四个组件Logger、Handler、Filter 和 Formatter。通过它们的协作可以快速实现对日志的控制和处理。</p>
<h2 id="组件">组件<a aria-hidden="true" tabindex="-1" href="#组件" class="internal"> §</a></h2>
<h3 id="logger">Logger<a aria-hidden="true" tabindex="-1" href="#logger" class="internal"> §</a></h3>
<p>**Logger 是日志模块中最基本的组件,它负责生成和分类日志。**在应用代码中,我们可以通过 getLogger 方法获取 Logger 对象,主要有两种获取方式:</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 1. 不指定 Logger 名称,返回 root Logger </span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">getLogger</span><span style="color:var(--shiki-token-punctuation);">()</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 2. 指定名称获取 Logger </span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">getLogger</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'test'</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
<h3 id="handler">Handler<a aria-hidden="true" tabindex="-1" href="#handler" class="internal"> §</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>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">logger </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">getLogger</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'test'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 创建一个 Handler用于写入日志文件</span></span>
<span data-line><span style="color:var(--shiki-color-text);">fh </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">FileHandler</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'test.log'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 给 logger 添加 Handler</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">addHandler</span><span style="color:var(--shiki-token-punctuation);">(fh)</span></span></code></pre></div>
<h3 id="filter">Filter<a aria-hidden="true" tabindex="-1" href="#filter" class="internal"> §</a></h3>
<p>**Filter 用于提供复杂的日志过滤功能,可以用来过滤 INFO、DEBUG 等不同级别的日志。**根据需求,我们可以通过 logger.addFilter 将过滤器加到 logger 或者 handler 里。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 只记录 ERROR 及以上级别的日志</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">class</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">ErrorFilter</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-function);">logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">Filter</span><span style="color:var(--shiki-color-text);">):</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">def</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">filter</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-parameter);">self</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-parameter);">record</span><span style="color:var(--shiki-color-text);">):</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">return</span><span style="color:var(--shiki-color-text);"> record</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">levelno </span><span style="color:var(--shiki-token-keyword);">>=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">ERROR</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">addFilter</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-function);">ErrorFilter</span><span style="color:var(--shiki-token-punctuation);">())</span></span></code></pre></div>
<h3 id="formatter">Formatter<a aria-hidden="true" tabindex="-1" href="#formatter" class="internal"> §</a></h3>
<p>**Formatter 用于控制日志信息的最终输出格式。**我们可以指定日志不同部分的显示格式,如时间、代码位置、日志消息等。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 指定日志格式</span></span>
<span data-line><span style="color:var(--shiki-color-text);">formatter </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">Formatter</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'[</span><span style="color:var(--shiki-token-constant);">%(levelname)s</span><span style="color:var(--shiki-token-string-expression);">] </span><span style="color:var(--shiki-token-constant);">%(asctime)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(message)s</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);">handler</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setFormatter</span><span style="color:var(--shiki-token-punctuation);">(formatter)</span></span></code></pre></div>
<h2 id="基础配置">基础配置<a aria-hidden="true" tabindex="-1" href="#基础配置" class="internal"> §</a></h2>
<p>我们可以通过字典或文件的方式配置 logging 模块。</p>
<h3 id="dictconfig-配置">dictConfig 配置<a aria-hidden="true" tabindex="-1" href="#dictconfig-配置" class="internal"> §</a></h3>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">config</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);">config_dict </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'version'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'formatters'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'simple'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'format'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'[</span><span style="color:var(--shiki-token-constant);">%(levelname)s</span><span style="color:var(--shiki-token-string-expression);">] </span><span style="color:var(--shiki-token-constant);">%(asctime)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(message)s</span><span style="color:var(--shiki-token-string-expression);">'</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">},</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'handlers'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'console'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'class'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'logging.StreamHandler'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'level'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'DEBUG'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'formatter'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'simple'</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">},</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'file'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'class'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'logging.FileHandler'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'filename'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'test.log'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'formatter'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'simple'</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">},</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'root'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'level'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'DEBUG'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'handlers'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-string-expression);">'console'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'file'</span><span style="color:var(--shiki-color-text);">]</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);">logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">config</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">dictConfig</span><span style="color:var(--shiki-token-punctuation);">(config_dict)</span></span></code></pre></div>
<h3 id="fileconfig-配置">fileConfig 配置<a aria-hidden="true" tabindex="-1" href="#fileconfig-配置" class="internal"> §</a></h3>
<p>将配置字典写入 json 或 yaml 文件,通过 fileConfig 函数加载:</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">config</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);">logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">config</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">fileConfig</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'config.yaml'</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
<h3 id="配置参数">配置参数<a aria-hidden="true" tabindex="-1" href="#配置参数" class="internal"> §</a></h3>
<p>logging 模块的配置参数较多,主要包括以下方面:</p>
<table><thead><tr><th align="left">参数</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left">version</td><td align="left">配置字典的版本,目前为 1</td></tr><tr><td align="left">disable_existing_loggers</td><td align="left">是否禁用已存在的其他日志器,默认 False</td></tr><tr><td align="left">formatters</td><td align="left">定义日志信息的格式化样式</td></tr><tr><td align="left">filters</td><td align="left">定义过滤日志信息的过滤器</td></tr><tr><td align="left">handlers</td><td align="left">定义日志的处理方法以及目标位置</td></tr><tr><td align="left">loggers</td><td align="left">定义具体的日志器并绑定处理器和过滤器</td></tr><tr><td align="left">root</td><td align="left">根日志器配置,其配置会应用到所有日志器上</td></tr></tbody></table>
<p>loggers 配置参数:</p>
<table><thead><tr><th align="left">参数</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left">level</td><td align="left">日志器接收的日志级别</td></tr><tr><td align="left">handlers</td><td align="left">绑定的日志处理器</td></tr><tr><td align="left">filters</td><td align="left">绑定的日志过滤器</td></tr><tr><td align="left">propagate</td><td align="left">是否传递给父日志器,默认 True</td></tr></tbody></table>
<p>handlers 常用参数:</p>
<table><thead><tr><th align="left">参数</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left">class</td><td align="left">指定处理器类,如 FileHandler、StreamHandler 等</td></tr><tr><td align="left">level</td><td align="left">处理器的日志级别</td></tr><tr><td align="left">formatter</td><td align="left">用于格式化日志的 Formatter</td></tr><tr><td align="left">args</td><td align="left">处理器初始化参数,如文件名等</td></tr></tbody></table>
<p>formatters 参数:</p>
<table><thead><tr><th align="left">参数</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left">format</td><td align="left">指定日志输出格式的模板</td></tr><tr><td align="left">datefmt</td><td align="left">格式化时间的格式字符串</td></tr><tr><td align="left">style</td><td align="left">输出样式,如 {$</td></tr></tbody></table>
<h2 id="模块用法">模块用法<a aria-hidden="true" tabindex="-1" href="#模块用法" class="internal"> §</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 aria-hidden="true" tabindex="-1" href="#日志级别" class="internal"> §</a></h3>
<p>logging 模块总共定义了 6 个级别的日志,按照严重程度从低到高排列如下:</p>
<table><thead><tr><th align="left">级别</th><th align="left">数值</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left">DEBUG</td><td align="left">10</td><td align="left">最详细的调试信息</td></tr><tr><td align="left">INFO</td><td align="left">20</td><td align="left">确认一切运行正常</td></tr><tr><td align="left">WARNING</td><td align="left">30</td><td align="left">表示出现异常,但尚未影响程序运行</td></tr><tr><td align="left">ERROR</td><td align="left">40</td><td align="left">由于更严重的问题,程序的某些功能已经不能正常运行了</td></tr><tr><td align="left">CRITICAL</td><td align="left">50</td><td align="left">严重错误,程序可能无法继续运行</td></tr></tbody></table>
<p>在代码中记录日志时,我们可以根据需要的日志级别选择合适的函数,比如:</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);">logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">debug</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'detailed debug info'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">info</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'program running info'</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);">logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">warning</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'warning occurred'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">error</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'error occurred'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">critical</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'critical error'</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
<p>日志级别可用于控制日志的输出粒度,通过设置级别参数可以只输出大于或等于该级别的日志。</p>
<h3 id="日志输出">日志输出<a aria-hidden="true" tabindex="-1" href="#日志输出" class="internal"> §</a></h3>
<p>logging 模块支持通过多种 Handler 将日志输出到不同的位置,常见的有两种:</p>
<h4 id="输出到控制台">输出到控制台<a aria-hidden="true" tabindex="-1" href="#输出到控制台" class="internal"> §</a></h4>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># create logger</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">getLogger</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'simple_example'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setLevel</span><span style="color:var(--shiki-token-punctuation);">(logging.DEBUG)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># create console handler and set level to debug</span></span>
<span data-line><span style="color:var(--shiki-color-text);">ch </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">StreamHandler</span><span style="color:var(--shiki-token-punctuation);">()</span></span>
<span data-line><span style="color:var(--shiki-color-text);">ch</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setLevel</span><span style="color:var(--shiki-token-punctuation);">(logging.DEBUG)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># create formatter</span></span>
<span data-line><span style="color:var(--shiki-color-text);">formatter </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">Formatter</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-constant);">%(asctime)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(name)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(levelname)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(message)s</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># add formatter to ch</span></span>
<span data-line><span style="color:var(--shiki-color-text);">ch</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setFormatter</span><span style="color:var(--shiki-token-punctuation);">(formatter)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># add ch to logger</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">addHandler</span><span style="color:var(--shiki-token-punctuation);">(ch)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 'application' code</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">debug</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'debug message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">info</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'info message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">warning</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'warn message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">error</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'error message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">critical</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'critical message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
<h4 id="输出到文件">输出到文件<a aria-hidden="true" tabindex="-1" href="#输出到文件" class="internal"> §</a></h4>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 创建 logger</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">getLogger</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'simple_example'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setLevel</span><span style="color:var(--shiki-token-punctuation);">(logging.DEBUG)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 创建一个向文件输出的 handler</span></span>
<span data-line><span style="color:var(--shiki-color-text);">fh </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">FileHandler</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'test.log'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">fh</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setLevel</span><span style="color:var(--shiki-token-punctuation);">(logging.DEBUG)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 创建 formatter</span></span>
<span data-line><span style="color:var(--shiki-color-text);">formatter </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">Formatter</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-constant);">%(asctime)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(name)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(levelname)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(message)s</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 添加 formatter 到 fh</span></span>
<span data-line><span style="color:var(--shiki-color-text);">fh</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setFormatter</span><span style="color:var(--shiki-token-punctuation);">(formatter)</span><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 添加 fh 到 logger</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">addHandler</span><span style="color:var(--shiki-token-punctuation);">(fh)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 日志输出</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">debug</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'debug message'</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">info</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'info message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">warning</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'warn message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">error</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'error message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">critical</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'critical message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
<p>此外,还可以通过 SocketHandler 进行网络日志,通过 MemoryHandler 将日志输出到内存等。</p>
<h3 id="日志格式化">日志格式化<a aria-hidden="true" tabindex="-1" href="#日志格式化" class="internal"> §</a></h3>
<p>logging 模块允许自定义日志的输出格式,方便阅读和分析。主要通过 Formatter 实现。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 自定义格式</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 2023-02-28 155007431 - myapp.utils - INFO - User signed in successfully [in /path/to/utils.py456] </span></span>
<span data-line><span style="color:var(--shiki-color-text);">formatter </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">Formatter</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-constant);">%(asctime)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(name)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(levelname)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(message)s</span><span style="color:var(--shiki-token-string-expression);"> [in </span><span style="color:var(--shiki-token-constant);">%(pathname)s</span><span style="color:var(--shiki-token-string-expression);"></span><span style="color:var(--shiki-token-constant);">%(lineno)d</span><span style="color:var(--shiki-token-string-expression);">]'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);">handler</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setFormatter</span><span style="color:var(--shiki-token-punctuation);">(formatter)</span></span></code></pre></div>
<p>Formatter 使用的格式字符串类似于 C 语言的 printf 格式,可以包含以下字段:</p>
<table><thead><tr><th align="left">字段</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left">asctime</td><td align="left">日志事件发生时间,如 2023-02-28 154923895</td></tr><tr><td align="left">levelname</td><td align="left">日志级别的文本表示,如 INFO、ERROR 等</td></tr><tr><td align="left">levelno</td><td align="left">日志级别的数值,如 20、40 等</td></tr><tr><td align="left">message</td><td align="left">日志消息文本</td></tr><tr><td align="left">pathname</td><td align="left">调用日志输出函数的源代码文件路径</td></tr><tr><td align="left">filename</td><td align="left">pathname 的文件名部分</td></tr><tr><td align="left">module</td><td align="left">文件名的模块名部分</td></tr><tr><td align="left">funcName</td><td align="left">调用日志输出函数的函数名</td></tr><tr><td align="left">lineno</td><td align="left">调用日志输出函数的源代码行号</td></tr><tr><td align="left">thread</td><td align="left">线程 ID</td></tr><tr><td align="left">process</td><td align="left">进程 ID</td></tr></tbody></table>
<h3 id="日志高级功能">日志高级功能<a aria-hidden="true" tabindex="-1" href="#日志高级功能" class="internal"> §</a></h3>
<h4 id="日志回滚">日志回滚<a aria-hidden="true" tabindex="-1" href="#日志回滚" class="internal"> §</a></h4>
<p>通过 RotatingFileHandler 可以实现日志文件回滚,即当日志文件达到一定大小时自动生成新文件。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">handler </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">RotatingFileHandler</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'test.log'</span><span style="color:var(--shiki-token-punctuation);">, maxBytes</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-constant);">1024</span><span style="color:var(--shiki-token-punctuation);">, backupCount</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
<h4 id="日志分割">日志分割<a aria-hidden="true" tabindex="-1" href="#日志分割" class="internal"> §</a></h4>
<p>它可以设置最大字节数和备份文件的数量,超过这些限制之后就会生成新的日志文件。通过 TimedRotatingFileHandler 可以实现日志文件按时间分割。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">handler </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">TimedRotatingFileHandler</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'test.log'</span><span style="color:var(--shiki-token-punctuation);">, when</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'D'</span><span style="color:var(--shiki-token-punctuation);">, interval</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span></span></code></pre></div>
<p>通过 when 参数可以设置按日、周、月等时间间隔进行切割interval 参数设置时间间隔大小。</p>
<h2 id="最佳实践">最佳实践<a aria-hidden="true" tabindex="-1" href="#最佳实践" class="internal"> §</a></h2>
<h3 id="根据应用场景配置-logging-模块">根据应用场景配置 Logging 模块<a aria-hidden="true" tabindex="-1" href="#根据应用场景配置-logging-模块" class="internal"> §</a></h3>
<p>不同的 Python 应用场景需要不同的日志方案:</p>
<ul>
<li>小型命令行程序:直接输出到 stdout</li>
<li>大型项目:输出到日志文件,同时进行错误提醒、日志分割等</li>
<li>分布式系统:实现集中式日志,结合 ELK 等日志分析系统</li>
</ul>
<p>选择合适的 logging 功能组合。</p>
<h3 id="与项目架构相结合">与项目架构相结合<a aria-hidden="true" tabindex="-1" href="#与项目架构相结合" class="internal"> §</a></h3>
<p>logging 模块要与项目的架构设计相结合,比如:</p>
<ul>
<li>对 Django 项目进行专门的 Logging 设置</li>
<li>对 Flask 项目使用 dictsConfig 进行配置</li>
<li>对于大型项目,创建一个 log.py 统一处理日志</li>
</ul>
<h3 id="可选的日志收集工具">可选的日志收集工具<a aria-hidden="true" tabindex="-1" href="#可选的日志收集工具" class="internal"> §</a></h3>
<p>可以结合日志收集系统,实现可视化分析,如 ELK(ElasticSearch + Logstash + Kibana) 组合。</p>
<h3 id="日志的性能优化">日志的性能优化<a aria-hidden="true" tabindex="-1" href="#日志的性能优化" class="internal"> §</a></h3>
<ul>
<li>通过配置只记录必要的日志级别,不记录无用日志</li>
<li>将调试日志单独记录到一个文件</li>
<li>避免频繁字符串拼接,使用参数化格式</li>
<li>控制日志文件大小和滚动频率</li>
</ul>
<h2 id="实战练习">实战练习<a aria-hidden="true" tabindex="-1" href="#实战练习" class="internal"> §</a></h2>
<h3 id="标准日志模块的配置示范">标准日志模块的配置示范<a aria-hidden="true" tabindex="-1" href="#标准日志模块的配置示范" class="internal"> §</a></h3>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># log_config.py</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">config</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 日志配置字典</span></span>
<span data-line><span style="color:var(--shiki-color-text);">config_dict </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 版本配置</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'version'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 配置日志格式化器</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'formatters'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 简单日志格式</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'simple'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'format'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'[</span><span style="color:var(--shiki-token-constant);">%(asctime)s</span><span style="color:var(--shiki-token-string-expression);">] [</span><span style="color:var(--shiki-token-constant);">%(levelname)s</span><span style="color:var(--shiki-token-string-expression);">] </span><span style="color:var(--shiki-token-constant);">%(message)s</span><span style="color:var(--shiki-token-string-expression);">'</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">},</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 复杂日志格式 </span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'complex'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'format'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'[</span><span style="color:var(--shiki-token-constant);">%(asctime)s</span><span style="color:var(--shiki-token-string-expression);">] [</span><span style="color:var(--shiki-token-constant);">%(levelname)s</span><span style="color:var(--shiki-token-string-expression);">] [</span><span style="color:var(--shiki-token-constant);">%(name)s</span><span style="color:var(--shiki-token-string-expression);">] [</span><span style="color:var(--shiki-token-constant);">%(filename)s</span><span style="color:var(--shiki-token-string-expression);"> </span><span style="color:var(--shiki-token-constant);">%(funcName)s</span><span style="color:var(--shiki-token-string-expression);"> </span><span style="color:var(--shiki-token-constant);">%(lineno)d</span><span style="color:var(--shiki-token-string-expression);">] </span><span style="color:var(--shiki-token-constant);">%(message)s</span><span style="color:var(--shiki-token-string-expression);">'</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">},</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 配置日志处理器</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'handlers'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 控制台日志</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'console'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'class'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'logging.StreamHandler'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'level'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'DEBUG'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'formatter'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'simple'</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">},</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 文件日志</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'file'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'class'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'logging.FileHandler'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'filename'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'app.log'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'mode'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'a'</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'formatter'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'complex'</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">},</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 配置日志器</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'loggers'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 应用日志器</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'app'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'handlers'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-string-expression);">'console'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'file'</span><span style="color:var(--shiki-color-text);">]</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'level'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'DEBUG'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line><span style="color:var(--shiki-token-punctuation);">}</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">__name__</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">==</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'__main__'</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 加载配置字典</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">config</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">dictConfig</span><span style="color:var(--shiki-token-punctuation);">(config_dict)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 获取日志器并输出日志</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> logger </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">getLogger</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'app'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">debug</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'This is a debug message'</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
<p>以上实现了一个标准的日志配置,输出到控制台和文件,并使用不同的格式器,包含日志实践中的常见操作。</p>
<h3 id="小型项目中添加日志">小型项目中添加日志<a aria-hidden="true" tabindex="-1" href="#小型项目中添加日志" class="internal"> §</a></h3>
<p>以一个小型的 Web 项目为例:</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> logging</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 创建日志器 </span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">getLogger</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'myweb'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 创建控制台处理器并设置级别</span></span>
<span data-line><span style="color:var(--shiki-color-text);">ch </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">StreamHandler</span><span style="color:var(--shiki-token-punctuation);">()</span></span>
<span data-line><span style="color:var(--shiki-color-text);">ch</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setLevel</span><span style="color:var(--shiki-token-punctuation);">(logging.INFO)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 添加格式器 </span></span>
<span data-line><span style="color:var(--shiki-color-text);">formatter </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> logging</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">Formatter</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-constant);">%(asctime)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(name)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(levelname)s</span><span style="color:var(--shiki-token-string-expression);"> - </span><span style="color:var(--shiki-token-constant);">%(message)s</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);">ch</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">setFormatter</span><span style="color:var(--shiki-token-punctuation);">(formatter)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 将处理器添加到日志器</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">addHandler</span><span style="color:var(--shiki-token-punctuation);">(ch)</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-comment);"># 记录一条日志</span></span>
<span data-line><span style="color:var(--shiki-color-text);">logger</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">info</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">'Starting myweb server...'</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span></span></code></pre></div>
<p>代码中创建日志器、添加处理器、设置格式后,就可以在项目代码任何地方使用 logger.info、logger.debug 等记录日志,从而方便地添加日志功能。</p>
<h3 id="尝试优化项目日志的性能">尝试优化项目日志的性能<a aria-hidden="true" tabindex="-1" href="#尝试优化项目日志的性能" class="internal"> §</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 aria-hidden="true" tabindex="-1" href="#扩展学习" class="internal"> §</a></h2>
<h3 id="其他日志框架的比较-logurulog4j-等">其他日志框架的比较 (Loguru、log4j 等)<a aria-hidden="true" tabindex="-1" href="#其他日志框架的比较-logurulog4j-等" class="internal"> §</a></h3>
<p>logging 模块作为标准库,用法简单,但需要自行处理诸如异步、日志分割等额外功能。一些第三方日志库如 loguru、log4j 等提供了更多实用功能的封装,可以根据需求选择。</p>
<h3 id="分布式系统日志收集方案">分布式系统日志收集方案<a aria-hidden="true" tabindex="-1" href="#分布式系统日志收集方案" class="internal"> §</a></h3>
<p>在分布式系统中,可以通过将日志发送到消息队列如 Kafka然后由 Logstash 收集存储到 ElasticSearch 中,最后结合 Kibana 进行日志分析和可视化。也可以直接使用 ELK 提供的 Filebeat 代替 Logstash 从日志文件中收集日志。</p></article></div><div class="right sidebar"><div class="graph "><h3>Graph View</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;:[]}"></div><svg version="1.1" id="global-graph-icon" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xmlSpace="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></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;:[]}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class><h3>Table of Contents</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"><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="backlinks "><h3>Backlinks</h3><ul class="overflow"><li>No backlinks found</li></ul></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;Journal&quot;,&quot;co
function toggleCallout() {
const outerBlock = this.parentElement;
outerBlock.classList.toggle(`is-collapsed`);
const collapsed = outerBlock.classList.contains(`is-collapsed`);
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight;
outerBlock.style.maxHeight = height + `px`;
let current = outerBlock;
let parent = outerBlock.parentElement;
while (parent) {
if (!parent.classList.contains(`callout`)) {
return;
}
const collapsed2 = parent.classList.contains(`is-collapsed`);
const height2 = collapsed2 ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight;
parent.style.maxHeight = height2 + `px`;
current = parent;
parent = parent.parentElement;
}
}
function setupCallout() {
const collapsible = document.getElementsByClassName(
`callout is-collapsible`
);
for (const div of collapsible) {
const title = div.firstElementChild;
if (title) {
title.removeEventListener(`click`, toggleCallout);
title.addEventListener(`click`, toggleCallout);
const collapsed = div.classList.contains(`is-collapsed`);
const height = collapsed ? title.scrollHeight : div.scrollHeight;
div.style.maxHeight = height + `px`;
}
}
}
document.addEventListener(`nav`, setupCallout);
window.addEventListener(`resize`, setupCallout);
</script><script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
});
document.addEventListener('nav', async () => {
await mermaid.run({
querySelector: '.mermaid'
})
});
</script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../../postscript.js" type="module"></script></html>