{"id":310264,"date":"2024-05-20T21:46:11","date_gmt":"2024-05-20T13:46:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/310264.html"},"modified":"2024-05-20T21:46:17","modified_gmt":"2024-05-20T13:46:17","slug":"python-%e4%b8%ad%e6%80%8e%e4%b9%88%e7%94%a8-logging-%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/310264.html","title":{"rendered":"python \u4e2d\u600e\u4e48\u7528 logging \u65e5\u5fd7"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26230816\/ec091a89-aef7-45b7-a462-b1db5f6550ac.webp\" alt=\"python \u4e2d\u600e\u4e48\u7528 logging \u65e5\u5fd7\" \/><\/p>\n<p><p>Python \u4e2d\u4f7f\u7528 logging \u65e5\u5fd7\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u8bb0\u5f55\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5, \u65b9\u4fbf\u8ffd\u8e2a\u9519\u8bef\u548c\u5f02\u5e38\u3002<strong>\u5177\u4f53\u505a\u6cd5\u5305\u62ec\u521b\u5efa Logger\u3001\u8bbe\u7f6e\u7ea7\u522b\u3001\u5b9a\u4e49 Handler \u548c Formatter<\/strong>\u3002\u5728\u5f00\u53d1\u65f6\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165 logging \u6a21\u5757\uff0c\u7136\u540e\u901a\u8fc7\u914d\u7f6e Logger\u3001Handler\u3001Formatter\uff0c\u5408\u7406\u5730\u8f93\u51fa\u65e5\u5fd7\u5230\u4e0d\u540c\u7684\u76ee\u7684\u5730\uff0c\u5982\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\u5668 LOGGER <\/p>\n<p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a logging \u6a21\u5757\u7684\u57fa\u7840\u8bbe\u65bd\uff0c\u5141\u8bb8\u901a\u8fc7\u521b\u5efa Logger \u5b9e\u4f8b\u6765\u5f00\u59cb\u8bb0\u5f55\u65e5\u5fd7\u3002\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668(Logger)\u5b9e\u4f8b\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u65e5\u5fd7\u5e94\u7528\u7684\u5165\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u9996\u5148\u5bfc\u5165\u4e86 logging \u6a21\u5757\uff0c\u4e4b\u540e\u521b\u5efa\u4e86\u4e00\u4e2a Logger \u5b9e\u4f8b\u5e76\u8bbe\u7f6e\u4e86\u65e5\u5fd7\u7ea7\u522b\u4e3a DEBUG\u3002Logger \u63d0\u4f9b\u4e86\u5e94\u7528\u7a0b\u5e8f\u53ef\u76f4\u63a5\u4f7f\u7528\u7684\u63a5\u53e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u65e5\u5fd7\u8bb0\u5f55\u5668\u76f4\u63a5\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b <\/p>\n<p>\u65e5\u5fd7\u7ea7\u522b\u51b3\u5b9a\u4e86\u65e5\u5fd7\u8bb0\u5f55\u7684\u4e25\u91cd\u6027\u7ea7\u522b\u3002logging \u6a21\u5757\u5b9a\u4e49\u4e86\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">logger.setLevel(logging.DEBUG)  # \u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3aDEBUG<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6709\u6548\u7684\u65e5\u5fd7\u7ea7\u522b\u5305\u62ec DEBUG\u3001INFO\u3001WARNING\u3001ERROR \u4ee5\u53ca CRITICAL\uff0c\u5206\u522b\u5bf9\u5e94\u4e0d\u540c\u5c42\u7ea7\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4ece\u8c03\u8bd5\u4fe1\u606f\u5230\u81f4\u547d\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u914d\u7f6e\u5904\u7406\u5668 HANDLER<\/p>\n<p>\u5904\u7406\u5668(Handler)\u5bf9\u8c61\u5c06\u65e5\u5fd7\u8bb0\u5f55\uff08\u6d88\u606f\uff09\u53d1\u9001\u5230\u65e5\u5fd7\u7684\u6307\u5b9a\u76ee\u7684\u5730\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u53f0\u5904\u7406\u5668\uff0c\u5e76\u8bbe\u7f6e\u5176\u65e5\u5fd7\u7ea7\u522b\u4e3aDEBUG<\/p>\n<p>console_handler = logging.StreamHandler()<\/p>\n<p>console_handler.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u53ef\u4ee5\u521b\u5efa\u5176\u4ed6\u5904\u7406\u5668\uff0c\u6bd4\u5982\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>file_handler = logging.FileHandler(filename=&#039;app.log&#039;)<\/p>\n<p>file_handler.setLevel(logging.INFO)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e86\u4e00\u4e2a\u63a7\u5236\u53f0\u5904\u7406\u5668\u548c\u4e00\u4e2a\u6587\u4ef6\u5904\u7406\u5668\uff0c\u5e76\u5206\u522b\u8bbe\u7f6e\u4e86\u5b83\u4eec\u7684\u65e5\u5fd7\u7ea7\u522b\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u4e0d\u540c\u7684\u4f4d\u7f6e\uff0c\u6bd4\u5982\u6587\u4ef6\u3001\u7f51\u7edc\u670d\u52a1\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b9a\u4e49\u683c\u5f0f\u5668 FORMATTER<\/p>\n<p>Formatter \u5bf9\u8c61\u5b9a\u4e49\u4e86\u6700\u7ec8\u8f93\u51fa\u4e2d\u65e5\u5fd7\u8bb0\u5f55\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u683c\u5f0f\u5668<\/p>\n<p>formatter = logging.Formatter(&#039;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#039;)<\/p>\n<h2><strong>\u5c06\u5b9a\u4e49\u597d\u7684\u683c\u5f0f\u5668\u6dfb\u52a0\u5230\u5904\u7406\u5668\u4e2d<\/strong><\/h2>\n<p>console_handler.setFormatter(formatter)<\/p>\n<p>file_handler.setFormatter(formatter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa Formatter \u7684\u5b9e\u4f8b\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u79cd\u7279\u5b9a\u7684\u65e5\u5fd7\u683c\u5f0f\u3002\u8fd9\u79cd\u683c\u5f0f\u5305\u542b\u65f6\u95f4\u6233\u3001\u8bb0\u5f55\u5668\u540d\u79f0\u3001\u65e5\u5fd7\u7ea7\u522b\uff0c\u4ee5\u53ca\u5b9e\u9645\u7684\u6d88\u606f\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u8bb0\u5f55\u5668 LOGGER<\/p>\n<p>\u5b8c\u6210\u4e86\u4e0a\u8ff0\u6b65\u9aa4\u540e\uff0c\u9700\u8981\u5c06 Handler \u5bf9\u8c61\u6dfb\u52a0\u5230 Logger \u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">logger.addHandler(console_handler)<\/p>\n<p>logger.addHandler(file_handler)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u5148\u524d\u5b9a\u4e49\u7684 console_handler \u548c file_handler \u6dfb\u52a0\u5230 logger \u4e2d\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86 Logger \u7684\u57fa\u672c\u914d\u7f6e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/p>\n<p>\u65e5\u5fd7\u7ea7\u522b\u4e00\u65e6\u8bbe\u7f6e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u8bb0\u5f55\u65e5\u5fd7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">logger.debug(&#039;Debugging information&#039;)<\/p>\n<p>logger.info(&#039;Informational message&#039;)<\/p>\n<p>logger.warning(&#039;Warning occurred&#039;)<\/p>\n<p>logger.error(&#039;An error has happened&#039;)<\/p>\n<p>logger.critical(&#039;Critical issue&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 logger \u5bf9\u8c61\uff0c\u6839\u636e\u4e0d\u540c\u7684\u7ea7\u522b\u8bb0\u5f55\u4e0d\u540c\u7c7b\u578b\u7684\u65e5\u5fd7\u3002\u8fd9\u4e9b\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u65e5\u5fd7\u6d88\u606f\u4f5c\u4e3a\u5fc5\u586b\u53c2\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u63d0\u4f9b\u989d\u5916\u7684\u5173\u952e\u5b57\u53c2\u6570\u7528\u4e8e\u8bb0\u5f55\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u914d\u7f6e logging \u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u65e5\u5fd7\u5316\uff0c\u6781\u5927\u5730\u63d0\u5347\u5e94\u7528\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u76d1\u63a7\u6027\u3002\u4e1a\u754c\u6807\u51c6\u5b9e\u8df5\u901a\u5e38\u4f1a\u8fdb\u4e00\u6b65\u5bf9\u65e5\u5fd7\u7cfb\u7edf\u8fdb\u884c\u62bd\u8c61\u548c\u5c01\u88c5\uff0c\u4fbf\u4e8e\u5728\u4e0d\u540c\u7684\u7a0b\u5e8f\u6a21\u5757\u95f4\u590d\u7528\u5e76\u63d0\u9ad8\u4ee3\u7801\u6574\u6d01\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528logging\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u4ec0\u4e48\u662flogging\u6a21\u5757\u548c\u4e3a\u4f55\u4f7f\u7528\u5b83\u8bb0\u5f55\u65e5\u5fd7\uff1f<\/strong><br \/>\nlogging\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u8bb0\u5f55\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u4fe1\u606f\u3002\u901a\u8fc7\u4f7f\u7528logging\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u65e5\u5fd7\u8bed\u53e5\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6355\u83b7\u548c\u8bb0\u5f55\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u65e5\u5fd7\u8bb0\u5f55\u8bed\u53e5\uff1f<\/strong><br \/>\n\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165logging\u6a21\u5757\uff1a<code>import logging<\/code>\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>logging.basicConfig()<\/code>\u65b9\u6cd5\u8fdb\u884c\u57fa\u672c\u7684\u914d\u7f6e\u8bbe\u7f6e\uff0c\u4f8b\u5982\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u540d\u79f0\u3001\u683c\u5f0f\u7b49\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>logging.debug()<\/code>\u3001<code>logging.info()<\/code>\u3001<code>logging.warning()<\/code>\u7b49\u65b9\u6cd5\u6765\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u63a7\u5236\u65e5\u5fd7\u7684\u7ea7\u522b\u548c\u8bb0\u5f55\u7684\u4fe1\u606f\uff1f<\/strong><br \/>\nlogging\u6a21\u5757\u652f\u6301\u591a\u4e2a\u65e5\u5fd7\u7ea7\u522b\uff0c\u5305\u62ecDEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\u7b49\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>logging.setLevel()<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u65e5\u5fd7\u7684\u6700\u4f4e\u7ea7\u522b\uff0c\u4ece\u800c\u63a7\u5236\u65e5\u5fd7\u7684\u8f93\u51fa\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>logging.getLogger()<\/code>\u65b9\u6cd5\u521b\u5efa\u6216\u83b7\u53d6\u4e00\u4e2alogger\u5bf9\u8c61\uff0c\u5728\u5176\u4e2d\u8bbe\u7f6e\u8bb0\u5f55\u7684\u683c\u5f0f\u3001\u65e5\u5fd7\u5904\u7406\u7a0b\u5e8f\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5c06\u65e5\u5fd7\u8bb0\u5f55\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\n\u4f7f\u7528<code>logging.basicConfig()<\/code>\u65b9\u6cd5\u4e2d\u7684<code>filename<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06\u65e5\u5fd7\u8bb0\u5f55\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u6765\u6307\u5b9a\u6587\u4ef6\u7684\u4f4d\u7f6e\u548c\u540d\u79f0\u3002\u5982\u679c\u60a8\u6ca1\u6709\u6307\u5b9a\u6587\u4ef6\u540d\uff0c\u5219\u65e5\u5fd7\u5c06\u88ab\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u540c\u6a21\u5757\u4e2d\u5171\u4eab\u65e5\u5fd7\u914d\u7f6e\uff1f<\/strong><br \/>\n\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u914d\u7f6e\u65e5\u5fd7\uff0c\u7136\u540e\u5c06\u6b64\u914d\u7f6e\u5bfc\u5165\u5176\u4ed6\u6a21\u5757\uff0c\u4ee5\u4fbf\u5728\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5171\u4eab\u76f8\u540c\u7684\u65e5\u5fd7\u8bbe\u7f6e\u3002\u8fd9\u6837\uff0c\u4e0d\u540c\u6a21\u5757\u4e2d\u7684\u65e5\u5fd7\u4fe1\u606f\u5c06\u4e00\u81f4\uff0c\u5e76\u9075\u5faa\u76f8\u540c\u7684\u683c\u5f0f\u548c\u7ea7\u522b\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u603b\u4e4b\uff0c\u4f7f\u7528logging\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u548c\u8c03\u8bd5\u4ee3\u7801\uff0c\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5728\u65e5\u540e\u6392\u67e5\u95ee\u9898\u65f6\u66f4\u8f7b\u677e\u5730\u5b9a\u4f4d\u548c\u89e3\u51b3\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u4e2d\u4f7f\u7528 logging \u65e5\u5fd7\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u8bb0\u5f55\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5, \u65b9\u4fbf\u8ffd\u8e2a\u9519\u8bef\u548c\u5f02\u5e38\u3002\u5177\u4f53\u505a\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":310270,"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\/310264"}],"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=310264"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/310264\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/310270"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=310264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=310264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=310264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}