{"id":1163377,"date":"2025-01-15T14:54:36","date_gmt":"2025-01-15T06:54:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163377.html"},"modified":"2025-01-15T14:54:39","modified_gmt":"2025-01-15T06:54:39","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e6%97%a5%e5%bf%97%e7%9b%91%e6%8e%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1163377.html","title":{"rendered":"python\u5982\u4f55\u505a\u65e5\u5fd7\u76d1\u63a7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204148\/ebda7e17-8116-4de4-95d4-c9b912b4c507.webp\" alt=\"python\u5982\u4f55\u505a\u65e5\u5fd7\u76d1\u63a7\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684logging\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\u5982loguru\u3001\u7ed3\u5408\u76d1\u63a7\u5de5\u5177\u5982ELK Stack\u6765\u5b9e\u73b0\u65e5\u5fd7\u76d1\u63a7\u3002<\/strong> \u4f7f\u7528logging\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u7ed3\u5408loguru\u5e93\u53ef\u4ee5\u7b80\u5316\u65e5\u5fd7\u8bb0\u5f55\u8fc7\u7a0b\uff0c\u800cELK Stack\uff08Elasticsearch, Logstash, Kibana\uff09\u53ef\u4ee5\u5bf9\u65e5\u5fd7\u8fdb\u884c\u96c6\u4e2d\u5904\u7406\u548c\u5206\u6790\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528logging\u6a21\u5757\u8fdb\u884c\u65e5\u5fd7\u76d1\u63a7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684logging\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684logging\u6a21\u5757\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u65e5\u5fd7\u8bb0\u5f55\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\u3001\u65e5\u5fd7\u683c\u5f0f\u548c\u65e5\u5fd7\u5904\u7406\u5668\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3001\u9519\u8bef\u4fe1\u606f\u548c\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u914d\u7f6elogging\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6elogging\u6a21\u5757\uff0c\u4ee5\u4fbf\u5b83\u80fd\u591f\u6309\u7167\u6211\u4eec\u7684\u8981\u6c42\u8bb0\u5f55\u65e5\u5fd7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG,<\/p>\n<p>                    format=&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;,<\/p>\n<p>                    handlers=[logging.FileHandler(&#39;app.log&#39;, encoding=&#39;utf-8&#39;),<\/p>\n<p>                              logging.StreamHandler()])<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.info(&#39;\u8fd9\u662f\u4e00\u6761\u4fe1\u606f&#39;)<\/p>\n<p>logger.warning(&#39;\u8fd9\u662f\u4e00\u6761\u8b66\u544a\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p>logger.critical(&#39;\u8fd9\u662f\u4e00\u6761\u4e25\u91cd\u7684\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u914d\u7f6e\u4e86logging\u6a21\u5757\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\uff0c\u5e76\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u548c\u63a7\u5236\u53f0\u3002<code>basicConfig<\/code>\u65b9\u6cd5\u7528\u4e8e\u4e00\u6b21\u6027\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c<code>getLogger<\/code>\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u8bb0\u5f55\u5668\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u65e5\u5fd7\u7ea7\u522b<\/h4>\n<\/p>\n<p><p>logging\u6a21\u5757\u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u65e5\u5fd7\u7ea7\u522b\uff0c\u4ece\u4f4e\u5230\u9ad8\u4f9d\u6b21\u4e3a\uff1a<\/p>\n<\/p>\n<ul>\n<li>DEBUG: \u8c03\u8bd5\u4fe1\u606f\uff0c\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u901a\u5e38\u53ea\u5728\u5f00\u53d1\u65f6\u4f7f\u7528\u3002<\/li>\n<li>INFO: \u4fe1\u606f\uff0c\u786e\u8ba4\u7a0b\u5e8f\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/li>\n<li>WARNING: \u8b66\u544a\uff0c\u8868\u793a\u67d0\u4e9b\u975e\u4e25\u91cd\u7684\u95ee\u9898\u3002<\/li>\n<li>ERROR: \u9519\u8bef\uff0c\u7a0b\u5e8f\u4e2d\u7684\u67d0\u4e9b\u529f\u80fd\u4e0d\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/li>\n<li>CRITICAL: \u4e25\u91cd\u9519\u8bef\uff0c\u8868\u793a\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p><p>\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u9002\u5f53\u7684\u65e5\u5fd7\u7ea7\u522b\u8bb0\u5f55\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u65e5\u5fd7\u5904\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u5904\u7406\u5668\u7528\u4e8e\u6307\u5b9a\u65e5\u5fd7\u7684\u8f93\u51fa\u4f4d\u7f6e\uff0c\u5e38\u89c1\u7684\u5904\u7406\u5668\u5305\u62ec<code>StreamHandler<\/code>\uff08\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff09\u3001<code>FileHandler<\/code>\uff08\u8f93\u51fa\u5230\u6587\u4ef6\uff09\u548c<code>HTTPHandler<\/code>\uff08\u901a\u8fc7HTTP\u53d1\u9001\u65e5\u5fd7\uff09\u3002\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u591a\u4e2a\u5904\u7406\u5668\uff0c\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u4e0d\u540c\u7684\u5730\u65b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\u63a7\u5236\u53f0\u5904\u7406\u5668<\/strong><\/h2>\n<p>console_handler = logging.StreamHandler()<\/p>\n<p>console_handler.setLevel(logging.DEBUG)<\/p>\n<p>console_formatter = logging.Formatter(&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>console_handler.setFormatter(console_formatter)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>file_handler = logging.FileHandler(&#39;app.log&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p>file_handler.setLevel(logging.ERROR)<\/p>\n<p>file_formatter = logging.Formatter(&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>file_handler.setFormatter(file_formatter)<\/p>\n<h2><strong>\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger.addHandler(console_handler)<\/p>\n<p>logger.addHandler(file_handler)<\/p>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u521b\u5efa\u4e86\u63a7\u5236\u53f0\u5904\u7406\u5668\u548c\u6587\u4ef6\u5904\u7406\u5668\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5904\u7406\u5668\u8bbe\u7f6e\u4e86\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f\u3002\u6700\u540e\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93loguru<\/h3>\n<\/p>\n<p><p>loguru\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5e93\uff0c\u76f8\u8f83\u4e8e\u5185\u7f6e\u7684logging\u6a21\u5757\uff0c\u5b83\u66f4\u52a0\u7b80\u6d01\u6613\u7528\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7279\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5loguru<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5loguru\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install loguru<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528loguru\u8bb0\u5f55\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>loguru\u5e93\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5bfc\u5165\u5e76\u76f4\u63a5\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from loguru import logger<\/p>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.info(&#39;\u8fd9\u662f\u4e00\u6761\u4fe1\u606f&#39;)<\/p>\n<p>logger.warning(&#39;\u8fd9\u662f\u4e00\u6761\u8b66\u544a\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p>logger.critical(&#39;\u8fd9\u662f\u4e00\u6761\u4e25\u91cd\u7684\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u914d\u7f6eloguru<\/h4>\n<\/p>\n<p><p>loguru\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u914d\u7f6e\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>logger.add<\/code>\u65b9\u6cd5\u6dfb\u52a0\u4e0d\u540c\u7684\u65e5\u5fd7\u5904\u7406\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from loguru import logger<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>logger.add(&#39;app.log&#39;, level=&#39;DEBUG&#39;, format=&#39;{time} - {name} - {level} - {message}&#39;)<\/p>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>logger.add<\/code>\u65b9\u6cd5\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6587\u4ef6\u5904\u7406\u5668\uff0c\u6307\u5b9a\u4e86\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408ELK Stack\u8fdb\u884c\u65e5\u5fd7\u76d1\u63a7<\/h3>\n<\/p>\n<p><p>ELK Stack\uff08Elasticsearch, Logstash, Kibana\uff09\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u65e5\u5fd7\u7ba1\u7406\u548c\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u96c6\u4e2d\u5904\u7406\u548c\u5206\u6790\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5ELK Stack<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u548c\u914d\u7f6eELK Stack\u3002\u5177\u4f53\u7684\u5b89\u88c5\u6b65\u9aa4\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff0c\u8fd9\u91cc\u7b80\u8981\u4ecb\u7ecd\u4e00\u4e0b\u5404\u7ec4\u4ef6\u7684\u4f5c\u7528\uff1a<\/p>\n<\/p>\n<ul>\n<li>Elasticsearch: \u4e00\u4e2a\u5206\u5e03\u5f0f\u641c\u7d22\u548c\u5206\u6790\u5f15\u64ce\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u641c\u7d22\u65e5\u5fd7\u6570\u636e\u3002<\/li>\n<li>Logstash: \u4e00\u4e2a\u6570\u636e\u5904\u7406\u7ba1\u9053\uff0c\u7528\u4e8e\u6536\u96c6\u3001\u89e3\u6790\u548c\u8f6c\u6362\u65e5\u5fd7\u6570\u636e\u3002<\/li>\n<li>Kibana: \u4e00\u4e2a\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u7528\u4e8e\u5c55\u793a\u548c\u5206\u6790\u65e5\u5fd7\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u914d\u7f6eLogstash<\/h4>\n<\/p>\n<p><p>\u914d\u7f6eLogstash\u4ee5\u63a5\u6536Python\u7a0b\u5e8f\u751f\u6210\u7684\u65e5\u5fd7\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">input {<\/p>\n<p>  file {<\/p>\n<p>    path =&gt; &quot;\/path\/to\/app.log&quot;<\/p>\n<p>    start_position =&gt; &quot;beginning&quot;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>filter {<\/p>\n<p>  grok {<\/p>\n<p>    match =&gt; { &quot;message&quot; =&gt; &quot;%{TIMESTAMP_ISO8601:timestamp} - %{DATA:logger} - %{LOGLEVEL:level} - %{GREEDYDATA:message}&quot; }<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>output {<\/p>\n<p>  elasticsearch {<\/p>\n<p>    hosts =&gt; [&quot;http:\/\/localhost:9200&quot;]<\/p>\n<p>    index =&gt; &quot;python-logs&quot;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0cLogstash\u4f1a\u4ece\u6307\u5b9a\u8def\u5f84\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528grok\u8fc7\u6ee4\u5668\u89e3\u6790\u65e5\u5fd7\u683c\u5f0f\uff0c\u6700\u540e\u5c06\u65e5\u5fd7\u6570\u636e\u53d1\u9001\u5230Elasticsearch\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u914d\u7f6eKibana<\/h4>\n<\/p>\n<p><p>\u914d\u7f6eKibana\u4ee5\u5c55\u793a\u548c\u5206\u6790\u65e5\u5fd7\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7Kibana\u7684Web\u754c\u9762\u6dfb\u52a0Elasticsearch\u7d22\u5f15\uff0c\u5e76\u521b\u5efa\u5404\u79cd\u53ef\u89c6\u5316\u56fe\u8868\u548c\u4eea\u8868\u76d8\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0Python\u65e5\u5fd7\u7684\u8bb0\u5f55\u548c\u76d1\u63a7\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u7684logging\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u5404\u79cd\u4fe1\u606f\uff0cloguru\u5e93\u7b80\u5316\u4e86\u65e5\u5fd7\u8bb0\u5f55\u8fc7\u7a0b\uff0c\u800cELK Stack\u53ef\u4ee5\u5bf9\u65e5\u5fd7\u8fdb\u884c\u96c6\u4e2d\u5904\u7406\u548c\u5206\u6790\u3002<\/strong> \u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5206\u6790\u7a0b\u5e8f\u65e5\u5fd7\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6269\u5c55\u9605\u8bfb<\/h3>\n<\/p>\n<p><h4>1\u3001\u65e5\u5fd7\u8f6e\u8f6c<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u8f6e\u8f6c\u662f\u6307\u5b9a\u671f\u6216\u6839\u636e\u65e5\u5fd7\u6587\u4ef6\u5927\u5c0f\u8fdb\u884c\u65e5\u5fd7\u6587\u4ef6\u7684\u66f4\u6362\uff0c\u907f\u514d\u5355\u4e2a\u65e5\u5fd7\u6587\u4ef6\u8fc7\u5927\u3002\u53ef\u4ee5\u4f7f\u7528<code>logging.handlers.RotatingFileHandler<\/code>\u6216<code>logging.handlers.TimedRotatingFileHandler<\/code>\u5b9e\u73b0\u65e5\u5fd7\u8f6e\u8f6c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>from logging.handlers import RotatingFileHandler<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u8f6e\u8f6c\u5904\u7406\u5668<\/strong><\/h2>\n<p>rotating_handler = RotatingFileHandler(&#39;app.log&#39;, maxBytes=1024*1024, backupCount=5)<\/p>\n<p>rotating_handler.setLevel(logging.DEBUG)<\/p>\n<p>rotating_formatter = logging.Formatter(&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>rotating_handler.setFormatter(rotating_formatter)<\/p>\n<h2><strong>\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger.addHandler(rotating_handler)<\/p>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>RotatingFileHandler<\/code>\u4f1a\u5728\u65e5\u5fd7\u6587\u4ef6\u8fbe\u52301MB\u65f6\u8fdb\u884c\u8f6e\u8f6c\uff0c\u5e76\u4fdd\u7559\u6700\u8fd1\u76845\u4e2a\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5f02\u6b65\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u5f02\u6b65\u65e5\u5fd7\u53ef\u4ee5\u63d0\u9ad8\u65e5\u5fd7\u8bb0\u5f55\u7684\u6027\u80fd\uff0c\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002\u53ef\u4ee5\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u5b9e\u73b0\u5f02\u6b65\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>from concurrent.futures import ThreadPoolExecutor<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>file_handler = logging.FileHandler(&#39;app.log&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p>file_handler.setLevel(logging.DEBUG)<\/p>\n<p>file_formatter = logging.Formatter(&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>file_handler.setFormatter(file_formatter)<\/p>\n<h2><strong>\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger.addHandler(file_handler)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=1)<\/p>\n<p>def log_message(level, message):<\/p>\n<p>    if level == &#39;debug&#39;:<\/p>\n<p>        logger.debug(message)<\/p>\n<p>    elif level == &#39;info&#39;:<\/p>\n<p>        logger.info(message)<\/p>\n<p>    elif level == &#39;warning&#39;:<\/p>\n<p>        logger.warning(message)<\/p>\n<p>    elif level == &#39;error&#39;:<\/p>\n<p>        logger.error(message)<\/p>\n<p>    elif level == &#39;critical&#39;:<\/p>\n<p>        logger.critical(message)<\/p>\n<h2><strong>\u5f02\u6b65\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>executor.submit(log_message, &#39;debug&#39;, &#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>executor.submit(log_message, &#39;error&#39;, &#39;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>ThreadPoolExecutor<\/code>\u7528\u4e8e\u5f02\u6b65\u6267\u884c\u65e5\u5fd7\u8bb0\u5f55\u4efb\u52a1\uff0c\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u7ed3\u6784\u5316\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u7ed3\u6784\u5316\u65e5\u5fd7\u662f\u6307\u5c06\u65e5\u5fd7\u4fe1\u606f\u4ee5\u7ed3\u6784\u5316\u7684\u683c\u5f0f\u8bb0\u5f55\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u89e3\u6790\u548c\u5206\u6790\u3002\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5b9e\u73b0\u7ed3\u6784\u5316\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import json<\/p>\n<p>class JsonFormatter(logging.Formatter):<\/p>\n<p>    def format(self, record):<\/p>\n<p>        log_record = {<\/p>\n<p>            &#39;timestamp&#39;: self.formatTime(record, self.datefmt),<\/p>\n<p>            &#39;logger&#39;: record.name,<\/p>\n<p>            &#39;level&#39;: record.levelname,<\/p>\n<p>            &#39;message&#39;: record.getMessage()<\/p>\n<p>        }<\/p>\n<p>        return json.dumps(log_record)<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>file_handler = logging.FileHandler(&#39;app.log&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p>file_handler.setLevel(logging.DEBUG)<\/p>\n<p>json_formatter = JsonFormatter()<\/p>\n<p>file_handler.setFormatter(json_formatter)<\/p>\n<h2><strong>\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger.addHandler(file_handler)<\/p>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>JsonFormatter<\/code>\uff0c\u5c06\u65e5\u5fd7\u4fe1\u606f\u4ee5JSON\u683c\u5f0f\u8bb0\u5f55\u3002\u8fd9\u6837\u4fbf\u4e8e\u540e\u7eed\u7684\u89e3\u6790\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u65e5\u5fd7\u4fe1\u606f\u7684\u91cd\u8981\u6027\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u907f\u514d\u8bb0\u5f55\u4e0d\u5fc5\u8981\u7684\u4fe1\u606f\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5f00\u53d1\u9636\u6bb5\u53ef\u4ee5\u4f7f\u7528DEBUG\u7ea7\u522b\uff0c\u751f\u4ea7\u73af\u5883\u53ef\u4ee5\u4f7f\u7528INFO\u6216WARNING\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u786e\u4fdd\u5728\u5f02\u5e38\u60c5\u51b5\u4e0b\u6b63\u786e\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\uff0c\u907f\u514d\u9057\u6f0f\u91cd\u8981\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>def process_data(data):<\/p>\n<p>    with logger.catch():<\/p>\n<p>        # \u5904\u7406\u6570\u636e<\/p>\n<p>        pass<\/p>\n<p>process_data(None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b9a\u671f\u6e05\u7406\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u5b9a\u671f\u6e05\u7406\u65e7\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u907f\u514d\u65e5\u5fd7\u6587\u4ef6\u5360\u7528\u8fc7\u591a\u78c1\u76d8\u7a7a\u95f4\u3002\u53ef\u4ee5\u901a\u8fc7\u65e5\u5fd7\u8f6e\u8f6c\u673a\u5236\u5b9e\u73b0\u81ea\u52a8\u6e05\u7406\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u76d1\u63a7\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u76d1\u63a7\u5de5\u5177\uff08\u5982ELK Stack\uff09\u5b9e\u65f6\u76d1\u63a7\u65e5\u5fd7\u4fe1\u606f\uff0c\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u95ee\u9898\uff0c\u63d0\u9ad8\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>5\u3001\u4fdd\u62a4\u65e5\u5fd7\u5b89\u5168<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u65e5\u5fd7\u6587\u4ef6\u7684\u5b89\u5168\u6027\uff0c\u9632\u6b62\u672a\u7ecf\u6388\u6743\u7684\u8bbf\u95ee\u548c\u7be1\u6539\u3002\u53ef\u4ee5\u8bbe\u7f6e\u9002\u5f53\u7684\u6587\u4ef6\u6743\u9650\uff0c\u5e76\u5bf9\u654f\u611f\u4fe1\u606f\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3\u4e0e\u5c55\u671b<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86Python\u65e5\u5fd7\u76d1\u63a7\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684logging\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93loguru\u4ee5\u53ca\u7ed3\u5408ELK Stack\u8fdb\u884c\u65e5\u5fd7\u76d1\u63a7\u3002<strong>\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u548c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u8bb0\u5f55\u3001\u7ba1\u7406\u548c\u5206\u6790\u7a0b\u5e8f\u65e5\u5fd7\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u672a\u6765\uff0c\u968f\u7740\u6280\u672f\u7684\u53d1\u5c55\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u66f4\u591a\u529f\u80fd\u5f3a\u5927\u3001\u6613\u4e8e\u4f7f\u7528\u7684\u65e5\u5fd7\u8bb0\u5f55\u548c\u76d1\u63a7\u5de5\u5177\u3002\u5f00\u53d1\u8005\u5e94\u4fdd\u6301\u5b66\u4e60\u548c\u63a2\u7d22\uff0c\u4e0d\u65ad\u63d0\u5347\u81ea\u5df1\u7684\u6280\u80fd\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9879\u76ee\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u65e5\u5fd7\u76d1\u63a7\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u65e5\u5fd7\u76d1\u63a7\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>logging<\/code>\u5e93\u3001\u7b2c\u4e09\u65b9\u5e93\u5982<code>watchdog<\/code>\uff0c\u4ee5\u53ca\u7ed3\u5408Flask\u6216Django\u7b49\u6846\u67b6\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u548c\u76d1\u63a7\u3002\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u811a\u672c\u6765\u5b9a\u671f\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6\u7684\u53d8\u5316\uff0c\u6216\u8005\u5b9e\u65f6\u76d1\u63a7\u6587\u4ef6\u7684\u66f4\u65b0\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u6765\u589e\u5f3a\u65e5\u5fd7\u76d1\u63a7\u529f\u80fd\uff1f<\/strong><br \/>\u9664\u4e86<code>logging<\/code>\u5e93\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>watchdog<\/code>\u6765\u76d1\u63a7\u6587\u4ef6\u7cfb\u7edf\u7684\u53d8\u5316\uff0c<code>loguru<\/code>\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7b80\u5355\u6613\u7528\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0c<code>pyinotify<\/code>\u5219\u9002\u5408Linux\u7cfb\u7edf\u7684\u5b9e\u65f6\u76d1\u63a7\u3002\u7ed3\u5408\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u65e5\u5fd7\u76d1\u63a7\u9700\u6c42\uff0c\u6bd4\u5982\u53d1\u9001\u90ae\u4ef6\u901a\u77e5\u3001\u751f\u6210\u62a5\u8868\u7b49\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u76d1\u63a7\u65e5\u5fd7\u4e2d\u7684\u9519\u8bef\u548c\u5f02\u5e38\u4fe1\u606f\uff1f<\/strong><br \/>\u5728\u65e5\u5fd7\u76d1\u63a7\u8fc7\u7a0b\u4e2d\uff0c\u5173\u952e\u662f\u8981\u53ca\u65f6\u6355\u6349\u9519\u8bef\u4fe1\u606f\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff08\u5982ERROR\u6216WARNING\uff09\u6765\u8fc7\u6ee4\u4e0d\u5fc5\u8981\u7684\u4fe1\u606f\u3002\u540c\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5c06\u5f02\u5e38\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u548c\u5904\u7406\u3002\u8fd9\u79cd\u505a\u6cd5\u6709\u52a9\u4e8e\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684logging\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\u5982loguru\u3001\u7ed3\u5408\u76d1\u63a7\u5de5\u5177\u5982ELK Stack\u6765\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":1163382,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163377"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1163377"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163377\/revisions"}],"predecessor-version":[{"id":1163383,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163377\/revisions\/1163383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1163382"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1163377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1163377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1163377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}