{"id":938469,"date":"2024-12-26T20:11:45","date_gmt":"2024-12-26T12:11:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/938469.html"},"modified":"2024-12-26T20:11:47","modified_gmt":"2024-12-26T12:11:47","slug":"python-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/938469.html","title":{"rendered":"python \u5982\u4f55\u4f7f\u7528\u65e5\u5fd7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073930\/56d75dba-b8f3-43c5-b3df-80a4f43f7765.webp\" alt=\"python \u5982\u4f55\u4f7f\u7528\u65e5\u5fd7\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d:<br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u65e5\u5fd7\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u8c03\u8bd5\u548c\u76d1\u63a7\u624b\u6bb5\u3002<strong>\u53ef\u4ee5\u901a\u8fc7<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u7a0b\u5e8f\u8fd0\u884c\u4fe1\u606f\u3001\u5e2e\u52a9\u8c03\u8bd5\u548c\u6392\u67e5\u9519\u8bef\u3001\u76d1\u63a7\u7a0b\u5e8f\u8fd0\u884c\u72b6\u6001<\/strong>\u3002\u5176\u4e2d\uff0c<code>logging<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65e5\u5fd7\u529f\u80fd\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u65e5\u5fd7\u7684\u7ea7\u522b\u3001\u683c\u5f0f\u548c\u8f93\u51fa\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u5728\u5f00\u53d1\u548c\u751f\u4ea7\u73af\u5883\u4e2d\u6709\u6548\u7ba1\u7406\u548c\u8ffd\u8e2a\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff08\u5982DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\uff09\u6765\u8bb0\u5f55\u4e0d\u540c\u4e25\u91cd\u7a0b\u5ea6\u7684\u4fe1\u606f\uff0c\u5e76\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u6216\u5176\u4ed6\u65e5\u5fd7\u7ba1\u7406\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<code>LOGGING<\/code>\u6a21\u5757\u57fa\u7840<\/p>\n<\/p>\n<p><p>Python\u7684<code>logging<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8bb0\u5f55\u8fd0\u884c\u65f6\u7684\u4e8b\u4ef6\u3002\u5b83\u652f\u6301\u591a\u79cd\u65e5\u5fd7\u8bb0\u5f55\u65b9\u5f0f\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u5730\u914d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u65e5\u5fd7\u7ea7\u522b<\/strong><br \/><code>logging<\/code>\u6a21\u5757\u5b9a\u4e49\u4e86\u51e0\u4e2a\u6807\u51c6\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u6309\u4e25\u91cd\u7a0b\u5ea6\u9012\u589e\uff1aDEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\u3002\u6bcf\u4e2a\u7ea7\u522b\u90fd\u6709\u5176\u7279\u6b8a\u7684\u7528\u9014\u3002DEBUG\u7ea7\u522b\u7528\u4e8e\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u901a\u5e38\u53ea\u5728\u8bca\u65ad\u95ee\u9898\u65f6\u4f7f\u7528\u3002INFO\u7ea7\u522b\u7528\u4e8e\u786e\u8ba4\u7a0b\u5e8f\u6309\u9884\u671f\u8fd0\u884c\u3002WARNING\u7ea7\u522b\u8868\u793a\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u7684\u8b66\u544a\u4fe1\u606f\u3002ERROR\u7ea7\u522b\u7528\u4e8e\u8bb0\u5f55\u7531\u4e8e\u4e25\u91cd\u95ee\u9898\u5bfc\u81f4\u7684\u4e00\u4e9b\u529f\u80fd\u672a\u80fd\u6267\u884c\u3002CRITICAL\u7ea7\u522b\u8868\u793a\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u884c\u7684\u4e25\u91cd\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u57fa\u672c\u914d\u7f6e<\/strong><br \/>\u4f7f\u7528<code>logging.basicConfig()<\/code>\u53ef\u4ee5\u5feb\u901f\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u57fa\u672c\u914d\u7f6e\uff0c\u5305\u62ec\u65e5\u5fd7\u7ea7\u522b\u3001\u65e5\u5fd7\u683c\u5f0f\u548c\u8f93\u51fa\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u901a\u8fc7<code>logging.basicConfig(level=logging.INFO)<\/code>\u53ef\u4ee5\u5c06\u65e5\u5fd7\u7ea7\u522b\u8bbe\u7f6e\u4e3aINFO\uff0c\u8fd9\u610f\u5473\u7740\u53ea\u6709INFO\u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u4fe1\u606f\u4f1a\u88ab\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u65e5\u5fd7\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>\u65e5\u5fd7\u683c\u5f0f\u5316\u662f\u6307\u5982\u4f55\u5c06\u65e5\u5fd7\u4fe1\u606f\u5c55\u793a\u7ed9\u7528\u6237\uff0c<code>logging<\/code>\u6a21\u5757\u5141\u8bb8\u81ea\u5b9a\u4e49\u65e5\u5fd7\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u683c\u5f0f\u5b57\u7b26\u4e32<\/strong><br \/>\u683c\u5f0f\u5b57\u7b26\u4e32\u6307\u5b9a\u4e86\u65e5\u5fd7\u8bb0\u5f55\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u5e38\u7528\u7684\u683c\u5f0f\u5316\u7b26\u53f7\u6709\uff1a<code>%(asctime)s<\/code>\u8868\u793a\u65e5\u5fd7\u8bb0\u5f55\u65f6\u95f4\uff0c<code>%(levelname)s<\/code>\u8868\u793a\u65e5\u5fd7\u7ea7\u522b\uff0c<code>%(message)s<\/code>\u8868\u793a\u65e5\u5fd7\u4fe1\u606f\uff0c<code>%(name)s<\/code>\u8868\u793a\u8bb0\u5f55\u5668\u540d\u79f0\u7b49\u3002\u901a\u8fc7\u8bbe\u7f6e\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8ba9\u65e5\u5fd7\u4fe1\u606f\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65f6\u95f4\u683c\u5f0f<\/strong><br \/>\u901a\u8fc7<code>datefmt<\/code>\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u65f6\u95f4\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff0c<code>datefmt=&#39;%Y-%m-%d %H:%M:%S&#39;<\/code>\u53ef\u4ee5\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u201c\u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2\u201d\u7684\u5f62\u5f0f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u66f4\u597d\u5730\u4e86\u89e3\u65e5\u5fd7\u4e8b\u4ef6\u53d1\u751f\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u65e5\u5fd7\u5904\u7406\u5668<\/p>\n<\/p>\n<p><p>\u5728<code>logging<\/code>\u6a21\u5757\u4e2d\uff0c\u5904\u7406\u5668\uff08Handler\uff09\u7528\u4e8e\u5c06\u65e5\u5fd7\u8bb0\u5f55\u8f93\u51fa\u5230\u4e0d\u540c\u7684\u76ee\u6807\uff0c\u5982\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u3001\u7f51\u7edc\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6d41\u5904\u7406\u5668<\/strong><br \/><code>logging.StreamHandler<\/code>\u7528\u4e8e\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u5b83\u9002\u5408\u7528\u4e8e\u8c03\u8bd5\u548c\u5f00\u53d1\u9636\u6bb5\uff0c\u8ba9\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u5b9e\u65f6\u67e5\u770b\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u5904\u7406\u5668<\/strong><br \/><code>logging.FileHandler<\/code>\u7528\u4e8e\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\uff0c\u53ef\u4ee5\u5c06\u8fd0\u884c\u65f6\u7684\u65e5\u5fd7\u8bb0\u5f55\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u548c\u6392\u67e5\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6587\u4ef6\u7684\u6eda\u52a8\u7b56\u7565\uff08\u5982\u6309\u65f6\u95f4\u3001\u6309\u5927\u5c0f\u6eda\u52a8\uff09\u6765\u7ba1\u7406\u65e5\u5fd7\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u65e5\u5fd7\u8fc7\u6ee4\u5668<\/p>\n<\/p>\n<p><p>\u65e5\u5fd7\u8fc7\u6ee4\u5668\uff08Filter\uff09\u7528\u4e8e\u66f4\u7cbe\u7ec6\u5730\u63a7\u5236\u54ea\u4e9b\u65e5\u5fd7\u8bb0\u5f55\u5e94\u8be5\u88ab\u8f93\u51fa\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u5668<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>logging.Filter<\/code>\u7c7b\u5e76\u91cd\u5199\u5176<code>filter<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u5668\u3002\u5728<code>filter<\/code>\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u65e5\u5fd7\u8bb0\u5f55\u7684\u5c5e\u6027\u51b3\u5b9a\u662f\u5426\u5141\u8bb8\u5176\u901a\u8fc7\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u8fc7\u6ee4\u5668<\/strong><br \/>\u8fc7\u6ee4\u5668\u53ef\u4ee5\u5e94\u7528\u5230\u8bb0\u5f55\u5668\uff08Logger\uff09\u6216\u5904\u7406\u5668\uff08Handler\uff09\u4e0a\uff0c\u4ece\u800c\u53ea\u8f93\u51fa\u6ee1\u8db3\u6761\u4ef6\u7684\u65e5\u5fd7\u8bb0\u5f55\u3002\u8fd9\u5728\u9700\u8981\u5bf9\u65e5\u5fd7\u8bb0\u5f55\u8fdb\u884c\u7ec6\u7c92\u5ea6\u63a7\u5236\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u65e5\u5fd7\u8bb0\u5f55\u5668<\/p>\n<\/p>\n<p><p>\u65e5\u5fd7\u8bb0\u5f55\u5668\uff08Logger\uff09\u662f<code>logging<\/code>\u6a21\u5757\u4e2d\u7528\u4e8e\u751f\u6210\u65e5\u5fd7\u8bb0\u5f55\u7684\u5bf9\u8c61\u3002\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u6709\u591a\u4e2a\u8bb0\u5f55\u5668\uff0c\u6bcf\u4e2a\u8bb0\u5f55\u5668\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u914d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u8bb0\u5f55\u5668<\/strong><br \/>\u4f7f\u7528<code>logging.getLogger(name)<\/code>\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a\u8bb0\u5f55\u5668\u3002\u5982\u679c\u4e0d\u6307\u5b9a<code>name<\/code>\uff0c\u5219\u8fd4\u56de\u6839\u8bb0\u5f55\u5668\u3002\u6bcf\u4e2a\u8bb0\u5f55\u5668\u90fd\u6709\u81ea\u5df1\u7684\u65e5\u5fd7\u7ea7\u522b\u548c\u5904\u7406\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><br \/>\u901a\u8fc7\u8bb0\u5f55\u5668\u7684<code>debug()<\/code>, <code>info()<\/code>, <code>warning()<\/code>, <code>error()<\/code>, <code>critical()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u3002\u8bb0\u5f55\u5668\u4f1a\u6839\u636e\u5176\u7ea7\u522b\u548c\u5904\u7406\u5668\u7684\u8bbe\u7f6e\uff0c\u5c06\u65e5\u5fd7\u8bb0\u5f55\u4f20\u9012\u7ed9\u76f8\u5e94\u7684\u5904\u7406\u5668\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u65e5\u5fd7\u914d\u7f6e\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u548c\u7ef4\u62a4\u65e5\u5fd7\u914d\u7f6e\uff0c\u53ef\u4ee5\u5c06\u65e5\u5fd7\u914d\u7f6e\u5199\u5165\u914d\u7f6e\u6587\u4ef6\u4e2d\u3002Python\u652f\u6301\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u65e5\u5fd7\u914d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>INI\u683c\u5f0f\u914d\u7f6e\u6587\u4ef6<\/strong><br \/>\u4f7f\u7528<code>logging.config.fileConfig()<\/code>\u53ef\u4ee5\u52a0\u8f7dINI\u683c\u5f0f\u7684\u65e5\u5fd7\u914d\u7f6e\u6587\u4ef6\u3002\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86\u8bb0\u5f55\u5668\u3001\u5904\u7406\u5668\u3001\u683c\u5f0f\u5316\u5668\u7b49\u7684\u914d\u7f6e\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u8c03\u6574\u65e5\u5fd7\u914d\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178\u683c\u5f0f\u914d\u7f6e\u6587\u4ef6<\/strong><br \/>\u901a\u8fc7<code>logging.config.dictConfig()<\/code>\u53ef\u4ee5\u52a0\u8f7d\u5b57\u5178\u683c\u5f0f\u7684\u65e5\u5fd7\u914d\u7f6e\u3002\u5b57\u5178\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u66f4\u9002\u5408\u7528\u4e8e\u52a8\u6001\u751f\u6210\u65e5\u5fd7\u914d\u7f6e\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528YAML\u6216JSON\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u65e5\u5fd7\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u4f7f\u7528\u65e5\u5fd7\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8c03\u8bd5\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b<\/strong><br \/>\u6839\u636e\u65e5\u5fd7\u4fe1\u606f\u7684\u91cd\u8981\u7a0b\u5ea6\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b\u3002\u8c03\u8bd5\u4fe1\u606f\u4f7f\u7528DEBUG\uff0c\u6b63\u5e38\u64cd\u4f5c\u4fe1\u606f\u4f7f\u7528INFO\uff0c\u8b66\u544a\u4fe1\u606f\u4f7f\u7528WARNING\uff0c\u9519\u8bef\u4fe1\u606f\u4f7f\u7528ERROR\uff0c\u4e25\u91cd\u9519\u8bef\u4f7f\u7528CRITICAL\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u8fc7\u591a\u65e5\u5fd7\u8bb0\u5f55<\/strong><br \/>\u8fc7\u591a\u7684\u65e5\u5fd7\u8bb0\u5f55\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\uff0c\u5e76\u4e14\u96be\u4ee5\u4ece\u4e2d\u627e\u5230\u6709\u7528\u7684\u4fe1\u606f\u3002\u5e94\u53ea\u8bb0\u5f55\u5fc5\u8981\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u907f\u514d\u8bb0\u5f55\u65e0\u7528\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u62a4\u654f\u611f\u4fe1\u606f<\/strong><br \/>\u5728\u65e5\u5fd7\u4e2d\u907f\u514d\u8bb0\u5f55\u654f\u611f\u4fe1\u606f\uff0c\u5982\u7528\u6237\u5bc6\u7801\u3001\u4e2a\u4eba\u9690\u79c1\u6570\u636e\u7b49\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u5668\u6216\u683c\u5f0f\u5316\u5668\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u671f\u6e05\u7406\u65e5\u5fd7\u6587\u4ef6<\/strong><br \/>\u5b9a\u671f\u6e05\u7406\u6216\u5f52\u6863\u65e5\u5fd7\u6587\u4ef6\uff0c\u4ee5\u9632\u6b62\u78c1\u76d8\u7a7a\u95f4\u8017\u5c3d\u3002\u53ef\u4ee5\u4f7f\u7528\u65e5\u5fd7\u6eda\u52a8\u7b56\u7565\u81ea\u52a8\u7ba1\u7406\u65e5\u5fd7\u6587\u4ef6\u7684\u5927\u5c0f\u548c\u6570\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u548c\u4f7f\u7528Python\u7684<code>logging<\/code>\u6a21\u5757\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u76d1\u63a7\u548c\u8c03\u8bd5\u7a0b\u5e8f\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u57fa\u672c\u914d\u7f6e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>logging<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u57fa\u672c\u914d\u7f6e\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>logging.basicConfig()<\/code>\u6765\u6307\u5b9a\u65e5\u5fd7\u7684\u7ea7\u522b\u3001\u683c\u5f0f\u548c\u8f93\u51fa\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3aDEBUG\uff0c\u4ee5\u4fbf\u6355\u6349\u6240\u6709\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u540c\u65f6\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\u6765\u5305\u542b\u65f6\u95f4\u6233\u548c\u65e5\u5fd7\u7ea7\u522b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import logging\n\nlogging.basicConfig(level=logging.DEBUG,\n                    format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u65e5\u5fd7\u8bb0\u5f55\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b\uff1f<\/strong><br \/>\u5728Python\u7684\u65e5\u5fd7\u8bb0\u5f55\u4e2d\uff0c\u5e38\u89c1\u7684\u65e5\u5fd7\u7ea7\u522b\u5305\u62ecDEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\u3002\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\u3002DEBUG\u9002\u7528\u4e8e\u5f00\u53d1\u9636\u6bb5\uff0c\u4fbf\u4e8e\u8c03\u8bd5\uff1bINFO\u9002\u5408\u8bb0\u5f55\u6b63\u5e38\u64cd\u4f5c\u7684\u4fe1\u606f\uff1bWARNING\u7528\u4e8e\u8b66\u544a\u6f5c\u5728\u7684\u95ee\u9898\uff1bERROR\u5219\u8bb0\u5f55\u9519\u8bef\u4e8b\u4ef6\uff0c\u53ef\u80fd\u5bfc\u81f4\u529f\u80fd\u5931\u6548\uff1bCRITICAL\u8868\u793a\u4e25\u91cd\u9519\u8bef\uff0c\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u7ec8\u6b62\u3002\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u7ea7\u522b\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u800c\u4e0d\u662f\u63a7\u5236\u53f0\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728<code>basicConfig()<\/code>\u4e2d\u6307\u5b9a<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,\n                    format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6240\u6709\u7684\u65e5\u5fd7\u4fe1\u606f\u5c31\u4f1a\u88ab\u5199\u5165\u5230\u6307\u5b9a\u7684<code>app.log<\/code>\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u65e5\u5fd7\u7684\u6301\u4e45\u5316\u548c\u540e\u7eed\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d:\u5728Python\u4e2d\uff0c\u4f7f\u7528\u65e5\u5fd7\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u8c03\u8bd5\u548c\u76d1\u63a7\u624b\u6bb5\u3002\u53ef\u4ee5\u901a\u8fc7logging\u6a21\u5757\u8bb0\u5f55\u7a0b\u5e8f\u8fd0\u884c\u4fe1\u606f [&hellip;]","protected":false},"author":3,"featured_media":938475,"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\/938469"}],"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=938469"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938469\/revisions"}],"predecessor-version":[{"id":938476,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938469\/revisions\/938476"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/938475"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=938469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=938469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=938469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}