{"id":172861,"date":"2022-04-22T07:36:06","date_gmt":"2022-04-21T23:36:06","guid":{"rendered":"http:\/\/www.idc.net\/help\/172861\/"},"modified":"2022-04-22T07:36:06","modified_gmt":"2022-04-21T23:36:06","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%a8logging%e6%8a%8a%e6%97%a5%e5%bf%97%e8%be%93%e5%87%ba%e5%88%b0%e6%96%87%e4%bb%b6%e5%92%8c%e6%8e%a7","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/172861\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u4e2d\u5982\u4f55\u7528logging\u628a\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u548c\u63a7\u5236\u53f0\uff1f"},"content":{"rendered":"<p style=\"text-align: center\"><span style=\"font-size: 16px\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/314\/219\/564\/1607497077325054.png\" class=\"aligncenter\"><\/span><\/p>\n<p><span style=\"font-size: 16px\">\u8fd8\u8bb0\u5f97\u5c0f\u7f16\u5c0f\u65f6\u5019\uff0c\u53d1\u751f\u4ec0\u4e48\u4e8b\u60c5\uff0c\u9047\u5230\u4ec0\u4e48\u59d4\u5c48\u6216\u8005\u9ad8\u5174\u7684\u4e8b\u60c5\uff0c\u559c\u6b22\u5199\u5728QQ\u65e5\u5fd7\u4e2d\u8868\u8fbe\u81ea\u5df1\u7684\u60c5\u7eea\u3002\u867d\u7136QQ\u65e5\u5fd7\u73b0\u5728\u5df2\u7ecf\u4e0d\u4f7f\u7528\uff0c\u4f46\u662f\u65e5\u5fd7\u7684\u4f5c\u7528\u8fd8\u5728\u53d1\u6325\u3002\u5728\u8ba1\u7b97\u673a\u91cc\uff0c\u6211\u4eec\u53ef\u4ee5\u65e5\u5fd7\u91cc\u8bb0\u5f55\u81ea\u5df1\u7684\u65e5\u5e38\u7410\u4e8b\u6216\u8005\u662f\u5199\u7684\u4ee3\u7801\u3002\u90a3\u4f60\u77e5\u9053\u5982\u548c\u5c06\u5199\u597d\u7684\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u548c\u63a7\u5236\u53f0\uff0c\u4fbf\u4e8e\u6211\u4eec\u5b58\u50a8\u4f7f\u7528\u5417\uff1f\u672c\u6587\u5c0f\u7f16\u5c06\u7b80\u5355\u5411\u5927\u5bb6\u4ecb\u7ecdpython\u4e2d\u901a\u7528\u7684\u65e5\u5fd7\u7cfb\u7edf<span style=\"font-size: 16px\">logging\u6a21\u5757<\/span>\uff0c\u5e76\u5411\u5927\u5bb6\u6f14\u793a\u7528logging\u6a21\u5757\u628a\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u548c\u63a7\u5236\u53f0\u7684\u8fc7\u7a0b\u3002<\/span><\/p>\n<p><strong><span>1\u3001python\u7684logging\u6a21\u5757<\/span><\/strong><\/p>\n<p>python\u7684logging\u6a21\u5757<span>\u63d0\u4f9b\u4e86\u901a\u7528\u7684\u65e5\u5fd7\u7cfb\u7edf\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7b2c\u4e09\u65b9\u6a21\u5757\u6216\u8005\u662f\u5e94\u7528\u4f7f\u7528\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u5e76\u53ef\u4ee5\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u5f0f\u8bb0\u5f55\u65e5\u5fd7\uff0c\u6bd4\u5982\u6587\u4ef6\uff0cHTTP GET\/POST\uff0cSMTP\uff0cSocket\u7b49\uff0c\u751a\u81f3\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u5177\u4f53\u7684\u65e5\u5fd7\u8bb0\u5f55\u65b9\u5f0f\u3002<\/span><\/p>\n<p><strong><span>2\u3001logging\u6a21\u5757\u57fa\u672c\u4f7f\u7528<\/span><\/strong><\/p>\n<pre>import&nbsp;logging\n\nlogging.basicConfig()\nlogging.debug('This&nbsp;is&nbsp;a&nbsp;debug&nbsp;message')\nlogging.info('This&nbsp;is&nbsp;an&nbsp;info&nbsp;message')\nlogging.warning('This&nbsp;is&nbsp;a&nbsp;warning&nbsp;message')\nlogging.error('This&nbsp;is&nbsp;an&nbsp;error&nbsp;message')\nlogging.critical('This&nbsp;is&nbsp;a&nbsp;critical&nbsp;message')<\/pre>\n<p><span><strong>3\u3001\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u548c\u63a7\u5236\u53f0<\/strong><\/span><\/p>\n<pre>import&nbsp;logging\n\nLOG_FILE&nbsp;=&nbsp;'mylog.log'\n\nfile_handler&nbsp;=&nbsp;logging.FileHandler(LOG_FILE)&nbsp;#\u8f93\u51fa\u5230\u6587\u4ef6\nconsole_handler&nbsp;=&nbsp;logging.StreamHandler()&nbsp;&nbsp;#\u8f93\u51fa\u5230\u63a7\u5236\u53f0\nfile_handler.setLevel('ERROR')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#error\u4ee5\u4e0a\u624d\u8f93\u51fa\u5230\u6587\u4ef6\nconsole_handler.setLevel('INFO')&nbsp;&nbsp;&nbsp;#info\u4ee5\u4e0a\u624d\u8f93\u51fa\u5230\u63a7\u5236\u53f0\n\nfmt&nbsp;=&nbsp;'%(asctime)s&nbsp;-&nbsp;%(funcName)s&nbsp;-&nbsp;%(lineno)s&nbsp;-&nbsp;%(levelname)s&nbsp;-&nbsp;%(message)s'&nbsp;&nbsp;\nformatter&nbsp;=&nbsp;logging.Formatter(fmt)&nbsp;\nfile_handler.setFormatter(formatter)&nbsp;#\u8bbe\u7f6e\u8f93\u51fa\u5185\u5bb9\u7684\u683c\u5f0f\nconsole_handler.setFormatter(formatter)\n\nlogger&nbsp;=&nbsp;logging.getLogger('updateSecurity')\nlogger.setLevel('DEBUG')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#\u8bbe\u7f6e\u4e86\u8fd9\u4e2a\u624d\u4f1a\u628adebug\u4ee5\u4e0a\u7684\u8f93\u51fa\u5230\u63a7\u5236\u53f0\n\nlogger.addHandler(file_handler)&nbsp;&nbsp;&nbsp;&nbsp;#\u6dfb\u52a0handler\nlogger.addHandler(console_handler)<\/pre>\n<p><span style=\"font-size: 16px\">\u4ee5\u4e0a\u5c31\u662f\u5bf9python\u7684logging\u6a21\u5757\u7684\u4ecb\u7ecd\u548c\u7528logging\u6a21\u5757\u628a\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u548c\u63a7\u5236\u53f0\u7684\u8fc7\u7a0b\uff0c\u5e0c\u671b\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u54e6~<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd8\u8bb0\u5f97\u5c0f\u7f16\u5c0f\u65f6\u5019\uff0c\u53d1\u751f\u4ec0\u4e48\u4e8b\u60c5\uff0c\u9047\u5230\u4ec0\u4e48\u59d4\u5c48\u6216\u8005\u9ad8\u5174\u7684\u4e8b\u60c5\uff0c\u559c\u6b22\u5199\u5728QQ\u65e5\u5fd7\u4e2d\u8868\u8fbe\u81ea\u5df1\u7684\u60c5\u7eea\u3002\u867d\u7136QQ\u65e5\u5fd7\u73b0\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":172862,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-172861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/172861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=172861"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/172861\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/172862"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=172861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=172861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=172861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}