{"id":1007167,"date":"2024-12-27T10:50:55","date_gmt":"2024-12-27T02:50:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007167.html"},"modified":"2024-12-27T10:50:59","modified_gmt":"2024-12-27T02:50:59","slug":"python%e5%a6%82%e4%bd%95%e5%85%b3%e9%97%ad%e6%97%a5%e5%bf%97log","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007167.html","title":{"rendered":"python\u5982\u4f55\u5173\u95ed\u65e5\u5fd7log"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083009\/a710a30b-8a7e-43b7-9d31-6fcec6812ee8.webp\" alt=\"python\u5982\u4f55\u5173\u95ed\u65e5\u5fd7log\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5173\u95ed\u65e5\u5fd7\u8bb0\u5f55\u6709\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\uff1a\u7981\u7528\u65e5\u5fd7\u3001\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u3001\u79fb\u9664\u5904\u7406\u5668\u7b49\u3002<\/strong><br \/>\u8981\u66f4\u8be6\u7ec6\u5730\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u5c06\u89e3\u91ca\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u5173\u95ed\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u7981\u7528\u65e5\u5fd7<\/p>\n<\/p>\n<p><p>\u7981\u7528\u65e5\u5fd7\u8bb0\u5f55\u662f\u5173\u95ed\u65e5\u5fd7\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5728Python\u7684<code>logging<\/code>\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3a<code>logging.CRITICAL<\/code>\u6765\u6709\u6548\u5730\u7981\u7528\u5176\u4ed6\u7ea7\u522b\u7684\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.CRITICAL)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ea\u6709<code>CRITICAL<\/code>\u7ea7\u522b\u7684\u65e5\u5fd7\u4f1a\u88ab\u8bb0\u5f55\uff0c\u800c\u5176\u4ed6\u6240\u6709\u7ea7\u522b\u7684\u65e5\u5fd7\u90fd\u5c06\u88ab\u5ffd\u7565\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u548c\u751f\u4ea7\u73af\u5883\u4e2d\u7684\u65e5\u5fd7\u7ba1\u7406\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b<\/p>\n<\/p>\n<p><p>Python\u7684<code>logging<\/code>\u6a21\u5757\u4e2d\u63d0\u4f9b\u4e86\u591a\u4e2a\u65e5\u5fd7\u7ea7\u522b\uff1a<code>DEBUG<\/code>\u3001<code>INFO<\/code>\u3001<code>WARNING<\/code>\u3001<code>ERROR<\/code>\u548c<code>CRITICAL<\/code>\u3002\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff0c\u53ef\u4ee5\u63a7\u5236\u8bb0\u5f55\u54ea\u4e9b\u7ea7\u522b\u7684\u65e5\u5fd7\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u53ea\u8bb0\u5f55<code>ERROR<\/code>\u548c<code>CRITICAL<\/code>\u7ea7\u522b\u7684\u65e5\u5fd7\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u65e5\u5fd7\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.ERROR)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u662f\u901a\u8fc7<code>basicConfig<\/code>\u51fd\u6570\u8bbe\u7f6e\u7684\u57fa\u7840\u914d\u7f6e\u3002\u901a\u8fc7\u8fd9\u6837\u7684\u8bbe\u7f6e\uff0c\u60a8\u53ef\u4ee5\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u4ece\u800c\u8fbe\u5230\u5173\u95ed\u90e8\u5206\u65e5\u5fd7\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u79fb\u9664\u5904\u7406\u5668<\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u66f4\u7cbe\u7ec6\u5730\u63a7\u5236\u65e5\u5fd7\u7684\u8f93\u51fa\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u65e5\u5fd7\u5904\u7406\u5668\uff08Handler\uff09\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u901a\u8fc7\u79fb\u9664\u5904\u7406\u5668\u6765\u5b8c\u5168\u505c\u6b62\u65e5\u5fd7\u7684\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logger = logging.getLogger()<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aStreamHandler<\/strong><\/h2>\n<p>stream_handler = logging.StreamHandler()<\/p>\n<p>logger.addHandler(stream_handler)<\/p>\n<h2><strong>\u79fb\u9664\u6240\u6709\u5904\u7406\u5668\u6765\u5173\u95ed\u65e5\u5fd7<\/strong><\/h2>\n<p>logger.removeHandler(stream_handler)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u79fb\u9664\u5904\u7406\u5668\uff0c\u65e5\u5fd7\u5c06\u4e0d\u4f1a\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u6216\u6587\u4ef6\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7075\u6d3b\u5730\u7ba1\u7406\u4e0d\u540c\u7684\u65e5\u5fd7\u8f93\u51fa\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7a7a\u5904\u7406\u5668<\/p>\n<\/p>\n<p><p>\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528\u7a7a\u5904\u7406\u5668\uff08NullHandler\uff09\u6765\u6291\u5236\u65e5\u5fd7\u8f93\u51fa\u3002\u7a7a\u5904\u7406\u5668\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5904\u7406\u5668\uff0c\u5b83\u4e0d\u4f1a\u8f93\u51fa\u4efb\u4f55\u65e5\u5fd7\u4fe1\u606f\u3002\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0<code>NullHandler<\/code>\u6765\u8fbe\u5230\u5173\u95ed\u65e5\u5fd7\u7684\u76ee\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e2aNullHandler<\/strong><\/h2>\n<p>null_handler = logging.NullHandler()<\/p>\n<p>logger = logging.getLogger()<\/p>\n<p>logger.addHandler(null_handler)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5e93\u5f00\u53d1\u8005\uff0c\u4ed6\u4eec\u53ef\u80fd\u5e0c\u671b\u5728\u4e0d\u5f71\u54cd\u7528\u6237\u65e5\u5fd7\u914d\u7f6e\u7684\u60c5\u51b5\u4e0b\u6291\u5236\u65e5\u5fd7\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u52a8\u6001\u63a7\u5236\u65e5\u5fd7<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u52a8\u6001\u5730\u63a7\u5236\u65e5\u5fd7\u7684\u8f93\u51fa\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8fd0\u884c\u65f6\u8c03\u6574\u65e5\u5fd7\u7ea7\u522b\u6216\u6dfb\u52a0\/\u79fb\u9664\u5904\u7406\u5668\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>def toggle_logging(enable):<\/p>\n<p>    logger = logging.getLogger()<\/p>\n<p>    if enable:<\/p>\n<p>        logger.setLevel(logging.DEBUG)<\/p>\n<p>    else:<\/p>\n<p>        logger.setLevel(logging.CRITICAL)<\/p>\n<h2><strong>\u542f\u7528\u65e5\u5fd7<\/strong><\/h2>\n<p>toggle_logging(True)<\/p>\n<h2><strong>\u7981\u7528\u65e5\u5fd7<\/strong><\/h2>\n<p>toggle_logging(False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u6837\u7684\u673a\u5236\uff0c\u60a8\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4e0d\u540c\u9636\u6bb5\u6839\u636e\u9700\u8981\u5f00\u542f\u6216\u5173\u95ed\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5173\u95ed\u65e5\u5fd7\u8bb0\u5f55\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u7981\u7528\u65e5\u5fd7\u3001\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u3001\u79fb\u9664\u5904\u7406\u5668\u548c\u4f7f\u7528\u7a7a\u5904\u7406\u5668\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u5e94\u7528\uff0c\u4ee5\u5b9e\u73b0\u5bf9\u65e5\u5fd7\u7684\u7cbe\u7ec6\u63a7\u5236\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u65e5\u5fd7\u8f93\u51fa\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\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\u63a7\u5236\u65e5\u5fd7\u7684\u8f93\u51fa\u7ea7\u522b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u6a21\u5757\u7684\u7ea7\u522b\u6765\u63a7\u5236\u65e5\u5fd7\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u53ea\u8bb0\u5f55WARNING\u53ca\u4ee5\u4e0a\u7ea7\u522b\u7684\u65e5\u5fd7\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logging.basicConfig(level=logging.WARNING)<\/code>\u3002\u8fd9\u6837\uff0cDEBUG\u548cINFO\u7ea7\u522b\u7684\u65e5\u5fd7\u5c06\u4e0d\u4f1a\u88ab\u8f93\u51fa\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5173\u95ed\u7279\u5b9a\u6a21\u5757\u7684\u65e5\u5fd7\u8f93\u51fa\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u5982\u679c\u53ea\u5e0c\u671b\u5173\u95ed\u7279\u5b9a\u6a21\u5757\u7684\u65e5\u5fd7\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logging.getLogger(&#39;\u6a21\u5757\u540d&#39;).setLevel(logging.ERROR)<\/code>\u3002\u901a\u8fc7\u8bbe\u7f6e\u8be5\u6a21\u5757\u7684\u65e5\u5fd7\u7ea7\u522b\u4e3aERROR\uff0c\u53ef\u4ee5\u5c4f\u853dINFO\u548cDEBUG\u7ea7\u522b\u7684\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b8c\u5168\u7981\u7528\u65e5\u5fd7\u8bb0\u5f55\uff1f<\/strong><br \/>\u8981\u5b8c\u5168\u7981\u7528\u65e5\u5fd7\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logging.disable(logging.CRITICAL)<\/code>\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7ea7\u522b\u7684\u65e5\u5fd7\u90fd\u4e0d\u4f1a\u88ab\u8f93\u51fa\uff0c\u9002\u5408\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u9700\u8981\u5f7b\u5e95\u5173\u95ed\u65e5\u5fd7\u65f6\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5173\u95ed\u65e5\u5fd7\u8bb0\u5f55\u6709\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\uff1a\u7981\u7528\u65e5\u5fd7\u3001\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u3001\u79fb\u9664\u5904\u7406\u5668\u7b49\u3002\u8981\u66f4\u8be6\u7ec6\u5730\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211 [&hellip;]","protected":false},"author":3,"featured_media":1007184,"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\/1007167"}],"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=1007167"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007167\/revisions"}],"predecessor-version":[{"id":1007187,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007167\/revisions\/1007187"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007184"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}