{"id":1780,"date":"2024-06-11T01:44:37","date_gmt":"2024-06-10T17:44:37","guid":{"rendered":"https:\/\/www.dingdongai.com\/?p=1780"},"modified":"2024-06-11T01:44:37","modified_gmt":"2024-06-10T17:44:37","slug":"%e6%97%a5%e5%bf%97%e7%ba%a7%e5%88%ab%e5%92%8c%e6%97%a5%e5%bf%97%e5%88%86%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.dingdongai.com\/?p=1780","title":{"rendered":"\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u5206\u7c7b"},"content":{"rendered":"<p>\u667a\u80fd\u52a9\u624b\u7684\u7f16\u7a0b\u5e2e\u52a9\u4e0e\u4ee3\u7801\u8c03\u8bd5\u7684\u547d\u4ee4\u884c\u5de5\u5177\u5f00\u53d1\u7684\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u65e5\u5fd7\u7684\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u5206\u7c7b<\/p>\n<h1>\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u65e5\u5fd7<\/h1>\n<p>\u5728\u547d\u4ee4\u884c\u5de5\u5177\u5f00\u53d1\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u548c\u9519\u8bef\u65e5\u5fd7\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53ca\u65f6\u53d1\u73b0\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u800c\u9519\u8bef\u65e5\u5fd7\u5219\u8bb0\u5f55\u4e86\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u65b9\u4fbf\u6211\u4eec\u8fdb\u884c\u95ee\u9898\u6392\u67e5\u548c\u8c03\u8bd5\u3002<\/p>\n<h2>\u5f02\u5e38\u5904\u7406<\/h2>\n<p>\u5728\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6587\u4ef6\u8bfb\u5199\u9519\u8bef\u3001\u7f51\u7edc\u8fde\u63a5\u5931\u8d25\u7b49\u3002\u4e3a\u4e86\u80fd\u591f\u53ca\u65f6\u53d1\u73b0\u8fd9\u4e9b\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<p><code>python<br \/>\ntry:<br \/>\n    # \u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801<br \/>\nexcept Exception as e:<br \/>\n    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801<br \/>\n<\/code><\/p>\n<p>\u901a\u8fc7try-except\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u5230\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u5e76\u5728except\u5757\u4e2d\u7f16\u5199\u76f8\u5e94\u7684\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u3002\u5728\u5904\u7406\u5f02\u5e38\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u629b\u51fa\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u6216\u8005\u8fdb\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u64cd\u4f5c\u3002<\/p>\n<h2>\u9519\u8bef\u65e5\u5fd7<\/h2>\n<p>\u5728\u547d\u4ee4\u884c\u5de5\u5177\u5f00\u53d1\u4e2d\uff0c\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9519\u8bef\u65e5\u5fd7\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u95ee\u9898\uff0c\u5e76\u8fdb\u884c\u95ee\u9898\u6392\u67e5\u548c\u8c03\u8bd5\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528logging\u6a21\u5757\u6765\u5b9e\u73b0\u9519\u8bef\u65e5\u5fd7\u7684\u8bb0\u5f55\u3002<\/p>\n<p>&#8220;`python<br \/>\nimport logging<\/p>\n<h1>\u521b\u5efalogger\u5bf9\u8c61<\/h1>\n<p>logger = logging.getLogger(&#8216;mylogger&#8217;)<\/p>\n<h1>\u521b\u5efa\u6587\u4ef6\u5904\u7406\u5668<\/h1>\n<p>fh = logging.FileHandler(&#8216;error.log&#8217;)<\/p>\n<h1>\u521b\u5efa\u65e5\u5fd7\u683c\u5f0f\u5668<\/h1>\n<p>formatter = logging.Formatter(&#8216;%(asctime)s %(levelname)s %(message)s&#8217;)<\/p>\n<h1>\u6dfb\u52a0\u65e5\u5fd7\u683c\u5f0f\u5668\u5230\u5904\u7406\u5668<\/h1>\n<p>fh.setFormatter(formatter)<\/p>\n<h1>\u6dfb\u52a0\u5904\u7406\u5668\u5230logger\u5bf9\u8c61<\/h1>\n<p>logger.addHandler(fh)<\/p>\n<h1>\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b<\/h1>\n<p>logger.setLevel(logging.ERROR)<\/p>\n<h1>\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7<\/h1>\n<p>logger.error(&#8216;This is an error message&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2alogger\u5bf9\u8c61\uff0c\u5e76\u5c06\u9519\u8bef\u65e5\u5fd7\u8bb0\u5f55\u5230\u6587\u4ef6&#8217;error.log&#8217;\u4e2d\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff0c\u53ea\u8bb0\u5f55\u6307\u5b9a\u7ea7\u522b\u4ee5\u4e0a\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<h1>\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u5206\u7c7b<\/h1>\n<p>\u5728\u9519\u8bef\u65e5\u5fd7\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u5206\u7c7b\u3002\u5e38\u89c1\u7684\u65e5\u5fd7\u7ea7\u522b\u5305\u62ecDEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\uff0c\u800c\u65e5\u5fd7\u5206\u7c7b\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6a21\u5757\u6216\u529f\u80fd\u8fdb\u884c\u5212\u5206\u3002<\/p>\n<p>\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u5206\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u8fdb\u884c\u9519\u8bef\u65e5\u5fd7\u7684\u7ba1\u7406\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u667a\u80fd\u52a9\u624b\u7684\u7f16\u7a0b\u5e2e\u52a9\u4e0e\u4ee3\u7801\u8c03\u8bd5\u7684\u547d\u4ee4\u884c\u5de5\u5177\u5f00\u53d1\u7684\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u65e5\u5fd7\u7684\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u5206\u7c7b \u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u65e5\u5fd7 \u5728\u547d &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.dingdongai.com\/?p=1780\"> <span class=\"screen-reader-text\">\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u5206\u7c7b<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"","footnotes":""},"categories":[1],"tags":[384,2491,2493,2492,41,604],"class_list":["post-1780","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-384","tag-2491","tag-2493","tag-2492","tag-41","tag-604"],"_links":{"self":[{"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=\/wp\/v2\/posts\/1780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1780"}],"version-history":[{"count":0,"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=\/wp\/v2\/posts\/1780\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dingdongai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}