{"id":160,"date":"2018-08-31T13:33:50","date_gmt":"2018-08-31T05:33:50","guid":{"rendered":"http:\/\/www.yinyubo.cn\/?p=160"},"modified":"2022-05-17T09:50:46","modified_gmt":"2022-05-17T01:50:46","slug":"python-logging","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=160","title":{"rendered":"python-logging\u5168\u5c40\u65e5\u5fd7\u914d\u7f6e-\u6eda\u52a8\u5220\u9664\uff0c\u53ea\u4fdd\u5b58\u6700\u8fd17\u5929\u7684\u65e5\u5fd7\uff0c\u6309\u7ea7\u522b\u5b58\u5165\u4e0d\u540c\u6587\u4ef6"},"content":{"rendered":"\n<p>\u6700\u8fd1\u6709\u8fd9\u6837\u4e00\u4e2a\u9700\u6c42\uff0c\u9700\u8981\u8bb0\u5f55\u4e00\u4e0b\u7528\u6237\u884c\u4e3a\uff0c\u548c\u8bb0\u4e0b\u4e00\u4e9b\u9519\u8bef\u65e5\u5fd7\uff0c\u653e\u5165\u6307\u5b9a\u6587\u4ef6\u5939\u91cc\u4e0d\u540c\u7684\u6587\u4ef6\u4e0b\uff0c\u65b9\u4fbf\u540e\u7eeddebug\u3002\u6211\u51b3\u5b9a\u91c7\u7528python logging\u6a21\u5757\u3002\u5e76\u4e14\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u505a\u4e00\u4e2a\u5168\u5c40\u7684globalLog.py\u6765\u4f7f\u7528logging\u3002 \uff08\u5173\u952e\u8bcd\uff1alogging\uff0cTimedRotatingFileHandler\uff09 <\/p>\n\n\n\n<p><span style=\"color: #3366ff;\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u7b2c\u4e00\u6b65\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/span><\/p>\n\n\n\n<p> \u6dfb\u52a0\u914d\u7f6e\u6587\u4ef6logger_config.ini\u3002\u6587\u4ef6\u7684\u540e\u7f00\u540d\u5176\u5b9e\u65e0\u6240\u8c13\uff0cconf,ini\u6216\u8005\u522b\u7684\u90fd\u884c\u3002\u6211\u628a\u8be5\u6587\u4ef6\u653e\u7f6e\u5728\/{$\u9879\u76ee\u540d}\/resource\/\u8def\u5f84\u4e0b\u3002 <\/p>\n\n\n\n<p>\u91cd\u70b91\uff1aTimedRotatingFileHandler\u7528\u6765\u914d\u7f6elog\u7684\u6eda\u52a8\u5220\u9664\uff0carg\u91cc\u7684<span style=\"color: #ff0000;\">&#8220;midnight&#8221;, 1, 6,&#8217;utf-8&#8242; <span style=\"color: #000000;\">\u8868\u793a\u201c\u6bcf\u4e00\u5929\u5348\u591c12\u70b9\u5c06\u5f53\u5929\u7684\u65e5\u5fd7\u8f6c\u5b58\u5230\u4e00\u4efd\u65b0\u7684\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u5e76\u4e14\u52a0\u4e0a\u65f6\u95f4\u6233\u540e\u7f00\uff0c\u6700\u591a\u4fdd\u5b586\u4e2a\u6587\u4ef6\uff0c\u7f16\u7801\u683c\u5f0fUTF-8\uff0c\u652f\u6301\u4e2d\u6587\u201d<\/span><\/span> <\/p>\n\n\n\n<p>\u91cd\u70b92\uff1a\u5982\u679c\u8981\u540c\u65f6\u8f93\u51fa\u52302\u4e2a\u6587\u4ef6\u548c\u5c4f\u5e55\u4e0a\uff0c\u90a3\u4e48\u9700\u8981\u4f7f\u7528\u4e09\u4e2ahandler\uff0c2\u4e2a\u4e3aTimedRotatingFileHandler\uff0c\u8fd8\u67091\u4e2a\u662fStreamHandler <\/p>\n\n\n\n<p>\u91cd\u70b93\uff1alevel\u7ea7\u522b\u5982\u4e0b\uff0c\u7ea7\u522b\u8d8a\u4f4e\uff0c\u6253\u5370\u7684\u65e5\u5fd7\u8d8a\u8be6\u7ec6\uff0c\u4f8b\u5982\u7ea7\u522b\u4e3anoset\uff0c\u5219\u4f1a\u6253\u5370\u51fa\u6240\u6709\u4fe1\u606f\uff0c\u5982\u679c\u7ea7\u522b\u4e3ainfo,\u90a3\u4e48\u4e0d\u4f1a\u6253\u5370\u51fadebug\u4fe1\u606f\u3002\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u7ea7\u522b\u4e3ainfo\uff0c\u65e5\u5fd7\u91cc\u4e0d\u4f1a\u6253\u5370\u51fadebug\u7684\u4fe1\u606f <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u914d\u7f6e\u6587\u4ef6\uff1a <pre class=\"lang:python decode:true\">&#91;loggers] keys=root <\/pre>\n<pre class=\"lang:python decode:true\">&#91;handlers] keys=rotatingFileHandler,streamHandler,errorHandler <\/pre>\n<pre class=\"lang:python decode:true\">&#91;formatters] keys=simpleFmt <\/pre>\n<pre class=\"lang:python decode:true\">&#91;logger_root] level=DEBUG <\/pre>\n<pre class=\"lang:python decode:true\">handlers=rotatingFileHandler,streamHandler,errorHandler <\/pre>\n<pre class=\"lang:python decode:true\">&#91;handler_rotatingFileHandler] <\/pre>\n<pre class=\"lang:python decode:true\">class=handlers.TimedRotatingFileHandler level=INFO formatter=simpleFmt args=(os.path.abspath(os.getcwd() + \"\/resource\/ta_log\/default.log\"),\"midnight\", 1, 6,'utf-8') <\/pre>\n<pre class=\"lang:python decode:true\">&#91;handler_errorHandler] class=handlers.TimedRotatingFileHandler level=ERROR formatter=simpleFmt args=(os.path.abspath(os.getcwd() + \"\/resource\/ta_log\/error.log\"), \"midnight\", 1, 6,'utf-8') <\/pre>\n<pre class=\"lang:python decode:true\">&#91;handler_streamHandler] level=INFO class=StreamHandler formatter=simpleFmt args=(sys.stdout,) <\/pre>\n<pre class=\"lang:python decode:true\">&#91;formatter_simpleFmt] format=%(asctime)s %(pathname)s(%(lineno)d): %(levelname)s %(message)s<\/pre> <\/code><\/pre>\n\n\n<p><span style=\"color: #3366ff;\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u7b2c\u4e8c\u6b65\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/span><\/p>\n<p>\u7f16\u5199globalLog.py \u4f5c\u4e3a\u5168\u5c40log\u7684\u7ba1\u7406\u5165\u53e3\u3002<\/p>\n<p>\u540e\u7eed\u522b\u7684\u6a21\u5757\u4e0b\u7684python\u6587\u4ef6\u9700\u8981\u4f7f\u7528\u65e5\u5fd7\u529f\u80fd\u65f6\uff0c\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u8be5\u6a21\u5757\u7684\u539f\u7406\u4e5f\u5f88\u7b80\u5355\uff0c\u5b9a\u4f4d\u5230\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u901a\u8fc7logger\u7684\u540d\u5b57\u83b7\u5f97\u8981\u7528\u7684logger\u914d\u7f6e\uff0c\u4f8b\u5982\u6211\u4e0a\u9762\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d [loggers] keys=root \u6240\u4ee5\u8fd9\u91cc\u5c31\u7528\u4e86root.\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u7684logger\u5bf9\u8c61\uff0c\u547d\u540d\u4e3ata_log<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">#globalLog.py\nimport logging\nimport logging.config\nimport os\ndef get_logger(name='root'):\n    conf_log = os.path.abspath(os.getcwd() + \"\/resource\/logger_config.ini\")\n    logging.config.fileConfig(conf_log)\n    return logging.getLogger(name)\nta_log = get_logger(__name__)<\/code><\/pre>\n<p><span style=\"color: #3366ff;\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u7b2c\u4e09\u6b65\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/span><\/p>\n<p>\u5728\u9700\u8981\u6253\u5370\u65e5\u5fd7\u7684\u5730\u65b9\u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u4f8b\u5982\u5728main.py\u91cc\u901a\u8fc7ta_log.info()\u6253\u5370\u65e5\u5fd7\u3002\u8be5\u65b9\u6cd5\u4f1a\u6253\u5370\u5404\u4e2a\u7ea7\u522b\u7684\u4fe1\u606f\u8fdb\u5165\u4e24\u4e2a\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u5728\u7b2c\u56db\u6b65\u6765\u770b\u4e00\u770b\u65e5\u5fd7\u6587\u4ef6\u7684\u5185\u5bb9<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">import sys\nfrom globalLog import ta_log\nif __name__ == '__main__':\n    ta_log.info(\"start\")\n    try:\n        print(1 \/ 0)\n    except Exception:\n        ta_log.error(\"error:\")\n        ta_log.exception(sys.exc_info())\n    ta_log.debug(\"end\")<\/code><\/pre>\n<p><span style=\"color: #3366ff;\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u7b2c\u56db\u6b65\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/span><\/p>\n<p>\u8fd0\u884c\u7a0b\u5e8f\uff0c\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6\u3002 \u8fd9\u662fdefault.log\uff0c\u7ea7\u522b\u662finfo.\u6ca1\u6709\u8bb0\u5f55ta_log.debug()\u7684\u4fe1\u606f<\/p>\n<pre class=\"lang:python decode:true \" title=\"default.log\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">2018-08-31 13:09:34,928 C:\/lzw_programming\/test\/main.py(6): INFO start\n2018-08-31 13:09:34,928 C:\/lzw_programming\/test\/main.py(10): ERROR error:\n2018-08-31 13:09:34,930 C:\/lzw_programming\/test\/main.py(11): ERROR (&lt;type 'exceptions.ZeroDivisionError'&gt;, ZeroDivisionError('integer division or modulo by zero',), &lt;traceback object at 0x0000000003B38888&gt;)\nTraceback (most recent call last):\n  File \"C:\/lzw_programming\/test\/main.py\", line 8, in &lt;module&gt;\n    print(1 \/ 0)\nZeroDivisionError: integer division or modulo by zero<\/code><\/pre>\n<p>\u8fd9\u662ferror.log\u3002\u7ea7\u522b\u662ferror\u3002\u6ca1\u6709\u8bb0\u5f55ta_log.info()\u548cta_log.debug()\u7684\u4fe1\u606f<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">2018-08-31 13:09:34,928 C:\/lzw_programming\/testassist\/main.py(10): ERROR error:\n2018-08-31 13:09:34,930 C:\/lzw_programming\/testassist\/main.py(11): ERROR (&lt;type 'exceptions.ZeroDivisionError'&gt;, ZeroDivisionError('integer division or modulo by zero',), &lt;traceback object at 0x0000000003B38888&gt;)\nTraceback (most recent call last):\n  File \"C:\/lzw_programming\/testassist\/main.py\", line 8, in &lt;module&gt;\n    print(1 \/ 0)\nZeroDivisionError: integer division or modulo by zero<\/code><\/pre>\n<p><span style=\"color: #3366ff;\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u7b2c\u4e94\u6b65\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/span><\/p>\n<p>\u8fd0\u884c\u4e863\u5929\u4e3b\u7a0b\u5e8f\uff0c\u67e5\u770b\u662f\u5426\u6eda\u52a8\u4ea7\u751f\u4e86\u65b0\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u7ed3\u679c\u662f\u628a\u5386\u53f2\u65e5\u5fd7\u4fe1\u606f\u6eda\u52a8\u590d\u5236\u5230\u4e86\u65b0\u7684\u6587\u4ef6\u91cc\uff0c\u540e\u7f00\u540d\u4e3a\u65e5\u671f\uff0c\u5982\u56fe\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-170 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/\u65e5\u5fd7\u5386\u53f2\u56fe.png\" alt=\"\" width=\"583\" height=\"247\"><\/p>","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6709\u8fd9\u6837\u4e00\u4e2a\u9700\u6c42\uff0c\u9700\u8981\u8bb0\u5f55\u4e00\u4e0b\u7528\u6237\u884c\u4e3a\uff0c\u548c\u8bb0\u4e0b\u4e00\u4e9b\u9519\u8bef\u65e5\u5fd7\uff0c\u653e\u5165\u6307\u5b9a\u6587\u4ef6\u5939\u91cc\u4e0d\u540c\u7684\u6587\u4ef6\u4e0b\uff0c\u65b9\u4fbf\u540e\u7eeddebug [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-k8s"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":955,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions\/955"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}