{"id":3526,"date":"2019-05-13T18:16:00","date_gmt":"2019-05-13T10:16:00","guid":{"rendered":"https:\/\/www.yewen.us\/blog\/?p=3526"},"modified":"2019-05-13T18:16:00","modified_gmt":"2019-05-13T10:16:00","slug":"flask-celery-logging-problems","status":"publish","type":"post","link":"https:\/\/www.yewen.us\/blog\/2019\/05\/flask-celery-logging-problems\/","title":{"rendered":"flask celery logging \u7ffb\u8f66\u8bb0"},"content":{"rendered":"<p>\u867d\u7136\u524d\u9762\u81ea\u5df1\u5199\u8fc7\u4e00\u4e2a Python Logging \u7684\u5404\u79cd\u73a9\u6cd5\uff08<a href=\"https:\/\/www.yewen.us\/blog\/2019\/03\/playing-with-python-logging\/\" rel=\"noopener noreferrer\" target=\"_blank\">\u6298\u817e Python logging \u7684\u4e00\u4e9b\u8bb0\u5f55<\/a>\uff09\uff0c\u7ed3\u679c\u6ca1\u4e24\u4e2a\u6708\uff0c\u81ea\u5df1\u5c31\u5728\u4e4b\u524d\u7279\u610f\u53ee\u5631\u8fc7\u7684\u5730\u65b9\u7ffb\u4e86\u8f66<\/p>\n<p>\u4e8b\u60c5\u662f\u8fd9\u6837\u7684\uff0c\u6211\u4eec\u7684\u9879\u76ee\u4f7f\u7528 Flask \u4f5c\u4e3a\u4e1a\u52a1\u6846\u67b6\uff0c\u7528 Celery \u4f5c\u4e3a\u5f02\u6b65\u4efb\u52a1\u6846\u67b6\uff0c\u6309\u4e0a\u7bc7\u91cc\u63d0\u5230\u7684\uff0c\u6211\u4eec\u52a0\u4e86\u4e2a Filter \u6765\u52a0\u5165 <code>request_id<\/code> \u7528\u4e8e\u8ddf\u8e2a\u540c\u4e00\u4e2a Web \u8bf7\u6c42\u6216\u540c\u4e00\u4e2a Task\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u7684 <code>logging.conf<\/code> \u4e00\u5f00\u59cb\u662f\u8fd9\u6837\u7684\uff0c\u5728 <code>logfile<\/code> \u8fd9\u4e2a Handler \u91cc\u52a0\u5165 <code>request_id<\/code> \u5e76\u6253\u5370\u5230\u65e5\u5fd7\u6587\u4ef6\uff0c\u6211\u4eec\u7528\u963f\u91cc\u4e91\u7684 logtail \u628a\u6240\u6709\u90e8\u7f72\u5b9e\u4f8b\u7684\u65e5\u5fd7\u6587\u4ef6\u90fd\u6536\u96c6\u5230\u4e00\u8d77\uff0c\u8fd9\u4e00\u5c42 <code>app<\/code> Logger \u5f80\u4e0a\u629b\u662f\u8ba9\u7ec8\u7aef\u90fd\u6253\u5370\u6240\u6709\u7684\u65e5\u5fd7\uff0c\u5e76\u4e14\u7528 <code>sentry<\/code> \u8fd9\u4e2a Handler \u5728\u6700\u4e0a\u9762\u6536\u96c6\u5404\u79cd\u5f02\u5e38\u62a5\u9519<\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\nlogging_conf = {\r\n    &quot;version&quot;: 1,\r\n    &quot;disable_existing_loggers&quot;: True,\r\n    &quot;filters&quot;: {\r\n        &quot;addRequestId&quot;: {\r\n            &quot;()&quot;: &quot;app.utils.logging.request_filter.ContextFilter&quot;\r\n        }\r\n    },\r\n    &quot;formatters&quot;: {\r\n        &quot;standard&quot;: {\r\n            &quot;format&quot;: &quot;&#x5B;%(asctime)s]&#x5B;%(levelname)s]&#x5B;%(pathname)s:%(lineno)s]&#x5B;%(funcName)s]: %(message)s&quot;\r\n        },\r\n        &quot;withRequestId&quot;: {\r\n            &quot;format&quot;: &quot;&#x5B;%(asctime)s]&#x5B;%(levelname)s]&#x5B;%(relpath)s:%(lineno)s]&#x5B;%(funcName)s]&#x5B;%(request_id)s]: %(message)s&quot;\r\n        }\r\n    },\r\n    &quot;handlers&quot;: {\r\n        &quot;console&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;class&quot;: &quot;logging.StreamHandler&quot;,\r\n            &quot;formatter&quot;: &quot;standard&quot;\r\n        },\r\n        &quot;logfile&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;class&quot;: &quot;logging.handlers.WatchedFileHandler&quot;,\r\n            &quot;filters&quot;: &#x5B;&quot;addRequestId&quot;],\r\n            &quot;formatter&quot;: &quot;withRequestId&quot;,\r\n            &quot;filename&quot;: &quot;log\/app.log&quot;\r\n        },\r\n        &quot;sentry&quot;: {\r\n            &quot;level&quot;: &quot;ERROR&quot;,\r\n            &quot;class&quot;: &quot;raven.handlers.logging.SentryHandler&quot;,\r\n            &quot;dsn&quot;: &quot;DSN_URI&quot;,\r\n            &quot;string_max_length&quot;: 512000\r\n        }\r\n    },\r\n    &quot;loggers&quot;: {\r\n        &quot;app&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;handlers&quot;: &#x5B;&quot;logfile&quot;],\r\n            &quot;propagate&quot;: True\r\n        }\r\n    },\r\n    &quot;root&quot;: {\r\n        &quot;level&quot;: &quot;DEBUG&quot;,\r\n        &quot;handlers&quot;: &#x5B;&quot;console&quot;, &quot;sentry&quot;]\r\n    }\r\n}\r\n<\/pre>\n<p>\u540e\u6765\uff0c\u4e3a\u4e86\u628a Celery \u8c03\u5ea6\u4fe1\u606f\u7b49\u4e5f\u901a\u8fc7 logtail \u6536\u96c6\u5230\u963f\u91cc\u4e91\uff0c\u6211\u4eec\u5c31\u628a logtail \u7684\u6e90\u4ece\u65e5\u5fd7\u6587\u4ef6\u6539\u6210\u4e86 stdout\/stderr \u8f93\u51fa\uff0c\u4e0d\u540c\u65f6\u6536\u96c6\u65e5\u5fd7\u6587\u4ef6\u662f\u56e0\u4e3a\u540c\u6837\u7684\u65e5\u5fd7\u5728 <code>logfile<\/code> \u548c <code>console<\/code> \u91cc\u4f1a\u51fa\u73b0\u4e24\u6b21\uff0c\u6ca1\u6709\u5fc5\u8981\u3002\u4f46\u662f\u53ea\u6539 stdout\/stderr \u53c8\u4f1a\u5bfc\u81f4\u6536\u96c6\u5230\u7684\u4fe1\u606f\u6ca1\u6709\u6211\u4eec\u8f9b\u82e6\u52a0\u8fdb\u53bb\u7684 <code>relpath<\/code> \u548c <code>request_id<\/code>\uff0c\u4e00\u4e2a\u5f88\u76f4\u63a5\u7684\u601d\u8def\u5c31\u662f\uff0c\u5728 <code>app<\/code> \u8fd9\u5c42 Logger \u4e0a\u52a0\u4e00\u4e2a <code>consoleWithId<\/code> \u7684 <code>StreamHandler<\/code>\uff0c\u5e76\u4e14 formatter \u7528 <code>withRequestId<\/code> \u4e0d\u5c31\u597d\u4e86\uff0c\u7136\u540e\u9650\u5236 <code>app<\/code> Logger \u7684 propagate \u4e3a False \u7981\u6b62\u4e0a\u629b\uff0c\u95ee\u9898\u5e94\u8be5\u5b8c\u7f8e\u89e3\u51b3\uff1f\u7b49\u4f1a\uff0c\u8fd9\u91cc\u6709\u597d\u51e0\u4e2a\u95ee\u9898<\/p>\n<p>\u7b2c\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u5982\u679c <code>app<\/code> Logger \u4e0d\u5f80\u4e0a\u629b\uff0c\u90a3\u4e07\u4e00\u5f02\u5e38\u4e86\uff0c<code>sentry<\/code> Handler \u4e5f\u6536\u96c6\u4e0d\u5230\u9519\u8bef\uff1f\u5934\u75bc\u533b\u5934\u811a\u75bc\u533b\u811a\uff0c\u90a3\u5c31\u7ed9 <code>app<\/code> Logger \u4e5f\u6302\u4e0a <code>sentry<\/code> Handler \u4e0d\u5c31\u89e3\u51b3\u95ee\u9898<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u95ee\u9898\u662f\uff0chandlers \u7684\u5904\u7406\u987a\u5e8f\u662f\u4e0d\u662f\u4e25\u683c\u6309\u6211\u4eec\u914d\u7f6e\u7684\u987a\u5e8f\u6765\uff1f\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c<code>consoleWithId<\/code> \u8fdb\u5165\u7684\u65f6\u5019\uff0c\u53ef\u80fd <code>addRequestId<\/code> \u8fd9\u4e2a Filter \u8fd8\u6ca1\u6267\u884c\uff0c\u51fa\u73b0\u4e86\u8f93\u51fa\u65f6\u62ff\u4e0d\u5230 <code>relpath<\/code> \u548c <code>request_id<\/code> \u90a3\u4e0d\u5c31\u6302\u4e86\uff1f\u8fd9\u4e2a\u7b80\u5355\uff0c\u628a Filter \u79fb\u5230 Logger \u8fd9\u4e00\u5c42\u4e0d\u5c31\u89e3\u51b3\u4e86\u3002\u81f3\u6b64\uff0c\u914d\u7f6e\u5982\u4e0b\uff08\u53ea\u6458\u5f55\u6539\u52a8\u90e8\u5206\uff09<\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\n    # ...\r\n    &quot;handlers&quot;: {\r\n        # ...\r\n        &quot;logfile&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;class&quot;: &quot;logging.handlers.WatchedFileHandler&quot;,\r\n            &quot;formatter&quot;: &quot;withRequestId&quot;,\r\n            &quot;filename&quot;: &quot;log\/app.log&quot;\r\n        },\r\n        &quot;consoleWithId&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;class&quot;: &quot;logging.StreamHandler&quot;,\r\n            &quot;formatter&quot;: &quot;withRequestId&quot;\r\n        },\r\n        # ...\r\n    },\r\n    &quot;loggers&quot;: {\r\n        &quot;app&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;filters&quot;: &#x5B;&quot;addRequestId&quot;],\r\n            &quot;handlers&quot;: &#x5B;&quot;logfile&quot;, &quot;consoleWithId&quot;, &quot;sentry&quot;],\r\n            &quot;propagate&quot;: False\r\n        }\r\n    },\r\n    # ...\r\n<\/pre>\n<p>\u7ffb\u8f66\u5c31\u7ffb\u5728\u300c\u8fd9\u4e2a\u7b80\u5355\u300d\u4e0a\uff0c\u6309\u8fd9\u4e2a\u601d\u8def\u914d\u7f6e\u540e\uff0c\u8dd1\u8d77\u6765\u8fd8\u662f\u5728 <code>consoleWithId<\/code> \u7684 Handler \u4e0a\u8f93\u51fa\u62a5\u9519\uff0c\u800c\u4e14\u62a5\u7684\u5c31\u662f <code>relpath<\/code> \u548c <code>request_id<\/code> \u5b57\u6bb5\u4e0d\u5b58\u5728\u3002\u6000\u7591\u81ea\u5df1\u7684\u914d\u7f6e\u6709\u95ee\u9898\uff0c\u8dd1\u5230\u4ee3\u7801\u91cc\u6253\u65e5\u5fd7\u7684\u5730\u65b9\u7528 <code>logging.getLogger(__name__)<\/code> \u770b\u62ff\u5230\u7684\u5230\u5e95\u662f\u54ea\u4e2a Logger\uff0c\u4ee5\u53ca\u4e0a\u9762\u6302\u4e86\u54ea\u4e9b Handler\uff0c\u8fd8\u6709 Logger \u548c Handler \u7684 Filters \u90fd\u914d\u7684\u5565\uff0c\u53d1\u73b0\u9664\u4e86 <code>app<\/code> \u6839\u4e0a\uff0c\u5982\u679c\u662f <code>app.foo<\/code> \u8fd9\u6837\u7684\u8def\u5f84\uff0c\u62ff\u5230\u7684\u90fd\u662f\u4e00\u4e2a\u53eb <code>celery.utils.log.ProcessAwareLogger<\/code> \u7684 Logger\uff0c\u800c\u4e14\u6ca1\u6709\u4efb\u4f55 Handler \u548c Filter \u6302\u5728\u4e0a\u9762\uff0c\u6240\u4ee5\uff0cCelery \u4f60\u8fd9\u4e2a\u574f\u4eba\uff0c\u5230\u5e95\u5bf9\u6211\u7684\u4ee3\u7801\u505a\u4e86\u4ec0\u4e48\uff1f<\/p>\n<p>\u8dd1\u53bb\u7ffb <code>celery.utils.log.ProcessAwareLogger<\/code> \u8fd9\u4e2a\u4e1c\u897f\u7684\u6e90\u7801\u90fd\u6ca1\u770b\u51fa\u4e2a\u6240\u4ee5\u7136\uff0c\u4f3c\u4e4e\u53ea\u662f\u4e3a\u4e86\u4fdd\u8bc1 Flask \u7684 signal handler \u673a\u5236\u6b63\u5e38\uff0c\u6392\u67e5\u601d\u8def\u4e5f\u65ad\u6389\uff0c\u518d\u8dd1\u53bb\u770b\u770b\u6211\u4eec\u90a3\u4e2a <code>app.utils.logging.request_filter<\/code> \u7684\u5904\u7406\uff0c\u6709\u6ca1\u6709\u54ea\u91cc\u4e0d\u5bf9\u7684\uff0c\u5728\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u7684 filter \u91cc\u88f8\u7528 print \u6253\u5370\uff0c\u53d1\u73b0\u8fd9\u4e2a filter \u538b\u6839\u6ca1\u88ab\u8c03\u7528\u5230\uff1f\u55ef\uff1f\u6ca1\u88ab\u8c03\u7528\u5230\uff1f<\/p>\n<p>\u56de\u53bb\u770b\u81ea\u5df1\u7684\u4e0a\u4e00\u7bc7\uff0c\u679c\u7136\u91cc\u9762\u81ea\u5df1\u5c31\u63d0\u5230\u8fc7\u8fd9\u91cc\u6709\u5751\uff08\u4e3b\u6d41\u7a0b\u89e3\u91ca\u7684\u7b2c 5 \u6b65\uff09<\/p>\n<blockquote><p>\n  \u5982\u679c\u5f00\u542f\u4e86\u65e5\u5fd7\u5f80\u4e0a\u4f20\u9012\uff0c\u5219\u5224\u65ad\u5f53\u524d Logger \u662f\u5426\u6709\u7236 Logger\uff0c\u5982\u679c\u6709\u7684\u8bdd\uff0c\u76f4\u63a5\u5c06\u5f53\u524d LogRecord \u4f20\u7ed9\u7236 Logger \u4ece 4 \u5f00\u59cb\u5904\u7406\uff08\u8df3\u8fc7 1\/2\/3\uff0c\u6ce8\u610f\u6b64\u5904\u7ea7\u522b\u63a7\u5236 1 \u4f1a\u4e0d\u751f\u6548\uff0c\u7ed1\u5b9a\u5728\u7236 Logger \u4e0a\u7684 Filter \u4e5f\u4e0d\u6267\u884c\uff09\n<\/p><\/blockquote>\n<p>WTF\uff01\u679c\u7136\u5751\u90fd\u662f\u81ea\u5df1\u4e0d\u6389\u4e00\u904d\uff0c\u522b\u4eba\u8bf4\u5343\u4e07\u904d\u4e5f\u4e0d\u4f1a\u8bb0\u5f97\u7684\uff0c\u54ea\u6015\u8bf4\u7684\u8fd9\u4e2a\u4eba\u662f\u81ea\u5df1\u3002\u90a3\u597d\u54af\uff0c\u628a <code>addRequestId<\/code> \u8fd9\u4e2a Filter \u8fd8\u662f\u4ece <code>app<\/code> Logger \u4e0a\u79fb\u5230 Handler \u5c42\u9762\u4e0a\u597d\u4e86\uff0c\u6bcf\u4e2a\u9700\u8981\u7684 Handler \u90fd\u7ed9\u6302\u4e0a\uff0c\u591a\u70b9\u6027\u80fd\u5f00\u9500\u5c31\u591a\u70b9\u5427<\/p>\n<p>\u4e0d\u8fc7\u8fd9\u6837\u914d\u7684\u611f\u89c9\u8fd8\u662f\u602a\u602a\u7684\uff0c\u6bd4\u5982\u6709\u4e9b\u9519\u8bef\u4f1a\u88ab sentry \u6536\u96c6\u4e24\u6b21\uff0c\u56e0\u4e3a\u5728 app \u91cc\u4e00\u76f4\u5f80\u4e0a\u629b\u4f1a\u88ab <code>app<\/code> Logger \u91cc\u7684 sentry \u6536\u96c6\uff0c\u5982\u679c\u8fd9\u4e2a\u9519\u8bef\u8fd8\u7ee7\u7eed\u5f80\u4e0a\u629b\u5230\u4e86\u6846\u67b6\u5c42\u9762\uff0c\u6846\u67b6\u7684\u9519\u8bef\u8fd8\u4f1a\u88ab rootLogger \u7684 sentry \u53c8\u6536\u96c6\u4e00\u6b21\u3002\u800c\u4e14\uff0c\u65e2\u7136 app \u7684\u91cc\u9762\u548c\u5916\u9762\u90fd\u6709\u7ec8\u7aef\u548c sentry\uff0c\u4e3a\u5565\u4e0d\u5728\u6700\u5916\u9762\u4e00\u6b21\u5904\u7406\u597d\uff0c\u4e2d\u95f4\u62e6\u7740\u4e0d\u5f80\u4e0a\u629b\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\u3002\u8c03\u6574\u4e86\u4e0b\uff0c\u76f4\u63a5\u628a <code>standard<\/code> \u8fd9\u4e2a Formatter \u548c <code>console<\/code> \u8fd9\u4e2a Handler \u7ed9\u53bb\u6389\uff0c\u5728 rootLogger \u4e0a\u6302 <code>consoleWithId<\/code> \u548c <code>sentry<\/code> \u5c31\u597d\uff0c\u6700\u540e\u5b8c\u6574\u7684\u914d\u7f6e\u5982\u4e0b<\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\nlogging_conf = {\r\n    &quot;version&quot;: 1,\r\n    &quot;disable_existing_loggers&quot;: True,\r\n    &quot;filters&quot;: {\r\n        &quot;addRequestId&quot;: {\r\n            &quot;()&quot;: &quot;app.utils.logging.request_filter.ContextFilter&quot;\r\n        }\r\n    },\r\n    &quot;formatters&quot;: {\r\n        &quot;request&quot;: {\r\n            &quot;format&quot;: &quot;&#x5B;%(asctime)s]&#x5B;%(levelname)s]&#x5B;%(relpath)s:%(lineno)s]&#x5B;%(funcName)s]&#x5B;%(request_id)s]: %(message)s&quot;\r\n        }\r\n    },\r\n    &quot;handlers&quot;: {\r\n        &quot;logfile&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;class&quot;: &quot;logging.handlers.WatchedFileHandler&quot;,\r\n            &quot;filters&quot;: &#x5B;&quot;addRequestId&quot;],\r\n            &quot;formatter&quot;: &quot;request&quot;,\r\n            &quot;filename&quot;: &quot;log\/app.log&quot;\r\n        },\r\n        &quot;consoleWithId&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;class&quot;: &quot;logging.StreamHandler&quot;,\r\n            &quot;filters&quot;: &#x5B;&quot;addRequestId&quot;],\r\n            &quot;formatter&quot;: &quot;request&quot;\r\n        },\r\n        &quot;sentry&quot;: {\r\n            &quot;level&quot;: &quot;ERROR&quot;,\r\n            &quot;class&quot;: &quot;raven.handlers.logging.SentryHandler&quot;,\r\n            &quot;dsn&quot;: &quot;DSN_URI&quot;,\r\n            &quot;string_max_length&quot;: 512000\r\n        }\r\n    },\r\n    &quot;loggers&quot;: {\r\n        &quot;app&quot;: {\r\n            &quot;level&quot;: &quot;DEBUG&quot;,\r\n            &quot;handlers&quot;: &#x5B;&quot;logfile&quot;],\r\n        }\r\n    },\r\n    &quot;root&quot;: {\r\n        &quot;level&quot;: &quot;DEBUG&quot;,\r\n        &quot;handlers&quot;: &#x5B;&quot;consoleWithId&quot;, &quot;sentry&quot;]\r\n    }\r\n}\r\n<\/pre>\n<p>\u56e0\u4e3a Logger \u7684 <code>propagate<\/code> \u9ed8\u8ba4\u5c31\u662f <code>True<\/code>\uff0c\u6240\u4ee5\u76f8\u5bf9\u4e8e\u7b2c\u4e00\u7248\u5728 <code>app<\/code> \u8fd9\u4e2a Logger \u4e0a\u53bb\u6389\u4e86\u8fd9\u6761\u914d\u7f6e\u4e5f\u6ca1\u5173\u7cfb<\/p>\n<p>\u6700\u540e\uff0c\u56e0\u4e3a <code>addRequestId<\/code> \u8fd9\u4e2a Filter \u8fd8\u662f\u4f1a\u88ab\u8c03\u4e24\u6b21\uff0c\u60f3\u4f18\u5316\u4e0b\u6027\u80fd\uff0c\u5c31\u5728 Filter \u505a\u5b8c\u540e\u52a0\u4e00\u4e2a\u6807\u8bb0\uff0c\u4e0b\u6b21\u518d\u8fdb\u6765\u5982\u679c\u770b\u5230\u6709\u8fd9\u4e2a\u6807\u8bb0\u5c31\u76f4\u63a5\u8df3\u8fc7\uff0c\u4ee5\u53ca\uff0c\u5bf9\u4e8e\u975e\u9879\u76ee\u5185\u7684\u65e5\u5fd7\u5c31\u4e0d\u8981\u7528\u9879\u76ee\u5185\u7684\u76f8\u5bf9\u8def\u5f84\u800c\u7528\u7edd\u5bf9\u8def\u5f84\u66ff\u4ee3\u3002\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# coding: utf8\r\n\r\nimport logging\r\nimport os.path\r\n\r\nfrom celery import current_task\r\nfrom flask import g, has_app_context, has_request_context\r\n\r\n\r\n_proj_root_path = os.path.abspath(os.path.join(__file__, '.\/..\/..\/..\/..\/'))\r\n_proj_root_length = len(_proj_root_path)\r\n\r\n\r\nclass ContextFilter(logging.Filter):\r\n    def filter(self, record):\r\n        # ignore duplicate filter\r\n        if hasattr(record, 'filter_by_yewen'):\r\n            return True\r\n\r\n        # request_id for flask web or celery task\r\n        request_id = 'Standalone'\r\n\r\n        if has_app_context():\r\n            if has_request_context():\r\n                request_id = g.get('request_id', 'UnknownRequest')\r\n            elif current_task:\r\n                request_id = current_task.request.id or 'UnknownTask'\r\n\r\n        record.request_id = request_id\r\n\r\n        # handle log_decorator pass\r\n        record.funcName = getattr(record, 'orig_funcName', record.funcName)\r\n        record.pathname = getattr(record, 'orig_pathname', record.pathname)\r\n        record.lineno = getattr(record, 'orig_lineno', record.lineno)\r\n\r\n        # relative path\r\n        if record.pathname.startswith(_proj_root_path):\r\n            record.relpath = record.pathname&#x5B;_proj_root_length:]\r\n        else:\r\n            record.relpath = record.pathname\r\n\r\n        record.filter_by_yewen = True\r\n\r\n        return True\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u867d\u7136\u524d\u9762\u81ea\u5df1\u5199\u8fc7\u4e00\u4e2a Python Logging \u7684\u5404\u79cd\u73a9\u6cd5\uff08\u6298\u817e Python logging \u7684\u4e00\u4e9b\u8bb0\u5f55 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[7],"tags":[689,688,686,127],"class_list":["post-3526","post","type-post","status-publish","format-standard","hentry","category-tech-notes","tag-celery","tag-flask","tag-logging","tag-python"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4aR5e-US","_links":{"self":[{"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/posts\/3526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/comments?post=3526"}],"version-history":[{"count":6,"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/posts\/3526\/revisions"}],"predecessor-version":[{"id":3532,"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/posts\/3526\/revisions\/3532"}],"wp:attachment":[{"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/media?parent=3526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/categories?post=3526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yewen.us\/blog\/wp-json\/wp\/v2\/tags?post=3526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}