{"id":1004099,"date":"2024-12-27T10:23:13","date_gmt":"2024-12-27T02:23:13","guid":{"rendered":""},"modified":"2024-12-27T10:23:18","modified_gmt":"2024-12-27T02:23:18","slug":"python%e5%a6%82%e4%bd%95%e6%89%93log%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004099.html","title":{"rendered":"Python\u5982\u4f55\u6253log\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081152\/101faf1d-c73f-461a-bf16-c1d6506e2e0d.webp\" alt=\"Python\u5982\u4f55\u6253log\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8bb0\u5f55\u65e5\u5fd7\u4e3b\u8981\u901a\u8fc7logging\u6a21\u5757\u5b8c\u6210\uff0c\u4f7f\u7528logging\u6a21\u5757\u7684\u4f18\u70b9\u5305\u62ec\u7075\u6d3b\u3001\u53ef\u914d\u7f6e\u3001\u652f\u6301\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u3001\u5141\u8bb8\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u4e0d\u540c\u76ee\u6807\uff0c\u5982\u63a7\u5236\u53f0\u548c\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684logging\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u529f\u80fd\u5168\u9762\u4e14\u7075\u6d3b\u7684\u6846\u67b6\uff0c\u7528\u4e8e\u8ddf\u8e2a\u548c\u8c03\u8bd5\u8f6f\u4ef6\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u8fc7\u7a0b\u3002\u5728Python\u4e2d\uff0c\u6253log\u51fd\u6570\u4e3b\u8981\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a<strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\u3001\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u3001\u5b9a\u4e49\u65e5\u5fd7\u683c\u5f0f\u3001\u9009\u62e9\u65e5\u5fd7\u8f93\u51fa\u76ee\u6807<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u6b65\u9aa4\uff1a<strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong>\u3002\u5728Python\u4e2d\uff0c\u4f7f\u7528logging.getLogger()\u51fd\u6570\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668\u5bf9\u8c61\u3002\u901a\u8fc7\u8be5\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u65e5\u5fd7\u7684\u540d\u79f0\u3001\u65e5\u5fd7\u7ea7\u522b\u3001\u65e5\u5fd7\u683c\u5f0f\u7b49\u3002\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u4e00\u4e2a\u5e38\u89c1\u505a\u6cd5\u662f\u4f7f\u7528logging.basicConfig()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7528\u4e8e\u4e00\u6b21\u6027\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u57fa\u672c\u8bbe\u7f6e\uff0c\u5982\u65e5\u5fd7\u7ea7\u522b\u3001\u683c\u5f0f\u3001\u8f93\u51fa\u76ee\u6807\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON LOGGING\u6a21\u5757\u6982\u8ff0<\/p>\n<\/p>\n<p><p>Python\u7684logging\u6a21\u5757\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e8\u5728\u63d0\u4f9b\u4e00\u79cd\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u65b9\u5f0f\u3002\u4f7f\u7528logging\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u6216\u8005\u7f51\u7edc\u3002\u5b83\u652f\u6301\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u5982DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u9002\u5408\u7684\u7ea7\u522b\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u65e5\u5fd7\u7ea7\u522b<\/strong>\uff1a\u65e5\u5fd7\u7ea7\u522b\u7528\u4e8e\u63cf\u8ff0\u65e5\u5fd7\u7684\u91cd\u8981\u6027\uff0c\u4ece\u4f4e\u5230\u9ad8\u4f9d\u6b21\u4e3aDEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\u3002\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff0c\u53ef\u4ee5\u63a7\u5236\u54ea\u4e9b\u65e5\u5fd7\u4fe1\u606f\u4f1a\u88ab\u8bb0\u5f55\u4e0b\u6765\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong>\uff1a\u65e5\u5fd7\u8bb0\u5f55\u5668\u662flogging\u6a21\u5757\u7684\u6838\u5fc3\u5bf9\u8c61\uff0c\u8d1f\u8d23\u521b\u5efa\u548c\u7ba1\u7406\u65e5\u5fd7\u3002\u901a\u8fc7\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u53ef\u4ee5\u6307\u5b9a\u65e5\u5fd7\u7ea7\u522b\u3001\u683c\u5f0f\u5316\u65e5\u5fd7\u8f93\u51fa\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e5\u5fd7\u5904\u7406\u5668<\/strong>\uff1a\u65e5\u5fd7\u5904\u7406\u5668\u8d1f\u8d23\u5c06\u65e5\u5fd7\u8bb0\u5f55\u5668\u751f\u6210\u7684\u65e5\u5fd7\u8f93\u51fa\u5230\u6307\u5b9a\u7684\u76ee\u6807\uff0c\u5982\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u6216\u8005\u7f51\u7edc\u3002\u5e38\u7528\u7684\u5904\u7406\u5668\u5305\u62ecStreamHandler\u3001FileHandler\u548cSocketHandler\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e5\u5fd7\u683c\u5f0f\u5316\u5668<\/strong>\uff1a\u65e5\u5fd7\u683c\u5f0f\u5316\u5668\u7528\u4e8e\u5b9a\u4e49\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u53ef\u4ee5\u6307\u5b9a\u65e5\u5fd7\u7684\u65f6\u95f4\u3001\u65e5\u5fd7\u7ea7\u522b\u3001\u65e5\u5fd7\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u7684logging\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u914d\u7f6e\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528logging.getLogger()\u51fd\u6570\u83b7\u53d6\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668\u5bf9\u8c61\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u6839\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u4f46\u5982\u679c\u9700\u8981\u521b\u5efa\u7279\u5b9a\u529f\u80fd\u7684\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u8868\u793a\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u83b7\u53d6\u540d\u4e3a&#39;my_logger&#39;\u7684\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u5e38\u89c1\u505a\u6cd5\u662f\u4f7f\u7528logging.basicConfig()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u4e00\u6b21\u6027\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\u7684\u57fa\u672c\u8bbe\u7f6e\uff0c\u5982\u65e5\u5fd7\u7ea7\u522b\u3001\u683c\u5f0f\u3001\u8f93\u51fa\u76ee\u6807\u7b49\u3002<\/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.INFO,<\/p>\n<p>                    format=&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;,<\/p>\n<p>                    handlers=[logging.StreamHandler()])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0clogging.basicConfig()\u51fd\u6570\u8bbe\u7f6e\u4e86\u65e5\u5fd7\u7ea7\u522b\u4e3aINFO\uff0c\u65e5\u5fd7\u683c\u5f0f\u4e3a\u5305\u542b\u65f6\u95f4\u3001\u540d\u79f0\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u4fe1\u606f\u7684\u5b57\u7b26\u4e32\uff0c\u65e5\u5fd7\u8f93\u51fa\u76ee\u6807\u4e3a\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u65e5\u5fd7\u7ea7\u522b\u8bbe\u7f6e<\/p>\n<\/p>\n<p><p>\u5728logging\u6a21\u5757\u4e2d\uff0c\u65e5\u5fd7\u7ea7\u522b\u7528\u4e8e\u63a7\u5236\u54ea\u4e9b\u65e5\u5fd7\u4fe1\u606f\u4f1a\u88ab\u8f93\u51fa\u3002\u9ed8\u8ba4\u7684\u65e5\u5fd7\u7ea7\u522b\u4e3aWARNING\uff0c\u8fd9\u610f\u5473\u7740\u53ea\u6709\u65e5\u5fd7\u7ea7\u522b\u4e3aWARNING\u53ca\u5176\u4ee5\u4e0a\u7684\u65e5\u5fd7\u4fe1\u606f\u624d\u4f1a\u88ab\u8f93\u51fa\u3002\u5982\u679c\u9700\u8981\u8bb0\u5f55\u66f4\u591a\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3aDEBUG<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u65e5\u5fd7\u7ea7\u522b\u88ab\u8bbe\u7f6e\u4e3aDEBUG\uff0c\u8fd9\u610f\u5473\u7740\u6240\u6709\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u90fd\u5c06\u88ab\u8f93\u51fa\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u65e5\u5fd7\u7ea7\u522b\u7684\u8bbe\u7f6e\u53ef\u4ee5\u5728\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\u65f6\u901a\u8fc7logging.basicConfig()\u51fd\u6570\u6307\u5b9a\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u65e5\u5fd7\u8bb0\u5f55\u5668\u5bf9\u8c61\u7684setLevel()\u65b9\u6cd5\u6765\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u83b7\u53d6\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3aERROR<\/strong><\/h2>\n<p>logger.setLevel(logging.ERROR)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5b9a\u4e49\u65e5\u5fd7\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u65e5\u5fd7\u683c\u5f0f\u5316\u5668\u7528\u4e8e\u5b9a\u4e49\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7logging.Formatter\u7c7b\u6765\u5b9e\u73b0\u3002\u65e5\u5fd7\u683c\u5f0f\u5316\u5668\u53ef\u4ee5\u6307\u5b9a\u65e5\u5fd7\u8f93\u51fa\u7684\u65f6\u95f4\u3001\u65e5\u5fd7\u7ea7\u522b\u3001\u65e5\u5fd7\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u683c\u5f0f\u5316\u5668<\/strong><\/h2>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u63a7\u5236\u53f0\u5904\u7406\u5668\uff0c\u5e76\u8bbe\u7f6e\u683c\u5f0f\u5316\u5668<\/strong><\/h2>\n<p>console_handler = logging.StreamHandler()<\/p>\n<p>console_handler.setFormatter(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><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u65e5\u5fd7\u683c\u5f0f\u5316\u5668formatter\uff0c\u5b9a\u4e49\u4e86\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u63a7\u5236\u53f0\u5904\u7406\u5668console_handler\uff0c\u5e76\u5c06\u683c\u5f0f\u5316\u5668\u8bbe\u7f6e\u7ed9\u5904\u7406\u5668\u3002\u6700\u540e\uff0c\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668logger\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u9009\u62e9\u65e5\u5fd7\u8f93\u51fa\u76ee\u6807<\/p>\n<\/p>\n<p><p>\u65e5\u5fd7\u5904\u7406\u5668\u8d1f\u8d23\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6307\u5b9a\u7684\u76ee\u6807\uff0clogging\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65e5\u5fd7\u5904\u7406\u5668\uff0c\u5982StreamHandler\u3001FileHandler\u3001SocketHandler\u7b49\u3002\u4e0b\u9762\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65e5\u5fd7\u5904\u7406\u5668\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>StreamHandler<\/strong>\uff1a\u7528\u4e8e\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u83b7\u53d6\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u63a7\u5236\u53f0\u5904\u7406\u5668<\/strong><\/h2>\n<p>console_handler = logging.StreamHandler()<\/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><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>FileHandler<\/strong>\uff1a\u7528\u4e8e\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u83b7\u53d6\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>file_handler = logging.FileHandler(&#39;my_log.log&#39;)<\/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><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>RotatingFileHandler<\/strong>\uff1a\u7528\u4e8e\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\uff0c\u5e76\u652f\u6301\u65e5\u5fd7\u6587\u4ef6\u7684\u8f6e\u8f6c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>from logging.handlers import RotatingFileHandler<\/p>\n<h2><strong>\u83b7\u53d6\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u8f6e\u8f6c\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>rotating_handler = RotatingFileHandler(&#39;my_log.log&#39;, maxBytes=1024, backupCount=3)<\/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><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>SocketHandler<\/strong>\uff1a\u7528\u4e8e\u5c06\u65e5\u5fd7\u53d1\u9001\u5230\u7f51\u7edc\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>from logging.handlers import SocketHandler<\/p>\n<h2><strong>\u83b7\u53d6\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5957\u63a5\u5b57\u5904\u7406\u5668<\/strong><\/h2>\n<p>socket_handler = SocketHandler(&#39;localhost&#39;, 9999)<\/p>\n<h2><strong>\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger.addHandler(socket_handler)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5e94\u7528\u65e5\u5fd7\u8bb0\u5f55<\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u597d\u65e5\u5fd7\u8bb0\u5f55\u5668\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u5668\u6765\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\u3002logging\u6a21\u5757\u4e3a\u6bcf\u4e2a\u65e5\u5fd7\u7ea7\u522b\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u8bb0\u5f55\u65b9\u6cd5\uff0c\u5982debug()\u3001info()\u3001warning()\u3001error()\u548ccritical()\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u83b7\u53d6\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<h2><strong>\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7<\/strong><\/h2>\n<p>logger.debug(&#39;This is a debug message&#39;)<\/p>\n<p>logger.info(&#39;This is an info message&#39;)<\/p>\n<p>logger.warning(&#39;This is a warning message&#39;)<\/p>\n<p>logger.error(&#39;This is an error message&#39;)<\/p>\n<p>logger.critical(&#39;This is a critical message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u4f1a\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b\u3002DEBUG\u7ea7\u522b\u7684\u65e5\u5fd7\u901a\u5e38\u7528\u4e8e\u8c03\u8bd5\u548c\u5f00\u53d1\uff0cINFO\u7ea7\u522b\u7684\u65e5\u5fd7\u7528\u4e8e\u8bb0\u5f55\u4e00\u822c\u6027\u7684\u4fe1\u606f\uff0cWARNING\u7ea7\u522b\u7684\u65e5\u5fd7\u7528\u4e8e\u8bb0\u5f55\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\uff0cERROR\u548cCRITICAL\u7ea7\u522b\u7684\u65e5\u5fd7\u7528\u4e8e\u8bb0\u5f55\u4e25\u91cd\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u65e5\u5fd7\u8bb0\u5f55\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b<\/strong>\uff1a\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b\u53ef\u4ee5\u5e2e\u52a9\u56e2\u961f\u66f4\u597d\u5730\u7406\u89e3\u548c\u7ef4\u62a4\u4ee3\u7801\u3002DEBUG\u7ea7\u522b\u7684\u65e5\u5fd7\u53ef\u4ee5\u63d0\u4f9b\u8be6\u7ec6\u7684\u8c03\u8bd5\u4fe1\u606f\uff0c\u800cINFO\u7ea7\u522b\u7684\u65e5\u5fd7\u53ef\u4ee5\u63d0\u4f9b\u8fd0\u884c\u65f6\u7684\u91cd\u8981\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u8fc7\u591a\u7684\u65e5\u5fd7\u8f93\u51fa<\/strong>\uff1a\u867d\u7136\u8bb0\u5f55\u65e5\u5fd7\u662f\u8c03\u8bd5\u548c\u76d1\u63a7\u5e94\u7528\u7a0b\u5e8f\u7684\u91cd\u8981\u624b\u6bb5\uff0c\u4f46\u8fc7\u591a\u7684\u65e5\u5fd7\u8f93\u51fa\u53ef\u80fd\u5bfc\u81f4\u65e5\u5fd7\u6587\u4ef6\u8fc7\u5927\uff0c\u5f71\u54cd\u6027\u80fd\u3002\u5e94\u6839\u636e\u5b9e\u9645\u9700\u6c42\u5408\u7406\u9009\u62e9\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u8f93\u51fa\u76ee\u6807\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\uff1a\u5728\u8bb0\u5f55\u65e5\u5fd7\u65f6\uff0c\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4ee3\u66ff\u5b57\u7b26\u4e32\u62fc\u63a5\u53ef\u4ee5\u63d0\u9ad8\u65e5\u5fd7\u8bb0\u5f55\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u63a8\u8350\u7684\u505a\u6cd5<\/p>\n<p>logger.info(&#39;User %s logged in at %s&#39;, user_name, login_time)<\/p>\n<h2><strong>\u4e0d\u63a8\u8350\u7684\u505a\u6cd5<\/strong><\/h2>\n<p>logger.info(&#39;User &#39; + user_name + &#39; logged in at &#39; + str(login_time))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\n<p><strong>\u7edf\u4e00\u65e5\u5fd7\u683c\u5f0f<\/strong>\uff1a\u5728\u56e2\u961f\u5f00\u53d1\u4e2d\uff0c\u7edf\u4e00\u65e5\u5fd7\u683c\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u65e5\u5fd7\u4fe1\u606f\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5efa\u8bae\u56e2\u961f\u5728\u9879\u76ee\u521d\u671f\u5c31\u5b9a\u4e49\u597d\u65e5\u5fd7\u683c\u5f0f\uff0c\u5e76\u5728\u9879\u76ee\u4e2d\u7edf\u4e00\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u671f\u6e05\u7406\u65e5\u5fd7\u6587\u4ef6<\/strong>\uff1a\u5bf9\u4e8e\u8f93\u51fa\u5230\u6587\u4ef6\u7684\u65e5\u5fd7\uff0c\u5efa\u8bae\u5b9a\u671f\u6e05\u7406\u65e7\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u4ee5\u907f\u514d\u78c1\u76d8\u7a7a\u95f4\u88ab\u5360\u6ee1\u3002\u53ef\u4ee5\u4f7f\u7528RotatingFileHandler\u6216TimedRotatingFileHandler\u6765\u5b9e\u73b0\u65e5\u5fd7\u6587\u4ef6\u7684\u81ea\u52a8\u8f6e\u8f6c\u548c\u6e05\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u6b63\u786e\u4f7f\u7528Python\u7684logging\u6a21\u5757\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5730\u8bb0\u5f55\u548c\u7ba1\u7406\u65e5\u5fd7\u4fe1\u606f\uff0c\u4ece\u800c\u66f4\u597d\u5730\u8c03\u8bd5\u548c\u7ef4\u62a4\u5e94\u7528\u7a0b\u5e8f\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u7684\u65e5\u5fd7\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\u8bb0\u5f55\u4fe1\u606f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>logging<\/code>\u6a21\u5757\u6765\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\uff0c\u5982DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\uff0c\u5141\u8bb8\u7528\u6237\u6839\u636e\u9700\u8981\u8bb0\u5f55\u4e0d\u540c\u91cd\u8981\u6027\u7684\u65e5\u5fd7\u3002\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165\u6a21\u5757\u3001\u914d\u7f6e\u65e5\u5fd7\u683c\u5f0f\u548c\u7ea7\u522b\u3001\u4ee5\u53ca\u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import logging\n\n# \u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\nlogging.basicConfig(level=logging.INFO, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)\n\n# \u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\nlogging.info(&quot;\u8fd9\u662f\u4e00\u6761\u4fe1\u606f\u65e5\u5fd7&quot;)\nlogging.error(&quot;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u65e5\u5fd7&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49Python\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>logging.basicConfig()<\/code>\u51fd\u6570\u4e2d\u7684<code>format<\/code>\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u5305\u542b\u65e5\u671f\u3001\u65f6\u95f4\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u6d88\u606f\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>&#39;%(asctime)s - %(levelname)s - %(message)s&#39;<\/code>\u5c06\u663e\u793a\u65f6\u95f4\u6233\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u6d88\u606f\u3002\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8bbe\u8ba1\u81ea\u5df1\u7684\u683c\u5f0f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6ee1\u8db3\u7279\u5b9a\u7684\u8bb0\u5f55\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u800c\u4e0d\u662f\u63a7\u5236\u53f0\uff1f<\/strong><br \/>\u8981\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728<code>logging.basicConfig()<\/code>\u4e2d\u8bbe\u7f6e<code>filename<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6240\u6709\u7684\u65e5\u5fd7\u4fe1\u606f\u5c06\u4f1a\u88ab\u8bb0\u5f55\u5230\u540d\u4e3a<code>app.log<\/code>\u7684\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u663e\u793a\u5728\u63a7\u5236\u53f0\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>filemode<\/code>\u53c2\u6570\u6765\u63a7\u5236\u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\uff0c\u4f8b\u5982<code>&#39;w&#39;<\/code>\u4f1a\u8986\u76d6\u6587\u4ef6\uff0c<code>&#39;a&#39;<\/code>\u4f1a\u8ffd\u52a0\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8bb0\u5f55\u65e5\u5fd7\u4e3b\u8981\u901a\u8fc7logging\u6a21\u5757\u5b8c\u6210\uff0c\u4f7f\u7528logging\u6a21\u5757\u7684\u4f18\u70b9\u5305\u62ec\u7075\u6d3b\u3001\u53ef\u914d\u7f6e\u3001\u652f\u6301\u4e0d [&hellip;]","protected":false},"author":3,"featured_media":1004112,"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\/1004099"}],"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=1004099"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004099\/revisions"}],"predecessor-version":[{"id":1004114,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004099\/revisions\/1004114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004112"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}