{"id":10,"date":"2024-01-22T17:08:00","date_gmt":"2024-01-22T09:08:00","guid":{"rendered":"https:\/\/www.yuuriso.cn\/?p=10"},"modified":"2024-02-20T17:17:10","modified_gmt":"2024-02-20T09:17:10","slug":"python-logging","status":"publish","type":"post","link":"https:\/\/www.yuuriso.cn\/?p=10","title":{"rendered":"Python Logging"},"content":{"rendered":"\n<p>Python Logging \u662f\u5b98\u65b9\u7684\u6807\u51c6\u5e93,\u7528\u6765\u5b9e\u73b0\u65e5\u5fd7\u8bb0\u5f55\u7684\u529f\u80fd<\/p>\n\n\n\n<p>\u67e5\u770blogging\u5df2\u914d\u7f6e\u7684\u65e5\u5fd7\u4fdd\u5b58\u8def\u5f84<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> logging.getLoggerClass().root.handlers&#91;0].baseFilename<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u5219\u4e3a\u7a7a<\/p>\n\n\n\n<p>\u7ec4\u4ef6<br>Loggers\u3001handlers\u3001filters\u3001formatters<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loggers expose their interface that application code directly uses.<\/li>\n\n\n\n<li>Handlers send the log records (created by loggers) to the appropriate destination<\/li>\n\n\n\n<li>Filters provide a finer grained facility for determining which records to output<\/li>\n\n\n\n<li>Formattes specify the layout of records in the final output<\/li>\n<\/ul>\n\n\n\n<p>1.Logger<\/p>\n\n\n\n<p>Logger \u5bf9\u8c61\u4e0a\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u65b9\u6cd5\u5206\u4e3a\u4e24\u7c7b:\u914d\u7f6e\u548c\u6d88\u606f\u53d1\u9001<\/p>\n\n\n\n<p>\u53c2\u89c1\u7684\u914d\u7f6e\u65b9\u6cd5:<\/p>\n\n\n\n<p>1.Logger.setLevel()<br>2.Logger.addHandler()\u548cLogger.removeHandler()<br>3.Logger.addFilter()\u548cLogger.removeFilter()<\/p>\n\n\n\n<p>getLogger()\u8fd4\u56de\u5177\u6709\u6307\u5b9a\u540d\u79f0\u7684\u8bb0\u5f55\u5668\u5b9e\u4f8b\u7684\u5e94\u7528(\u5982\u679c\u5df2\u63d0\u4f9b),\u6d3b\u7740\u7684\u5982\u679c\u5219\u8fd4\u56de<code>root<\/code> . \u540d\u79f0\u662f\u4ee5\u636e\u70b9\u5206\u5272\u7684\u5c42\u6b21\u7ed3\u6784.\u591a\u6b21\u8c03\u7528\u8fd4\u56de\u5df2\u5b58\u5728\u5bf9\u8c61\u7684\u5f15\u7528.<br>\u5206\u6210\u5217\u8868\u4e2d\u8f83\u4f4e\u7684\u8bb0\u5f55\u5668\u662f\u5217\u8868\u4e2d\u8f83\u9ad8\u7684\u8bb0\u5f55\u5668\u7684\u5b50\u9879.<br>\u5982\u679c\u5b58\u5728<code>foo<\/code>\u7684\u8bb0\u5f55\u5668,\u540d\u79f0\u4e3a<code>foo.bar<\/code>\u3001<code>foo.bar.baz<\/code>\u7684\u8bb0\u5f55\u5668\u90fd\u662f<code>foo<\/code>\u7684\u5b50\u9879<\/p>\n\n\n\n<p>Logger \u6709<code>\u6709\u6548\u7b49\u7ea7<\/code>\u7684\u6982\u5ff5. \u5982\u679c\u56f4\u5728\u8bb0\u5f55\u5668\u4e0a\u663e\u5f0f\u8bbe\u7f6e\u7ea7\u522b,\u5219\u4f7f\u7528\u7236\u8bb0\u5f55\u5668\u7684\u7ea7\u522b\u4f5c\u4e3a\u6709\u6548\u7ea7\u522b,\u5982\u679c\u7236\u8bb0\u5f55\u5668\u6ca1\u6709\u660e\u786e\u7684\u7ea7\u522b\u8bbe\u7f6e,\u5219\u68c0\u67e5\u5176\u7236\u7ea7.\u4f9d\u6b64\u7c7b\u63a8,\u641c\u7d22\u4e0a\u7ea7\u5143\u7d20,\u77e5\u9053\u627e\u5230\u660e\u786e\u8bbe\u7f6e\u7684\u7ea7\u522b.\u6839\u8bb0\u5f55\u5668\u59cb\u7ec8\u6709\u663e\u5f0f\u7ea7\u522b\u96c6(\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e3a<code>WARNING<\/code>).\u5728\u51b3\u5b9a\u662f\u5426\u5904\u7406\u4e8b\u4ef6\u65f6,\u8bb0\u5f55\u5668\u7684\u6709\u6548\u7ea7\u522b\u786e\u5b9a\u662f\u5426\u4f20\u9012\u7ed9\u8bb0\u5f55\u5668\u76f8\u5173\u7684\u5904\u7406\u5668.<\/p>\n\n\n\n<p>\u5b50\u8bb0\u5f55\u5668\u5c06\u6d88\u606f\u4f20\u64ad\u5230\u4e0e\u5176\u7236\u7ea7\u8bb0\u5f55\u5668\u5173\u8054\u7684\u5904\u7406\u5668.\u56e0\u6b64\u4e0d\u5fc5\u4e3a\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u7684\u6240\u6709\u8bb0\u5f55\u5668\u8fdb\u884c\u5b9a\u4e49\u548c\u914d\u7f6e\u5904\u7406\u5668.\u4e00\u822c\u4e3a\u9876\u7ea7\u8bb0\u5f55\u5668\u914d\u7f6e\u5904\u7406\u5668,\u6839\u636e\u9700\u8981\u521b\u5efa\u5b50\u8bb0\u5f55\u5668\u96c6\u5c31\u8db3\u591f\u4e86<\/p>\n\n\n\n<p>\u53c2\u8003\u8d44\u6599<br>[1].<a href=\"https:\/\/docs.python.org\/zh-cn\/3.6\/howto\/logging-cookbook.html#customizing-handlers-with-dictconfig\">logging-cookbook<\/a><\/p>\n\n\n\n<p>[2].<a href=\"http:\/\/plumberjack.blogspot.com\/\">logging\u2018s author blog<\/a><\/p>\n\n\n\n<p>[3].<a href=\"https:\/\/github.com\/python\/cpython\/tree\/main\/Lib\/logging\">commit log of python logging module<\/a><\/p>\n\n\n\n<p>[4].<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0282\/\">PEP 282 &#8212; A Logging System<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Logging \u662f\u5b98\u65b9\u7684\u6807\u51c6\u5e93,\u7528\u6765\u5b9e\u73b0\u65e5\u5fd7\u8bb0\u5f55\u7684\u529f\u80fd \u67e5\u770blogging\u5df2\u914d\u7f6e\u7684\u65e5\u5fd7\u4fdd\u5b58\u8def\u5f84  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":1,"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":12,"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions\/12"}],"wp:attachment":[{"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yuuriso.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}