{"id":5106,"date":"2022-05-08T22:01:00","date_gmt":"2022-05-08T14:01:00","guid":{"rendered":"https:\/\/www.yumefx.com\/?p=5106"},"modified":"2022-05-08T23:37:17","modified_gmt":"2022-05-08T15:37:17","slug":"python%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97%e8%af%a6%e8%a7%a3%e4%b9%8blogging","status":"publish","type":"post","link":"https:\/\/www.yumefx.com\/?p=5106","title":{"rendered":"Python\u5e38\u7528\u6a21\u5757\u4e4blogging"},"content":{"rendered":"\n<h1 style=\"text-align: center;\">\u524d\u8a00<\/h1>\n<p>\u5bf9\u4e8epython\u811a\u672c\u6765\u8bf4\uff0c\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u96be\u514d\u4f1a\u51fa\u73b0\u6743\u9650\u3001\u7f16\u7801\u3001\u903b\u8f91\u7b49\u5404\u79cd\u5404\u6837\u7684bug\uff0c\u7279\u522b\u662f\u6253\u5305\u597d\u7684python\u7a0b\u5e8f\uff0c\u51fa\u73b0\u62a5\u9519\u6216\u8005\u95ea\u9000\u65f6\u5f80\u5f80\u96be\u4ee5\u6392\u67e5\u9519\u8bef\u539f\u56e0\uff0c\u56e0\u6b64\u6709\u5fc5\u8981\u5728\u8fd0\u884cpython\u7a0b\u5e8f\u65f6\u4fdd\u5b58\u4e00\u4e9b\u8fd0\u884c\u4fe1\u606f\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u4ece\u800c\u8bb0\u5f55\u4f7f\u7528\u8005\u7684\u64cd\u4f5c\u548c\u51fa\u73b0\u7684\u9519\u8bef\u4ee5\u53ca\u8b66\u544a\u4fe1\u606f\u7b49\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u4eca\u5929\u5c31\u4ecb\u7ecd\u4e00\u4e0bpython\u4e2d\u6700\u5e38\u7528\u7684\u65e5\u5fd7\u5904\u7406\u6a21\u5757\u2014\u2014<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/logging.html#logging.Handler\">logging<\/a>\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">logging\u6a21\u5757\u4ecb\u7ecd<\/h1>\n<p>logging\u6a21\u5757\u662fPython\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u8bb0\u5f55\u65e5\u5fd7\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\u662f\u4e25\u91cd\u7ea7\u522b\uff08level\uff09\uff0c\u5171\u6709\u4ee5\u4e0b5\u4e2a\u7b49\u7ea7\u3002<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 138px;\">\n<tbody>\n<tr style=\"height: 23px;\">\n<td style=\"width: 19.2857%; height: 23px;\">\u65e5\u5fd7\u4e25\u91cd\u7ea7\u522b<\/td>\n<td style=\"width: 80.7143%; height: 23px;\">\u63cf\u8ff0<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 19.2857%; height: 23px;\">DEBUG<\/td>\n<td style=\"width: 80.7143%; height: 23px;\">\u8c03\u8bd5\u4fe1\u606f\uff0c\u901a\u5e38\u5728\u6392\u67e5\u95ee\u9898\u65f6\u5f00\u542f<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 19.2857%; height: 23px;\">INFO<\/td>\n<td style=\"width: 80.7143%; height: 23px;\">\u666e\u901a\u4fe1\u606f\uff0c\u7a0b\u5e8f\u6309\u7167\u9884\u671f\u8fd0\u884c<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 19.2857%; height: 23px;\">WARNING<\/td>\n<td style=\"width: 80.7143%; height: 23px;\">\u8b66\u544a\u4fe1\u606f\uff0c\u8868\u793a\u53d1\u751f\u4e86\u610f\u60f3\u4e0d\u5230\u7684\u4e8b\u60c5\u6216\u8005\u63a5\u4e0b\u6765<strong>\u53ef\u80fd<\/strong>\u4f1a\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\uff0c\u4f46\u7a0b\u5e8f\u4ecd\u53ef\u4ee5\u7ee7\u7eed\u8fd0\u884c\u3002<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 19.2857%; height: 23px;\">ERROR<\/td>\n<td style=\"width: 80.7143%; height: 23px;\">\u9519\u8bef\u4fe1\u606f\uff0c\u8868\u793a\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u51fa\u73b0\u4e86\u4e00\u4e9b\u95ee\u9898\uff0c\u7a0b\u5e8f\u7684<strong>\u90e8\u5206\u529f\u80fd<\/strong>\u4e0d\u80fd\u6b63\u5e38\u6267\u884c\u3002<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 19.2857%; height: 23px;\">CRITICAL<\/td>\n<td style=\"width: 80.7143%; height: 23px;\">\u5371\u9669\u4fe1\u606f\uff0c\u8868\u793a\u51fa\u73b0\u4e86\u4e25\u91cd\u7684\u9519\u8bef\uff0c\u5bfc\u81f4\u7a0b\u5e8f<strong>\u4e0d\u80fd\u7ee7\u7eed<\/strong>\u8fd0\u884c\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d\u7531\u4e0a\u5230\u4e0b\uff0c\u7ea7\u522b\u8d8a\u6765\u8d8a\u9ad8\uff0c\u6253\u5370\u7684\u65e5\u5fd7\u6570\u91cf\u8d8a\u6765\u8d8a\u5c11\uff0c\u6700\u5e38\u7528\u7684\u662fINFO\u3001WARNING\u548cERROR\u4e09\u4e2a\u7b49\u7ea7\uff0c\u800cDEBUG\u7ea7\u522b\u5f80\u5f80\u4f1a\u6253\u5370\u5927\u91cf\u65e5\u5fd7\uff0c\u56e0\u6b64\u5f80\u5f80\u5728\u6392\u67e5\u95ee\u9898\u65f6\u624d\u4f1a\u5f00\u542f\u3002<\/p>\n<p>\u5f53\u6307\u5b9a\u4e00\u4e2a\u65e5\u5fd7\u7ea7\u522b\u4e4b\u540e\uff0c\u4f1a\u8bb0\u5f55\u5927\u4e8e\u6216\u7b49\u4e8e\u8fd9\u4e2a\u65e5\u5fd7\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u5c0f\u4e8e\u7684\u5c06\u4f1a\u88ab\u4e22\u5f03\uff0c \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u65e5\u5fd7\u4f1a\u6253\u5370\u5927\u4e8e\u7b49\u4e8e WARNING \u7ea7\u522b\u7684\u65e5\u5fd7\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">logging\u6a21\u5757\u7684\u4f7f\u7528<\/h1>\n<p>\u6700\u7b80\u5355\u7684\u4f7f\u7528\u65b9\u5f0f\u5c31\u662f\u76f4\u63a5\u8c03\u7528\u6253\u5370\u65e5\u5fd7\u51fd\u6570\u3002<\/p>\n\n\n\n<pre><span>import logging<\/span>\n<span><\/span>\n<span># \u6253\u5370\u65e5\u5fd7<\/span>\n<span>logging.debug('Python debug')<\/span>\n<span>logging.info('Python info')<\/span>\n<span>logging.warning('Python warning')<\/span>\n<span>logging.error('%s'%('Python Error'))<\/span>\n<span>logging.critical('Python critical')<\/span>\n<\/pre>\n\n\n\n<p>\u7c7b\u4f3c\u4e8eprint\u51fd\u6570\uff0c\u6253\u5370\u65e5\u5fd7\u51fd\u6570\u4e5f\u53ef\u4ee5\u901a\u8fc7%s\u3001%d\u7b49\u5360\u4f4d\u7b26\u5c06python\u7a0b\u5e8f\u4e2d\u7684\u53d8\u91cf\u6253\u5370\u5230\u65e5\u5fd7\u4e2d\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre><span>WARNING:root:Python warning<\/span>\n<span>ERROR:root:Python Error<\/span>\n<span>CRITICAL:root:Python critical<\/span>\n<\/pre>\n\n\n\n<p>\u4f46\u8fd9\u6837\u53ea\u662f\u5c06\u65e5\u5fd7\u6253\u5370\u5230\u63a7\u5236\u53f0\uff0c\u4e3a\u4e86\u65b9\u4fbf\u50a8\u5b58\u548c\u67e5\u770b\uff0c\u8fd8\u662f\u5e94\u8be5\u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre><span>import logging<\/span>\n<span><\/span>\n<span>logging.basicConfig(filename='yumefx.log',level=logging.INFO)<\/span>\n<span>logging.debug(\"debug message.\")<\/span>\n<span>logging.info(\"info message.\")<\/span>\n<span>logging.warning(\"warning message.\")<\/span>\n<\/pre>\n\n\n\n<p>\u5176\u4e2dbasicConfig\u51fd\u6570\u4e2d\u7684filename\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u65e5\u5fd7\u6587\u4ef6\u7684\u4fdd\u5b58\u8def\u5f84\uff0c\u540e\u9762\u8c03\u7528\u7684\u6253\u5370\u65e5\u5fd7\u51fd\u6570\u5c31\u4f1a\u81ea\u52a8\u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u63a7\u5236\u53f0\u4e2d\u5219\u4e0d\u518d\u8f93\u51fa\u3002<\/p>\n<p>level\u53c2\u6570\u5219\u662f\u8bbe\u7f6e\u65e5\u5fd7\u7684\u4e25\u91cd\u7ea7\u522b\uff0c\u5f53\u524d\u65e5\u5fd7\u7ea7\u522b\u8bbe\u7f6e\u4e3aINFO\uff0c\u5219\u5927\u4e8e\u7b49\u4e8eINFO\u7ea7\u522b\u7684\u65e5\u5fd7\u4f1a\u88ab\u8f93\u51fa\uff0c\u5c0f\u4e8eINFO\u7ea7\u522b\u7684\u65e5\u5fd7\u4f1a\u88ab\u4e22\u5f03\uff0c\u56e0\u6b64\u751f\u6210\u7684\u65e5\u5fd7\u6587\u4ef6\u4e2d\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre><span>INFO:root:info message.<\/span>\n<span>WARNING:root:warning message.<\/span>\n<\/pre>\n\n\n\n<p>basicConfig\u51fd\u6570\u4e2d\u8fd8\u6709\u4e00\u4e2afilemode\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3aa\uff0c\u4ee3\u8868\u6bcf\u6b21\u8f93\u51fa\u65e5\u5fd7\u65f6\u90fd\u8ffd\u52a0\u5230\u65e5\u5fd7\u6587\u4ef6\u672b\u5c3e\uff0c\u5982\u679c\u9700\u8981\u6539\u4e3a\u6bcf\u6b21\u8f93\u51fa\u65e5\u5fd7\u90fd\u8986\u76d6\u7684\u8bdd\uff0c\u53ef\u4ee5\u6539\u4e3aw\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3>\u65e5\u5fd7\u683c\u5f0f<\/h3>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4e0a\u9762\u8f93\u51fa\u7684\u65e5\u5fd7\u5f88\u7b80\u5355\uff0c\u53ea\u6709root\u52a0\u4e0a\u65e5\u5fd7\u4fe1\u606f\uff08root\u7684\u542b\u4e49\u540e\u9762\u4f1a\u4ecb\u7ecd\uff09\uff0c\u6ca1\u6709\u65e5\u5fd7\u65f6\u95f4\u4e4b\u7c7b\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u5728basicConfig\u51fd\u6570\u901a\u8fc7format\u53c2\u6570\u8bbe\u7f6e\u8f93\u51fa\u65e5\u5fd7\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre><span>import logging<\/span>\n<span><\/span>\n<span>logging.basicConfig(filename='yumefx.log', format='%(asctime)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')<\/span>\n<span>logging.warning('warning message.')<\/span>\n<\/pre>\n\n\n\n<p>\u8f93\u51fa\u7684\u65e5\u5fd7\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre><span>2022-05-04 22:48:07: warning message.<\/span>\n<\/pre>\n\n\n\n<p>format\u53c2\u6570\u4e2d\u7684asctime\u5b57\u6bb5\u4e3a\u65e5\u5fd7\u4e8b\u4ef6\u53d1\u751f\u65f6\u7684\u65f6\u95f4\uff0c\u914d\u5408datefmt\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u65f6\u95f4\u7684\u683c\u5f0f\uff0c\u4ece\u800c\u5c31\u53ef\u4ee5\u8bb0\u5f55\u6bcf\u6b21\u8f93\u51fa\u65e5\u5fd7\u7684\u5177\u4f53\u65f6\u95f4\u3002<\/p>\n<p>format\u53c2\u6570\u4e2d\u5e38\u7528\u7684\u5b57\u6bb5\u5982\u4e0b\uff1a<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 368px;\">\n<tbody>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">\u5b57\u6bb5\u540d\u79f0<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">\u4f7f\u7528\u683c\u5f0f<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u63cf\u8ff0<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">asctime<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(asctime)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u65e5\u5fd7\u4e8b\u4ef6\u53d1\u751f\u65f6\u7684\u65f6\u95f4<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">created<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(created)f<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u65e5\u5fd7\u4e8b\u4ef6\u53d1\u751f\u65f6\u7684\u65f6\u95f4\u6233<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">relativeCreated<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(relativeCreated)d<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u65e5\u5fd7\u4e8b\u4ef6\u53d1\u751f\u7684\u65f6\u95f4\u76f8\u5bf9\u4e8elogging\u6a21\u5757\u52a0\u8f7d\u65f6\u95f4\u7684\u76f8\u5bf9\u6beb\u79d2\u6570\uff08\u53ef\u4ee5\u7528\u4e8e\u8fd1\u4f3c\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff09\u3002<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">msecs<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(msecs)d<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u65e5\u5fd7\u4e8b\u4ef6\u53d1\u751f\u65f6\u7684\u65f6\u95f4\u7684\u6beb\u79d2<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">levvelname<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(levelname)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u8be5\u65e5\u5fd7\u4e8b\u4ef6\u6240\u5c5e\u7684\u4e25\u91cd\u7ea7\u522b<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">message<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(message)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u65e5\u5fd7\u7684\u6587\u672c\u5185\u5bb9<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">pathname<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(pathname)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u8c03\u7528\u65e5\u5fd7\u8bb0\u5f55\u51fd\u6570\u7684\u6e90\u7801\u6587\u4ef6\u7684\u8def\u5f84<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">filename<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(filename)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">pathname\u7684\u6587\u4ef6\u5168\u540d\uff0c\u542b\u6587\u4ef6\u540e\u7f00<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">module<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(module)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">filename\u7684\u6587\u4ef6\u540d\u90e8\u5206\uff0c\u4e0d\u542b\u6587\u4ef6\u540e\u7f00<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">lineno<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(lineno)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u8c03\u7528\u65e5\u5fd7\u8bb0\u5f55\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u6240\u5728\u7684\u884c\u53f7<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">funcName<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(funcName)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u8c03\u7528\u65e5\u5fd7\u8bb0\u5f55\u51fd\u6570\u7684\u51fd\u6570\u540d<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">process<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(process)d<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u8fdb\u7a0bID<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">processName<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(processName)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u8fdb\u7a0b\u540d\u79f0\uff0cPython3.1\u65b0\u589e<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">thread<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(thread)d<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u7ebf\u7a0bID<\/td>\n<\/tr>\n<tr style=\"height: 23px;\">\n<td style=\"width: 18.9285%; height: 23px;\">threadName<\/td>\n<td style=\"width: 21.1905%; height: 23px;\">%(threadName)s<\/td>\n<td style=\"width: 59.8809%; height: 23px;\">\u7ebf\u7a0b\u540d\u79f0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u5b57\u6bb5\u8bbe\u7f6eformat\u53c2\u6570\uff0c\u5c31\u53ef\u4ee5\u8ba9\u8f93\u51fa\u7684\u65e5\u5fd7\u5305\u542b\u6240\u9700\u7684\u8fd0\u884c\u4fe1\u606f\uff0c\u518d\u51fa\u73b0\u95ee\u9898\u65f6\u65b9\u4fbf\u6392\u67e5\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3>\u6807\u51c6\u4f7f\u7528<\/h3>\n<p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u50cf\u524d\u9762\u90a3\u6837\u901a\u8fc7basicConfig\u51fd\u6570\u914d\u7f6e\u597d\u53c2\u6570\u548c\u683c\u5f0f\u540e\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\uff0c\u4e0d\u8fc7\u8fd9\u53ea\u662f\u6700\u7b80\u6613\u7684\u7528\u6cd5\u3002<\/p>\n<p>\u6807\u51c6\u7684\u7528\u6cd5\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre><span>import logging<\/span>\n<span><\/span>\n<span>#\u521b\u5efalogger\u5bf9\u8c61<\/span>\n<span>log=logging.getLogger(\"example\")<\/span>\n<span>log.setLevel(logging.DEBUG)<\/span>\n<span><\/span>\n<span>#\u521b\u5efahandler\uff0c\u7528\u4e8e\u5b9a\u4e49\u8f93\u51fa\u76ee\u7684\u5730<\/span>\n<span>handler1=logging.StreamHandler()<\/span>\n<span>handler2=logging.FileHandler('yumefx.log',mode='w')<\/span>\n<span>handler1.setLevel(logging.INFO)<\/span>\n<span>handler2.setLevel(logging.DEBUG)<\/span>\n<span><\/span>\n<span>#\u5b9a\u4e49handler\u7684\u8f93\u51fa\u683c\u5f0f<\/span>\n<span>formatter1=logging.Formatter(\"%(asctime)s-%(name)s-%(levelname)s-%(message)s\")<\/span>\n<span>formatter2=logging.Formatter(\"%(levelname)s : %(message)s\")<\/span>\n<span>handler1.setFormatter(formatter1)<\/span>\n<span>handler2.setFormatter(formatter2)<\/span>\n<span><\/span>\n<span>#\u5c06handler\u6dfb\u52a0\u5230\u5bf9\u5e94\u7684logger\u5bf9\u8c61<\/span>\n<span>log.addHandler(handler1)<\/span>\n<span>log.addHandler(handler2)<\/span>\n<span>log.info(\"info message.\")<\/span>\n<span>log.debug(\"debug message.\")<\/span>\n<span>log.warning(\"warnning message.\")<\/span>\n<\/pre>\n\n\n\n<p>\u5b9e\u73b0\u7684\u6548\u679c\u662f\u4e00\u6b21\u8c03\u7528\u8f93\u51fa\u65e5\u5fd7\u51fd\u6570\uff0c\u63a7\u5236\u53f0\u548c\u65e5\u5fd7\u6587\u4ef6\u91cc\u90fd\u4f1a\u8f93\u51fa\u65e5\u5fd7\uff0c\u5e76\u4e14\u65e5\u5fd7\u7684\u53c2\u6570\u548c\u683c\u5f0f\u5404\u4e0d\u76f8\u540c\uff0c\u63a7\u5236\u53f0\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre><span>2022-05-04 23:39:37,421-example-INFO-info message.<\/span>\n<span>2022-05-04 23:39:37,421-example-WARNING-warnning message.<\/span>\n<\/pre>\n\n\n\n<p>\u65e5\u5fd7\u6587\u4ef6\u4e2d\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre><span>INFO : info message.<\/span>\n<span>DEBUG : debug message.<\/span>\n<span>WARNING : warnning message.<\/span>\n<\/pre>\n\n\n\n<p>\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0clogging.getLogger(name=\u201cexample\u201d)\u8fd4\u56de\u4e00\u4e2alogger\u7684\u5bf9\u8c61\uff0cname\u53c2\u6570\u5982\u679c\u4e3a\u7a7a\u5219\u9ed8\u8ba4\u4f7f\u7528\u7684\u662froot logger\uff08\u8fd9\u5c31\u662f\u6700\u524d\u9762\u7684\u793a\u4f8b\u4e2d\u65e5\u5fd7\u4fe1\u606f\u91cc\u9762root\u7684\u7531\u6765\uff09\uff0c\u5426\u5219\u5373\u4e3a\u4f7f\u7528\u81ea\u5df1\u5b9a\u4e49\u7684logger\u3002\u6240\u6709\u8c03\u7528\u65e5\u5fd7\u7684\u63a5\u53e3\uff0c\u5982\u679clogger name\u4e00\u6837\u5219\u90fd\u662f\u8c03\u7528\u540c\u4e00\u4e2alogger\u5b9e\u4f8b\u3002<\/p>\n<p>StreamHandler\u51fd\u6570\u548cFileHandler\u51fd\u6570\u5219\u662f\u5b9a\u4e49\u5c06logger\u5bf9\u8c61\u7684\u65e5\u5fd7\u8f93\u51fa\u5230\u4ec0\u4e48\u6837\u7684\u4f4d\u7f6e\uff0c\u7edf\u79f0\u4e3aHandler\u3002\u4e00\u4e2alogger\u4e2d\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2aHandler\uff0c\u5c06\u540c\u4e00\u6761\u65e5\u5fd7\u8bb0\u5f55\u8f93\u51fa\u5230\u4e0d\u540c\u7684\u4f4d\u7f6e\u3002\u5176\u4ed6\u53ef\u7528\u7684handler\u8fd8\u6709SocketHandler\u3001SMTPHandler\u3001SysLogHandler\u3001MemoryHandler\u3001HTTPHandler\u7b49\u7b49\uff0c\u4f46\u4f7f\u7528\u9891\u7387\u5e76\u4e0d\u9ad8\u3002<\/p>\n<p>Formatter\u51fd\u6570\u5219\u4e0ebasicConfig\u51fd\u6570\u7684format\u53c2\u6570\u7c7b\u4f3c\uff0c\u90fd\u662f\u7528\u4e8e\u5b9a\u4e49\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u9700\u8981\u6dfb\u52a0\u5230Handler\u4e0a\u624d\u80fd\u751f\u6548\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u53ef\u7528\u7684\u51fd\u6570\u5219\u662fFilter\uff0c\u5b9a\u4e49\u540e\u52a0\u5230Handler\u4e0a\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u65e5\u5fd7\u8fc7\u6ee4\uff0c\u4e00\u4e2aHandler\u4e0a\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2aFilter\uff0c\u4e00\u6761\u65e5\u5fd7\u8bb0\u5f55\u5fc5\u987b\u6240\u6709\u8fc7\u6ee4\u5668\u90fd\u901a\u8fc7\u624d\u80fd\u6b63\u5e38\u8f93\u51fa\uff0c\u5426\u5219\u5c31\u4f1a\u88ab\u4e22\u5f03\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8be6\u7ec6\u8bf4\u4e86\u3002<\/p>\n<p>\u9664\u4e86\u4e0a\u9762\u8fd9\u6837\u7684\u663e\u5f0f\u5b9a\u4e49\u914d\u7f6e\u53c2\u6570\u5916\uff0clogging\u8fd8\u652f\u6301\u901a\u8fc7dictConfig\uff08\u5b57\u5178\u53c2\u6570\uff09\u3001fileConfig\uff08\u914d\u7f6e\u6587\u4ef6\uff09\u7b49\u65b9\u5f0f\u6765\u5b9a\u4e49\u914d\u7f6e\u53c2\u6570\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u524d\u9762\u7684\u7b80\u6613\u7528\u6cd5\u7684\u5b9e\u8d28\u662flogging\u6a21\u5757\u5728\u5bfc\u5165\u65f6\u81ea\u52a8\u751f\u6210\u4e00\u4e2aroot logger\uff0c\u8fd9\u4e2aroot logger\u7684\u5b9e\u4f8b\u53ef\u4ee5\u901a\u8fc7<code>logging.getLogger()<\/code>\u00a0\u5f97\u5230\uff0c\u4e4b\u540e\u7684basicConfig\u914d\u7f6e\u51fd\u6570\u548c\u65e5\u5fd7\u6253\u5370\u51fd\u6570\u90fd\u662f\u5728\u8fd9\u4e2aroot logger\u7684\u5b9e\u4f8b\u4e0a\u6267\u884c\u7684\uff0c\u5373<code>logging.debug<\/code>\u7b49\u4ef7\u4e8e<code>rootLogger.debug<\/code>\uff0c\u5e76\u4e14basicConfig\u914d\u7f6e\u51fd\u6570\u662f\u4e00\u4e2a\u4e00\u6b21\u6027\u65b9\u6cd5\uff0c\u53ea\u6709\u7b2c\u4e00\u6b21\u8bbe\u7f6e\u65f6\u4f1a\u751f\u6548\uff0c\u540e\u9762\u5c31\u4e0d\u80fd\u4fee\u6539\u4e86\uff0c\u6240\u4ee5\u5e94\u8be5\u5c3d\u91cf\u4f7f\u7528\u6807\u51c6\u7528\u6cd5\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3>logger\u7684\u6811\u5f62\u7ed3\u6784<\/h3>\n<p>logger\u662f\u6709\u6811\u5f62\u7ed3\u6784\u7684\uff0c\u9ed8\u8ba4\u751f\u6210\u7684root logger\u5c31\u662f\u6811\u5f62\u7ed3\u6784\u7684\u6839\uff0c\u901a\u8fc7 <code>getLogger(loggername)<\/code> \u65b9\u6cd5\u83b7\u5f97\u7684 logger \u5bf9\u8c61\u90fd\u662f\u4ece root logger \u4f38\u5c55\u51fa\u6765\u7684\u679d\u53f6\u3002<\/p>\n<p>\u5c42\u6b21\u7ed3\u6784\u7528<code>.<\/code>\u5206\u9694<\/p>\n\n\n\n<pre>\n<span>logger = logging.getLogger(&#x27;mod1.mod2.mod3&#x27;)<\/span>\n<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9e\u9645\u4e0a\u521b\u5efa\u4e86\u4e09\u4e2a logger\uff0c\u540d\u79f0\u5206\u522b\u662f mod1\u3001mod1.mod2 \u548c mod1.mod2.mod3\uff0cmod1 \u4e3a\u6839\uff0cmod1.mod2 \u4e3a\u5b50\uff0cmod1.mod2.mod3 \u4e3a\u5b59\u3002<\/p>\n<p>\u5982\u679c\u5728 mod1 \u4e0a\u8bbe\u7f6e\u4e86Handler\uff0c\u90a3\u4e48\u5176\u4ed6\u4e24\u4e2a\u7684\u65e5\u5fd7\u5bf9\u8c61\u90fd\u4f1a\u4f7f\u7528\u8fd9\u4e2aHandler\uff0c\u8fd9\u6837\u8bb0\u5f55\u7684\u65e5\u5fd7\u66f4\u6e05\u6670\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e3a\u540c\u4e00\u4e2a\u6839\u7684\u65e5\u5fd7\u5bf9\u8c61\u8bbe\u7f6e\u53ef\u4ee5\u5171\u4eab\u7684\u65e5\u5fd7\u5904\u7406\u65b9\u5f0f\u3002\u3002<\/p>\n\n\n\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">logging\u7684\u4e00\u4e9b\u7279\u70b9<\/h1>\n<h3>\u4e00\u6b21\u5b9a\u4e49\uff0c\u968f\u5904\u4f7f\u7528<\/h3>\n<p>\u914d\u7f6e\u597d\u65e5\u5fd7\u8f93\u51fa\u7684\u53c2\u6570\u548c\u683c\u5f0f\u540e\uff0c\u540e\u9762\u6240\u6709\u8c03\u7528\u8f93\u51fa\u65e5\u5fd7\u7684\u4ee3\u7801\uff0c\u90fd\u6309\u7167\u8fd9\u4e2a\u53c2\u6570\u548c\u683c\u5f0f\u8fdb\u884c\u8f93\u51fa\uff0c\u4e0d\u9700\u8981\u5728\u4e0d\u540c\u7684\u6a21\u5757\u6216\u51fd\u6570\u4e2d\u4f20\u8f93logger\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u5927\u5927\u63d0\u5347\u4e86\u4f7f\u7528\u7684\u4fbf\u6377\u6027\u3002<\/p>\n<p>\u00a0<\/p>\n<h3>\u7ebf\u7a0b\u5b89\u5168<\/h3>\n<p>logging\u6a21\u5757\u4e0d\u9700\u8981\u7528\u6237\u518d\u505a\u5176\u4ed6\u7684\u5b89\u5168\u64cd\u4f5c\u907f\u514d\u6587\u4ef6\u8bfb\u5199\u51b2\u7a81\u548c\u63d0\u9ad8\u7ebf\u7a0b\u8c03\u7528\u5b89\u5168\uff0c\u5b83\u5185\u90e8\u5df2\u7ecf\u5b9e\u73b0\u4e86\u7ebf\u7a0b\u9501\uff0c\u6bcf\u4e00\u4e2a\u6a21\u5757\u8c03\u7528\u7684\u65f6\u5019\u662f\u9501\u5b9a\u4e86\u5171\u4eab\u6570\u636e\u533a\u57df\uff0c\u6bcf\u4e00\u4e2ahandler\u4e5f\u5bf9IO\u8fdb\u884c\u52a0\u9501\u64cd\u4f5c\u3002<\/p>\n<p>\u00a0<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e0d\u8981\u53ea\u6ce8\u91cd\u5f62\u5f0f\u4e0a\u7684\u4fee\u8f9e\u800c\u653e\u5f03\u4e86\u5b9e\u8d28\u4e0a\u7684\u601d\u8003\u3002<\/p>\n<p style=\"text-align: right;\">\u300a\u6c89\u601d\u5f55\u300b<br \/>\u2014\u2014\u9a6c\u514b\u00b7\u5965\u52d2\u7559<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5bf9\u4e8epython\u811a\u672c\u6765\u8bf4\uff0c\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u96be\u514d\u4f1a\u51fa\u73b0\u6743\u9650\u3001\u7f16\u7801\u3001\u903b\u8f91\u7b49\u5404\u79cd\u5404\u6837\u7684bug\uff0c\u7279\u522b\u662f\u6253\u5305\u597d\u7684pyth [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5112,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,4],"tags":[37,14],"_links":{"self":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/5106"}],"collection":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5106"}],"version-history":[{"count":0,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/5106\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/media\/5112"}],"wp:attachment":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}