{"id":1729,"date":"2023-07-26T14:49:59","date_gmt":"2023-07-26T06:49:59","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=1729"},"modified":"2023-08-23T08:58:31","modified_gmt":"2023-08-23T00:58:31","slug":"python%e6%97%a5%e5%bf%97%ef%bc%9alogging%e6%a8%a1%e5%9d%97%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2023\/07\/26\/python%e6%97%a5%e5%bf%97%ef%bc%9alogging%e6%a8%a1%e5%9d%97%e4%bd%bf%e7%94%a8\/","title":{"rendered":"python\u65e5\u5fd7\uff1alogging\u6a21\u5757\u4f7f\u7528"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u4f55\u65f6\u4f7f\u7528logging<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>\u60f3\u8981\u6267\u884c\u7684\u4efb\u52a1<\/th><th>\u5de5\u5177<\/th><\/tr><tr><td>\u5bf9\u4e8e\u547d\u4ee4\u884c\u6216\u7a0b\u5e8f\u7684\u5e94\u7528\uff0c\u7ed3\u679c\u663e\u793a\u5728\u63a7\u5236\u53f0\u3002<\/td><td>print()<\/td><\/tr><tr><td>\u5728\u5bf9\u7a0b\u5e8f\u7684\u666e\u901a\u64cd\u4f5c\u53d1\u751f\u65f6\u63d0\u4ea4\u4e8b\u4ef6\u62a5\u544a (\u6bd4\u5982\uff1a\u72b6\u6001\u76d1\u63a7\u548c\u9519\u8bef\u8c03\u67e5)<\/td><td><a href=\"https:\/\/link.zhihu.com\/?target=http%3A\/\/logging.info%28%29\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/logging.info()<\/a>&nbsp;\u51fd\u6570 (\u5f53\u6709\u8bca\u65ad\u76ee\u7684\u9700\u8981\u8be6\u7ec6\u8f93\u51fa\u4fe1\u606f\u65f6\u4f7f\u7528<br>logging.debug() \u51fd\u6570)<\/td><\/tr><tr><td>\u63d0\u51fa\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f\u57fa\u4e8e\u4e00\u4e2a\u7279\u6b8a\u7684\u8fd0\u884c\u65f6\u4e8b\u4ef6<\/td><td>warnings.warn() \u4f4d\u4e8e\u4ee3\u7801\u5e93\u4e2d\uff0c\u8be5\u4e8b\u4ef6\u662f\u53ef\u4ee5\u907f\u514d\u7684\uff0c\u9700\u8981\u4fee\u6539<br>\u5ba2\u6237\u7aef\u5e94\u7528\u4ee5\u6d88\u9664\u544a\u8b66logging.warning() \u4e0d\u9700\u8981\u4fee\u6539\u5ba2\u6237\u7aef\u5e94\u7528\uff0c\u4f46\u662f\u8be5\u4e8b\u4ef6\u8fd8\u662f\u9700\u8981\u5f15\u8d77\u5173\u6ce8<\/td><\/tr><tr><td>\u62a5\u544a\u9519\u8bef\u800c\u4e0d\u5f15\u53d1\u5f02\u5e38 (\u5982\u5728\u957f\u65f6\u95f4\u8fd0\u884c\u4e2d\u7684\u670d\u52a1\u7aef\u8fdb\u7a0b\u7684\u9519\u8bef\u5904\u7406)<\/td><td>logging.error(),<br>logging.exception() \u6216 logging.critical() \u5206\u522b\u9002\u7528\u4e8e\u7279\u5b9a\u7684\u9519\u8bef\u53ca\u5e94\u7528\u9886\u57df<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65e5\u5fd7\u4e8b\u4ef6\u7ea7\u522b<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>\u7ea7\u522b<\/th><th>\u4f55\u65f6\u4f7f\u7528<\/th><\/tr><tr><td>DEBUG<\/td><td>\u7ec6\u8282\u4fe1\u606f\uff0c\u4ec5\u5f53\u8bca\u65ad\u95ee\u9898\u65f6\u9002\u7528\u3002<\/td><\/tr><tr><td>INFO<\/td><td>\u786e\u8ba4\u7a0b\u5e8f\u6309\u9884\u671f\u8fd0\u884c<\/td><\/tr><tr><td>WARNING<\/td><td>\u8868\u660e\u6709\u5df2\u7ecf\u6216\u5373\u5c06\u53d1\u751f\u7684\u610f\u5916\uff08\u4f8b\u5982\uff1a\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\uff09\u3002\u7a0b\u5e8f\u4ecd\u6309\u9884\u671f\u8fdb\u884c<\/td><\/tr><tr><td>ERROR<\/td><td>\u7531\u4e8e\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u7a0b\u5e8f\u7684\u67d0\u4e9b\u529f\u80fd\u5df2\u7ecf\u4e0d\u80fd\u6b63\u5e38\u6267\u884c<\/td><\/tr><tr><td>CRITICAL<\/td><td>\u4e25\u91cd\u7684\u9519\u8bef\uff0c\u8868\u660e\u7a0b\u5e8f\u5df2\u4e0d\u80fd\u7ee7\u7eed\u6267\u884c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u9ed8\u8ba4\u7684\u7ea7\u522b\u662f \u201cWARNING\u201c\uff0c\u610f\u5473\u7740\u53ea\u4f1a\u8ffd\u8e2a\u8be5\u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u4e8b\u4ef6\uff0c\u9664\u975e\u66f4\u6539\u65e5\u5fd7\u914d\u7f6e\u3002\u6240\u8ffd\u8e2a\u4e8b\u4ef6\u53ef\u4ee5\u4ee5\u4e0d\u540c\u5f62\u5f0f\u5904\u7406\u3002\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u5199\u5165\u78c1\u76d8\u6587\u4ef6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>basicConfig()\u8bbe\u7f6e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding:utf-8 -*-\n\nimport logging\n\n#\u9ed8\u8ba4\u7684warning\u7ea7\u522b\uff0c\u53ea\u8f93\u51fawarning\u4ee5\u4e0a\u7684\n#\u4f7f\u7528basicConfig()\u6765\u6307\u5b9a\u65e5\u5fd7\u7ea7\u522b\u548c\u76f8\u5173\u4fe1\u606f\n\nlogging.basicConfig(level=logging.DEBUG #\u8bbe\u7f6e\u65e5\u5fd7\u8f93\u51fa\u683c\u5f0f\n                    ,filename=\"demo.log\" #log\u65e5\u5fd7\u8f93\u51fa\u7684\u6587\u4ef6\u4f4d\u7f6e\u548c\u6587\u4ef6\u540d\n                    ,filemode=\"w\" #\u6587\u4ef6\u7684\u5199\u5165\u683c\u5f0f\uff0cw\u4e3a\u91cd\u65b0\u5199\u5165\u6587\u4ef6\uff0c\u9ed8\u8ba4\u662f\u8ffd\u52a0\n                    ,format=\"%(asctime)s - %(name)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s\" #\u65e5\u5fd7\u8f93\u51fa\u7684\u683c\u5f0f\n                    # -8\u8868\u793a\u5360\u4f4d\u7b26\uff0c\u8ba9\u8f93\u51fa\u5de6\u5bf9\u9f50\uff0c\u8f93\u51fa\u957f\u5ea6\u90fd\u4e3a8\u4f4d\n                    ,datefmt=\"%Y-%m-%d %H:%M:%S\" #\u65f6\u95f4\u8f93\u51fa\u7684\u683c\u5f0f\n                    )\n\nlogging.debug(\"This is  DEBUG !!\")\nlogging.info(\"This is  INFO !!\")\nlogging.warning(\"This is  WARNING !!\")\nlogging.error(\"This is  ERROR !!\")\nlogging.critical(\"This is  CRITICAL !!\")\n\n#\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6355\u83b7\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u5982\u679c\u4f7f\u7528logging.error(e)\uff0c\u53ea\u63d0\u793a\u6307\u5b9a\u7684logging\u4fe1\u606f\uff0c\u4e0d\u4f1a\u51fa\u73b0\n#\u4e3a\u4ec0\u4e48\u4f1a\u9519\u7684\u4fe1\u606f\uff0c\u6240\u4ee5\u8981\u4f7f\u7528logging.exception(e)\u53bb\u8bb0\u5f55\u3002\n\ntry:\n    3\/0\nexcept Exception as e:\n    # logging.error(e)\n    logging.exception(e)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u65e5\u5fd7\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2021-03-26 02:03:01 - root - DEBUG     - basiclogging : 16 line - This is  DEBUG !!\n2021-03-26 02:03:01 - root - INFO      - basiclogging : 17 line - This is  INFO !!\n2021-03-26 02:03:01 - root - WARNING   - basiclogging : 18 line - This is  WARNING !!\n2021-03-26 02:03:01 - root - ERROR     - basiclogging : 19 line - This is  ERROR !!\n2021-03-26 02:03:01 - root - CRITICAL  - basiclogging : 20 line - This is  CRITICAL !!\n\n2021-03-26 03:16:47 - root - ERROR     - basiclogging : 27 line - division by zero\nTraceback (most recent call last):\n  File \"E:\/pycharm_project\/examples-of-web-crawlers-master\/logginng\/basiclogging\", line 24, in &lt;module>\n    3\/0\nZeroDivisionError: division by zero\n\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>logging\u7684\u9ad8\u7ea7\u5e94\u7528<\/strong><\/h3>\n\n\n\n<p><strong>logging\u91c7\u7528\u4e86\u6a21\u5757\u5316\u8bbe\u8ba1\uff0c\u4e3b\u8981\u5305\u542b\u56db\u79cd\u7ec4\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<p><strong>Loggers:<\/strong>\u8bb0\u5f55\u5668\uff0c\u63d0\u4f9b\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u80fd\u76f4\u63a5\u4f7f\u7528\u7684\u63a5\u53e3\u3002\uff08\u7c7b\u4f3c\u4e8e\u7b14\uff0c\u4f7f\u7528\u4e0d\u540c\u7684\u7b14\u8bb0\u5f55\u4e0d\u540c\u7684\u65e5\u5fd7\uff0c\u4f8b\u5982\u4e00\u4e2a\u9879\u76ee\u91cc\u6709\u5f88\u591a\u529f\u80fd\u6a21\u5757\uff0c\u6bcf\u4e2a\u6a21\u5757\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684Logger\u53bb\u8bb0\u5f55\uff0c\u7136\u540e\u8bbe\u7f6e\u6bcf\u53ea\u7b14\u7684\u4e0d\u540c\u683c\u5f0f\uff09<\/p>\n\n\n\n<p><strong>Handlers:<\/strong>\u5904\u7406\u5668\uff0c\u5c06\u8bb0\u5f55\u5668\u4ea7\u751f\u7684\u65e5\u5fd7\u53d1\u9001\u5230\u76ee\u7684\u5730\u3002\uff08\u8f93\u51fa\u5230\u6587\u4ef6\u6216\u8005\u63a7\u5236\u53f0\u6216\u8005\u90ae\u4ef6\u7b49\uff0c\u4e00\u4e2a\u8bb0\u5f55\u5668\u53ef\u4ee5\u5bf9\u5e94\u591a\u4e2a\u5904\u7406\u5668\u3002\uff09<\/p>\n\n\n\n<p><strong>Filters:<\/strong>\u8fc7\u6ee4\u5668\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u7c92\u5ea6\u63a7\u5236\uff0c\u51b3\u5b9a\u54ea\u4e9b\u65e5\u5fd7\u4f1a\u88ab\u8f93\u51fa\u3002<\/p>\n\n\n\n<p><strong>Formatters:<\/strong>\u683c\u5f0f\u5316\u5668\uff0c\u8bbe\u7f6e\u65e5\u5fd7\u5185\u5bb9\u7684\u7ec4\u6210\u7ed3\u6784\u548c\u6d88\u606f\u5b57\u6bb5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic3.zhimg.com\/80\/v2-9763fa4654257d5108d8287afb1852e6_720w.webp\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Loggers\u8bb0\u5f55\u5668<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u63d0\u4f9b\u5e94\u7528\u7a0b\u5e8f\u7684\u8c03\u7528\u63a5\u53e3<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>logger = logging.getLogger(__name__)\n#logger\u662f\u5355\u4f8b\u7684\uff0c\u4e5f\u5c31\u662f__name__\u4e0d\u53d8\uff0c\u83b7\u53d6\u5230\u7684logger\u90fd\u662f\u540c\u4e00\u4e2a<\/code><\/pre>\n\n\n\n<p>2.\u51b3\u5b9a\u65e5\u5fd7\u8bb0\u5f55\u7684\u7ea7\u522b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>logger.setLevel()<\/code><\/pre>\n\n\n\n<p>3.\u5c06\u65e5\u5fd7\u5185\u5bb9\u4f20\u9012\u5230\u76f8\u5173\u8054\u7684handlers\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>logger.addHandler()\nlogger.removeHandler()<\/code><\/pre>\n\n\n\n<p><strong>Handler\u5904\u7406\u5668<\/strong><\/p>\n\n\n\n<p>\u5c06\u65e5\u5fd7\u53d1\u9001\u5230\u4e0d\u540c\u7684\u5730\u65b9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5e38\u89c1\u7684\u5904\u7406\u5668\nStreamHandler #\u5c4f\u5e55\u8f93\u51fa\nFileHandler #\u6587\u4ef6\u8bb0\u5f55\nBaseRotatingHandler #\u6807\u51c6\u7684\u5206\u5272\u6587\u4ef6\u65e5\u5fd7\nRotatingFileHandler #\u6309\u6587\u4ef6\u5927\u5c0f\u8bb0\u5f55\u65e5\u5fd7\nTimeRotatingFileHandler #\u6309\u65f6\u95f4\u8bb0\u5f55\u65e5\u5fd7\n<\/code><\/pre>\n\n\n\n<p><strong>StreamHandler<\/strong><\/p>\n\n\n\n<p>\u6807\u51c6\u8f93\u51fa\u5206\u53d1\u5668\uff0c\u4e5f\u5c31\u662f\u5c4f\u5e55\u663e\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sh = logging.StreamHandler(stream=None)<\/code><\/pre>\n\n\n\n<p><strong>FileHandler<\/strong><\/p>\n\n\n\n<p>\u5c06\u65e5\u5fd7\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fh = logging.FileHandler(filename,mode='a',encoding=None,delay=False)<\/code><\/pre>\n\n\n\n<p><strong>setFormatter()<\/strong><\/p>\n\n\n\n<p>\u8bbe\u7f6e\u5f53\u524dHandler\u5bf9\u8c61\u4f7f\u7528\u7684\u6d88\u606f\u683c\u5f0f<\/p>\n\n\n\n<p><strong>Formatters\u683c\u5f0f<\/strong><\/p>\n\n\n\n<p>formatter\u5bf9\u8c61\u7528\u6765\u6700\u7ec8\u8bbe\u7f6e\u65e5\u5fd7\u4fe1\u606f\u7684\u987a\u5e8f\uff0c\u7ed3\u6784\u548c\u5185\u5bb9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ft = logging.Formatter.__init__(fmt=None,datafmt=None)<\/code><\/pre>\n\n\n\n<p><strong>logging\u7f16\u7a0b\u65b9\u5f0f\u4ee3\u7801demo<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding:utf-8 -*-\n\nimport logging\n\n#\u7f16\u7a0b\u7684\u65b9\u5f0f\u8bb0\u5f55\u65e5\u5fd7\n\n#\u8bb0\u5f55\u5668\nlogger1 = logging.getLogger(\"logger1\")\nlogger1.setLevel(logging.DEBUG)\nprint(logger1)\nprint(type(logger1))\n\nlogger2 = logging.getLogger(\"logger2\")\nlogger2.setLevel(logging.INFO)\nprint(logger2)\nprint(type(logger2))\n\n\n#\u5904\u7406\u5668\n#1.\u6807\u51c6\u8f93\u51fa\nsh1 = logging.StreamHandler()\n\nsh1.setLevel(logging.WARNING)\n\nsh2 = logging.StreamHandler()\n\n\n# 2.\u6587\u4ef6\u8f93\u51fa\n# \u6ca1\u6709\u8bbe\u7f6e\u8f93\u51fa\u7ea7\u522b\uff0c\u5c06\u7528logger1\u7684\u8f93\u51fa\u7ea7\u522b(\u5e76\u4e14\u8f93\u51fa\u7ea7\u522b\u5728\u8bbe\u7f6e\u7684\u65f6\u5019\u7ea7\u522b\u4e0d\u80fd\u6bd4Logger\u7684\u4f4e!!!)\uff0c\u8bbe\u7f6e\u4e86\u5c31\u4f7f\u7528\u81ea\u5df1\u7684\u8f93\u51fa\u7ea7\u522b\nfh1 = logging.FileHandler(filename=\"fh.log\",mode='w')\n\nfh2 = logging.FileHandler(filename=\"fh.log\",mode='a')\nfh2.setLevel(logging.WARNING)\n\n# \u683c\u5f0f\u5668\nfmt1 = logging.Formatter(fmt=\"%(asctime)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s\")\n\nfmt2 = logging.Formatter(fmt=\"%(asctime)s - %(name)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s\"\n                        ,datefmt=\"%Y\/%m\/%d %H:%M:%S\")\n\n#\u7ed9\u5904\u7406\u5668\u8bbe\u7f6e\u683c\u5f0f\nsh1.setFormatter(fmt1)\nfh1.setFormatter(fmt2)\nsh2.setFormatter(fmt2)\nfh2.setFormatter(fmt1)\n\n#\u8bb0\u5f55\u5668\u8bbe\u7f6e\u5904\u7406\u5668\nlogger1.addHandler(sh1)\nlogger1.addHandler(fh1)\nlogger2.addHandler(sh2)\nlogger2.addHandler(fh2)\n\n#\u6253\u5370\u65e5\u5fd7\u4ee3\u7801\nlogger1.debug(\"This is  DEBUG of logger1 !!\")\nlogger1.info(\"This is  INFO of logger1 !!\")\nlogger1.warning(\"This is  WARNING of logger1 !!\")\nlogger1.error(\"This is  ERROR of logger1 !!\")\nlogger1.critical(\"This is  CRITICAL of logger1 !!\")\n\nlogger2.debug(\"This is  DEBUG of logger2 !!\")\nlogger2.info(\"This is  INFO of logger2 !!\")\nlogger2.warning(\"This is  WARNING of logger2 !!\")\nlogger2.error(\"This is  ERROR of logger2 !!\")\nlogger2.critical(\"This is  CRITICAL of logger2 !!\")<\/code><\/pre>\n\n\n\n<p><strong>\u63a7\u5236\u53f0\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2021-03-26 02:38:56,370 - WARNING   - advancelogging : 56 line - This is  WARNING of logger1 !!\n2021-03-26 02:38:56,370 - ERROR     - advancelogging : 57 line - This is  ERROR of logger1 !!\n2021-03-26 02:38:56,370 - CRITICAL  - advancelogging : 58 line - This is  CRITICAL of logger1 !!\n2021\/03\/26 02:38:56 - logger2 - INFO      - advancelogging : 61 line - This is  INFO of logger2 !!\n2021\/03\/26 02:38:56 - logger2 - WARNING   - advancelogging : 62 line - This is  WARNING of logger2 !!\n2021\/03\/26 02:38:56 - logger2 - ERROR     - advancelogging : 63 line - This is  ERROR of logger2 !!\n2021\/03\/26 02:38:56 - logger2 - CRITICAL  - advancelogging : 64 line - This is  CRITICAL of logger2 !!\n&lt;Logger logger1 (DEBUG)&gt;\n&lt;class 'logging.Logger'&gt;\n&lt;Logger logger2 (INFO)&gt;\n&lt;class 'logging.Logger'&gt;<\/code><\/pre>\n\n\n\n<p><strong>\u6587\u4ef6fh.log\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2021\/03\/26 02:38:56 - logger1 - DEBUG     - advancelogging : 54 line - This is  DEBUG of logger1 !!\n2021\/03\/26 02:38:56 - logger1 - INFO      - advancelogging : 55 line - This is  INFO of logger1 !!\n2021\/03\/26 02:38:56 - logger1 - WARNING   - advancelogging : 56 line - This is  WARNING of logger1 !!\n2021\/03\/26 02:38:56 - logger1 - ERROR     - advancelogging : 57 line - This is  ERROR of logger1 !!\n2021\/03\/26 02:38:56 - logger1 - CRITICAL  - advancelogging : 58 line - This is  CRITICAL of logger1 !!\n2021-03-26 02:38:56,370 - WARNING   - advancelogging : 62 line - This is  WARNING of logger2 !!\n2021-03-26 02:38:56,370 - ERROR     - advancelogging : 63 line - This is  ERROR of logger2 !!\n2021-03-26 02:38:56,370 - CRITICAL  - advancelogging : 64 line - This is  CRITICAL of logger2 !!<\/code><\/pre>\n\n\n\n<p><strong>\u8fc7\u6ee4\u5668\u5728\u4ee3\u7801\u91cc\u7684\u5e94\u7528<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5b9a\u4e49\u4e00\u4e2a\u8fc7\u6ee4\u5668\nflt1 = logging.Filter(\"logger1\") #\u540d\u5b57\u5c31\u662flogger\u8bb0\u5f55\u5668\u7684\u540d\u5b57\n\n#\u5173\u8054\u8fc7\u6ee4\u5668\n#1.logger\u5173\u8054\u8fc7\u6ee4\u5668\nlogger.addFilter(flt1)\n\n#2.\u5904\u7406\u5668\u5173\u8054\u8fc7\u6ee4\u5668\nsh1.addFilter(flt1)\n#fh1.addFilter(flt1)\n\n#\u8fc7\u6ee4\u5668\u5b9a\u4e49\u5728\u8bb0\u5f55\u5668\u4e0a\u7684\u65f6\u5019\uff0c\u4e0d\u7ba1\u4f60\u8bbe\u7f6e\u4e86\u4efb\u4f55\u5173\u4e8e\u8fd9\u4e2a\u8fc7\u6ee4\u5668\u7684\u4efb\u4f55\u8bbe\u7f6e\uff0c\u90fd\u4e0d\u8d77\u4f5c\u7528\u3002\n#\u8fc7\u6ee4\u5668\u5b9a\u4e49\u5728\u54ea\u4e2a\u5904\u7406\u5668\u4e0a\uff0c\u54ea\u4e2a\u5904\u7406\u5668\u5c31\u4e0d\u8d77\u4f5c\u7528\uff0c\u4e5f\u5c31\u53ef\u4ee5\u63a7\u5236\u65e5\u5fd7\u7684\u8f93\u51fa\uff0c\u60f3\u8981\u8fd8\u662f\u4e0d\u60f3\u8981\u3002\n#\u7279\u522b\u662f\u5728\u9879\u76ee\u8c03\u8bd5\u7684\u65f6\u5019\uff0c\u4e0d\u60f3\u8ba9\u65e5\u5fd7\u8bb0\u5f55\u5230\u6587\u4ef6\u91cc\u53bb\uff0c\u76f4\u63a5\u5728\u5c4f\u5e55\u663e\u793a\uff0c\u5c31\u8fc7\u6ee4\u5230fh\n#\u4ee3\u7801\u4e0a\u7ebf\u540e\uff0c\u65e5\u5fd7\u8bb0\u5f55\u5230\u6587\u4ef6\uff0c\u4e0d\u7528\u5728\u5c4f\u5e55\u663e\u793a\uff0c\u5c31\u8fc7\u6ee4\u6389sh<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u6587\u4ef6\u914d\u7f6elogging\u7f16\u7a0b<\/strong><\/p>\n\n\n\n<p><strong>\u4e0a\u8ff0\u7684\u65b9\u6cd5\u90fd\u5199\u7684\u5f88\u6b7b\u677f\uff0c\u5728\u5c0f\u5de5\u7a0b\u6216\u8005\u5355\u4e2a\u6587\u4ef6\u8fd8\u53ef\u4ee5\uff0c\u4f46\u662f\u5f53\u9879\u76ee\u5927\u4e86\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\u914d\u7f6elogging\u7684\u4fe1\u606f\uff0c\u4ece\u800c\u6765\u7075\u6d3b\u4f7f\u7528\uff0c\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u5f0f\u53bb\u914d\u7f6e\uff0c\u4e00\u79cd\u662fconf\u6587\u4ef6\uff0c\u4e00\u79cd\u662f\u5b57\u5178\u6587\u4ef6\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import logging.config\nlogging.config.fileConfig(\"\/path\/to\/configfile\")\n\nlogging.config.dictConfig(\"\/path\/to\/configfile\")<\/code><\/pre>\n\n\n\n<p><strong>yaml\u914d\u7f6e\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>version: 1\n#\u662f\u5426\u8986\u76d6\u6389\u5df2\u7ecf\u5b58\u5728\u7684loggers\ndisable_existing_loggers: True\n\nformatters:\n\n  tostrout:\n    format: \"%(asctime)s - %(name)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s\"\n    datefmt: \"%Y\/%m\/%d %H:%M:%S\"\n\n  tofile:\n    format: \"%(asctime)s - %(name)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s\"\n\nhandlers:\n  sh:\n    class: logging.StreamHandler\n    level: WARNING\n    formatter: tostrout\n    stream: ext:<em>\/\/sys.stdout<\/em>\n\n  fh:\n    class: logging.handlers.TimedRotatingFileHandler\n    filename: logconfdd.log\n    interval: 1\n    backupCount: 2\n    when: D\n    level: INFO\n    formatter: tofile\n\nloggers:\n  logger1:\n    level: DEBUG\n    handlers: &#91;sh]\n    #\u662f\u5426\u5f80\u4e0a\u7ea7Logger\u4f20\u9012\uff0c\u5982\u679c\u4e3ayes\u7684\u8bdd\uff0croot\u9009\u62e9\u4e86\u4e24\u4e2alogger\uff0c\u8fd9\u91cc\u7684\u65e5\u5fd7\u4e5f\u4f1a\u5728\u4e24\u4e2alogger\u7684\u914d\u7f6e\u4e2d\u8f93\u51fa\uff0c\u4f1a\u91cd\u590d\u3002\u6240\u4ee5\u9009No,\u81ea\u5df1\u8bb0\u5f55\u81ea\u5df1\u7684\u65e5\u5fd7\u3002\n    propagate: no\n\n  logger2:\n    level: INFO\n    handlers: &#91;fh]\n    propagate: no\n\nroot:\n  level: DEBUG\n  handlers: &#91;sh,fh]\n  propagate: no<\/code><\/pre>\n\n\n\n<p><strong>\u8c03\u7528\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding:utf-8 -*-\n\nimport logging\nimport logging.config\nimport yaml\n\nwith open(\"logconf.yml\", \"r\") as f:\n    dict_conf = yaml.safe_load(f)\n\nlogging.config.dictConfig(dict_conf)\nroot = logging.getLogger()\nlogger1 = logging.getLogger('logger1')\nlogger2 = logging.getLogger('logger2')\n\n#\u6253\u5370\u65e5\u5fd7\u4ee3\u7801\n\nroot.debug(\"This is  DEBUG of root !!\")\nroot.info(\"This is  INFO of root !!\")\nroot.warning(\"This is  WARNING of root !!\")\nroot.error(\"This is  ERROR of root !!\")\nroot.critical(\"This is  CRITICAL of root !!\")\n\nlogger1.debug(\"This is  DEBUG of logger1 !!\")\nlogger1.info(\"This is  INFO of logger1 !!\")\nlogger1.warning(\"This is  WARNING of logger1 !!\")\nlogger1.error(\"This is  ERROR of logger1 !!\")\nlogger1.critical(\"This is  CRITICAL of logger1 !!\")\n\nlogger2.debug(\"This is  DEBUG of logger2 !!\")\nlogger2.info(\"This is  INFO of logger2 !!\")\nlogger2.warning(\"This is  WARNING of logger2 !!\")\nlogger2.error(\"This is  ERROR of logger2 !!\")\nlogger2.critical(\"This is  CRITICAL of logger2 !!\")<\/code><\/pre>\n\n\n\n<p><strong>\u6807\u51c6\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>021\/03\/27 12:18:58 - root - WARNING   - loggingofconfing : 19 line - This is  WARNING of root !!\n2021\/03\/27 12:18:58 - root - ERROR     - loggingofconfing : 20 line - This is  ERROR of root !!\n2021\/03\/27 12:18:58 - root - CRITICAL  - loggingofconfing : 21 line - This is  CRITICAL of root !!\n2021\/03\/27 12:18:58 - logger1 - WARNING   - loggingofconfing : 25 line - This is  WARNING of logger1 !!\n2021\/03\/27 12:18:58 - logger1 - ERROR     - loggingofconfing : 26 line - This is  ERROR of logger1 !!\n2021\/03\/27 12:18:58 - logger1 - CRITICAL  - loggingofconfing : 27 line - This is  CRITICAL of logger1 !!<\/code><\/pre>\n\n\n\n<p><strong>\u6587\u4ef6\u8f93\u51fa(logconfdd.log):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2021-03-27 12:18:58,301 - root - INFO      - loggingofconfing : 18 line - This is  INFO of root !!\n2021-03-27 12:18:58,301 - root - WARNING   - loggingofconfing : 19 line - This is  WARNING of root !!\n2021-03-27 12:18:58,301 - root - ERROR     - loggingofconfing : 20 line - This is  ERROR of root !!\n2021-03-27 12:18:58,301 - root - CRITICAL  - loggingofconfing : 21 line - This is  CRITICAL of root !!\n2021-03-27 12:18:58,301 - logger2 - INFO      - loggingofconfing : 30 line - This is  INFO of logger2 !!\n2021-03-27 12:18:58,301 - logger2 - WARNING   - loggingofconfing : 31 line - This is  WARNING of logger2 !!\n2021-03-27 12:18:58,301 - logger2 - ERROR     - loggingofconfing : 32 line - This is  ERROR of logger2 !!\n2021-03-27 12:18:58,301 - logger2 - CRITICAL  - loggingofconfing : 33 line - This is  CRITICAL of logger2 !!<\/code><\/pre>\n\n\n\n<p>\u672c\u6587\u8f6c\u81ea\u77e5\u547c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f55\u65f6\u4f7f\u7528logging \u60f3\u8981\u6267\u884c\u7684\u4efb\u52a1 \u5de5\u5177 \u5bf9\u4e8e\u547d\u4ee4\u884c\u6216\u7a0b\u5e8f\u7684\u5e94\u7528\uff0c\u7ed3\u679c\u663e\u793a\u5728\u63a7\u5236\u53f0\u3002 print() \u5728\u5bf9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1729","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1729"}],"version-history":[{"count":1,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1729\/revisions"}],"predecessor-version":[{"id":1762,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1729\/revisions\/1762"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1729"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}