{"id":932083,"date":"2024-12-26T17:51:26","date_gmt":"2024-12-26T09:51:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932083.html"},"modified":"2024-12-26T17:51:28","modified_gmt":"2024-12-26T09:51:28","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/932083.html","title":{"rendered":"python\u5982\u4f55\u53d6\u65e5\u5fd7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070033\/3e043507-130e-4498-ba53-c8e0dfb4b4c0.webp\" alt=\"python\u5982\u4f55\u53d6\u65e5\u5fd7\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53d6\u65e5\u5fd7\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528logging\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7\u5b9a\u5236\u5316\u5b9e\u73b0\u65e5\u5fd7\u7ba1\u7406\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u5185\u7f6e\u7684logging\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u3001\u6613\u4e8e\u4f7f\u7528\u4e14\u53ef\u6269\u5c55\u3002\u901a\u8fc7logging\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u65e5\u5fd7\u3001\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u3001\u914d\u7f6e\u65e5\u5fd7\u683c\u5f0f\u548c\u8f93\u51fa\u76ee\u6807\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528logging\u6a21\u5757\u8fdb\u884c\u65e5\u5fd7\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001LOGGING\u6a21\u5757\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>Python\u7684logging\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u65e5\u5fd7\u7ba1\u7406\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\u3002\u901a\u8fc7\u8be5\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u65e5\u5fd7\u8bed\u53e5\uff0c\u4ee5\u4fbf\u5728\u8fd0\u884c\u65f6\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u548c\u95ee\u9898\u3002logging\u6a21\u5757\u652f\u6301\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\uff08\u5982DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\uff09\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u4e0d\u540c\u7684\u76ee\u6807\uff08\u5982\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><h4>1. \u65e5\u5fd7\u7ea7\u522b<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u7ea7\u522b\u7528\u4e8e\u6307\u793a\u65e5\u5fd7\u7684\u4e25\u91cd\u6027\uff0c\u5e38\u7528\u7684\u65e5\u5fd7\u7ea7\u522b\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>DEBUG<\/strong>\uff1a\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u901a\u5e38\u53ea\u5728\u8bca\u65ad\u95ee\u9898\u65f6\u4f7f\u7528\u3002<\/li>\n<li><strong>INFO<\/strong>\uff1a\u786e\u8ba4\u4e00\u5207\u6b63\u5e38\u7684\u4fe1\u606f\u3002<\/li>\n<li><strong>WARNING<\/strong>\uff1a\u8868\u793a\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\u6216\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/li>\n<li><strong>ERROR<\/strong>\uff1a\u7531\u4e8e\u66f4\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u7a0b\u5e8f\u65e0\u6cd5\u6267\u884c\u67d0\u4e9b\u529f\u80fd\u3002<\/li>\n<li><strong>CRITICAL<\/strong>\uff1a\u4e25\u91cd\u9519\u8bef\uff0c\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p><h4>2. \u65e5\u5fd7\u5904\u7406\u5668<\/h4>\n<\/p>\n<p><p>logging\u6a21\u5757\u901a\u8fc7\u5904\u7406\u5668\uff08Handler\uff09\u5c06\u65e5\u5fd7\u8bb0\u5f55\u53d1\u9001\u5230\u4e0d\u540c\u7684\u8f93\u51fa\u76ee\u6807\u3002\u5e38\u89c1\u7684\u5904\u7406\u5668\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>StreamHandler<\/strong>\uff1a\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/li>\n<li><strong>FileHandler<\/strong>\uff1a\u5c06\u65e5\u5fd7\u8bb0\u5f55\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<li><strong>NullHandler<\/strong>\uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h4>3. \u65e5\u5fd7\u683c\u5f0f\u5316\u5668<\/h4>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5668\uff08Formatter\uff09\u7528\u4e8e\u5b9a\u4e49\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u65f6\u95f4\u3001\u65e5\u5fd7\u7ea7\u522b\u3001\u6d88\u606f\u7b49\u5185\u5bb9\u6765\u5b9a\u5236\u65e5\u5fd7\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528LOGGING\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7<\/h3>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528logging\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u914d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5fd7\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u683c\u5f0f\u548c\u65e5\u5fd7\u7ea7\u522b<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG,<\/p>\n<p>                    format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>basicConfig<\/code>\u51fd\u6570\u914d\u7f6e\u4e86\u65e5\u5fd7\u683c\u5f0f\u548c\u65e5\u5fd7\u7ea7\u522b\uff0c\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>2. \u8bb0\u5f55\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u914d\u7f6e\u597d\u65e5\u5fd7\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u65b9\u6cd5\u8bb0\u5f55\u65e5\u5fd7\u4e86\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">logger.debug(&#39;\u8fd9\u662f\u4e00\u4e2a\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.info(&#39;\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u4fe1\u606f&#39;)<\/p>\n<p>logger.warning(&#39;\u8fd9\u662f\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p>logger.critical(&#39;\u8fd9\u662f\u4e00\u4e2a\u4e25\u91cd\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u6839\u636e\u914d\u7f6e\u7684\u65e5\u5fd7\u7ea7\u522b\u8f93\u51fa\u5bf9\u5e94\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5c06\u65e5\u5fd7\u8bb0\u5f55\u5230\u6587\u4ef6\u4e2d\u901a\u5e38\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff0c\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u4fdd\u7559\u5386\u53f2\u65e5\u5fd7\u8bb0\u5f55\u4ee5\u4f9b\u65e5\u540e\u5206\u6790\u3002\u8981\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>FileHandler<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;file_logger&#39;)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>file_handler = logging.FileHandler(&#39;app.log&#39;)<\/p>\n<p>file_handler.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65e5\u5fd7\u683c\u5f0f\u5316\u5668<\/strong><\/h2>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>file_handler.setFormatter(formatter)<\/p>\n<h2><strong>\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger.addHandler(file_handler)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u4e2a\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.info(&#39;\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u4fe1\u606f&#39;)<\/p>\n<p>logger.warning(&#39;\u8fd9\u662f\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p>logger.critical(&#39;\u8fd9\u662f\u4e00\u4e2a\u4e25\u91cd\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>FileHandler<\/code>\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668\u4e2d\u3002\u8fd9\u6837\uff0c\u6240\u6709\u65e5\u5fd7\u4fe1\u606f\u5c06\u88ab\u5199\u5165\u5230<code>app.log<\/code>\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u65e5\u5fd7\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5728\u4ee3\u7801\u4e2d\u8fdb\u884c\u65e5\u5fd7\u914d\u7f6e\u5916\uff0clogging\u6a21\u5757\u8fd8\u652f\u6301\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u914d\u7f6e\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5927\u578b\u9879\u76ee\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5c06\u65e5\u5fd7\u914d\u7f6e\u4e0e\u4ee3\u7801\u5206\u79bb\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u65e5\u5fd7\u914d\u7f6e\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[loggers]<\/p>\n<p>keys=root<\/p>\n<p>[handlers]<\/p>\n<p>keys=consoleHandler,fileHandler<\/p>\n<p>[formatters]<\/p>\n<p>keys=formatter<\/p>\n<p>[logger_root]<\/p>\n<p>level=DEBUG<\/p>\n<p>handlers=consoleHandler,fileHandler<\/p>\n<p>[handler_consoleHandler]<\/p>\n<p>class=StreamHandler<\/p>\n<p>level=DEBUG<\/p>\n<p>formatter=formatter<\/p>\n<p>args=(sys.stdout,)<\/p>\n<p>[handler_fileHandler]<\/p>\n<p>class=FileHandler<\/p>\n<p>level=DEBUG<\/p>\n<p>formatter=formatter<\/p>\n<p>args=(&#39;app.log&#39;, &#39;a&#39;)<\/p>\n<p>[formatter_formatter]<\/p>\n<p>format=%(asctime)s - %(name)s - %(levelname)s - %(message)s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>fileConfig<\/code>\u51fd\u6570\u52a0\u8f7d\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import logging.config<\/p>\n<p>logging.config.fileConfig(&#39;logging.conf&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(__name__)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u4e2a\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.info(&#39;\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u4fe1\u606f&#39;)<\/p>\n<p>logger.warning(&#39;\u8fd9\u662f\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p>logger.critical(&#39;\u8fd9\u662f\u4e00\u4e2a\u4e25\u91cd\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86logging\u6a21\u5757\uff0cPython\u8fd8\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5e93\uff0c\u5982<code>loguru<\/code>\u3001<code>structlog<\/code>\u7b49\u3002\u8fd9\u4e9b\u5e93\u901a\u5e38\u63d0\u4f9b\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u548c\u66f4\u7b80\u6d01\u7684API\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528LOGURU\u5e93<\/h4>\n<\/p>\n<p><p><code>loguru<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u65e5\u5fd7\u63a5\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from loguru import logger<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>logger.add(&#39;app.log&#39;, level=&#39;DEBUG&#39;, format=&#39;{time} - {name} - {level} - {message}&#39;)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logger.debug(&#39;\u8fd9\u662f\u4e00\u4e2a\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logger.info(&#39;\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u4fe1\u606f&#39;)<\/p>\n<p>logger.warning(&#39;\u8fd9\u662f\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f&#39;)<\/p>\n<p>logger.error(&#39;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p>logger.critical(&#39;\u8fd9\u662f\u4e00\u4e2a\u4e25\u91cd\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>loguru<\/code>\u5141\u8bb8\u4f60\u8f7b\u677e\u5730\u914d\u7f6e\u65e5\u5fd7\u8f93\u51fa\u683c\u5f0f\u3001\u8f93\u51fa\u76ee\u6807\u7b49\uff0c\u5e76\u4e14\u652f\u6301\u5f02\u6b65\u65e5\u5fd7\u8bb0\u5f55\u548c\u65e5\u5fd7\u8f6e\u66ff\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9a\u5236\u5316\u65e5\u5fd7\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7279\u6b8a\u7684\u65e5\u5fd7\u9700\u6c42\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b9a\u5236\u5316\u65e5\u5fd7\u7ba1\u7406\u65b9\u6848\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>logging.Handler<\/code>\u7c7b\u521b\u5efa\u81ea\u5b9a\u4e49\u5904\u7406\u5668\uff0c\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u65b9\u5f0f\u5b9e\u73b0\u5b9a\u5236\u5316\u7684\u65e5\u5fd7\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u81ea\u5b9a\u4e49\u5904\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>logging.Handler<\/code>\u7c7b\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u5904\u7406\u5668\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u5c06\u65e5\u5fd7\u53d1\u9001\u5230\u7f51\u7edc\u7684\u5904\u7406\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import socket<\/p>\n<p>class NetworkHandler(logging.Handler):<\/p>\n<p>    def __init__(self, host, port):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.host = host<\/p>\n<p>        self.port = port<\/p>\n<p>    def emit(self, record):<\/p>\n<p>        message = self.format(record)<\/p>\n<p>        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>        try:<\/p>\n<p>            sock.connect((self.host, self.port))<\/p>\n<p>            sock.sendall(message.encode(&#39;utf-8&#39;))<\/p>\n<p>        finally:<\/p>\n<p>            sock.close()<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u5904\u7406\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;network_logger&#39;)<\/p>\n<p>network_handler = NetworkHandler(&#39;localhost&#39;, 9999)<\/p>\n<p>logger.addHandler(network_handler)<\/p>\n<p>logger.info(&#39;\u8fd9\u662f\u4e00\u6761\u901a\u8fc7\u7f51\u7edc\u53d1\u9001\u7684\u65e5\u5fd7\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8bb0\u5f55\u65e5\u5fd7\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528\u5185\u7f6e\u7684logging\u6a21\u5757\u3002\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u65e5\u5fd7\uff0c\u4f60\u53ef\u4ee5\u6e05\u6670\u5730\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u5e2e\u52a9\u8bca\u65ad\u548c\u89e3\u51b3\u95ee\u9898\u3002\u5728\u590d\u6742\u9879\u76ee\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u65e5\u5fd7\u914d\u7f6e\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u65e5\u5fd7\u8bbe\u7f6e\u3002\u6b64\u5916\uff0c\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5e93\u5982<code>loguru<\/code>\u4e5f\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u65e5\u5fd7\u63a5\u53e3\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u89e3\u51b3\u65b9\u6848\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5185\u7f6e\u6a21\u5757\u8fd8\u662f\u7b2c\u4e09\u65b9\u5e93\uff0c\u826f\u597d\u7684\u65e5\u5fd7\u7ba1\u7406\u90fd\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u91cd\u8981\u56e0\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bb0\u5f55\u65e5\u5fd7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>logging<\/code>\u6a21\u5757\u6765\u8bb0\u5f55\u65e5\u5fd7\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\uff08\u5982DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\uff09\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7ea7\u522b\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u3002\u57fa\u672c\u7684\u4f7f\u7528\u65b9\u5f0f\u662f\u5bfc\u5165<code>logging<\/code>\u6a21\u5757\uff0c\u8bbe\u7f6e\u57fa\u672c\u914d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528<code>logging<\/code>\u7684\u51fd\u6570\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logging.basicConfig(level=logging.INFO)<\/code>\u6765\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3aINFO\uff0c\u5e76\u4f7f\u7528<code>logging.info(&#39;\u8fd9\u662f\u4e00\u4e2a\u4fe1\u606f\u65e5\u5fd7&#39;)<\/code>\u6765\u8bb0\u5f55\u4fe1\u606f\u65e5\u5fd7\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49Python\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\uff1f<\/strong><br \/>\u901a\u8fc7<code>logging<\/code>\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u81ea\u5b9a\u4e49\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4f7f\u7528<code>logging.basicConfig()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>format<\/code>\u53c2\u6570\u6307\u5b9a\u683c\u5f0f\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e<code>logging.basicConfig(format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/code>\uff0c\u53ef\u4ee5\u5c06\u65e5\u5fd7\u8f93\u51fa\u683c\u5f0f\u5316\u4e3a\u5305\u542b\u65f6\u95f4\u6233\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u6d88\u606f\u3002\u8fd9\u6837\uff0c\u8bb0\u5f55\u7684\u65e5\u5fd7\u4fe1\u606f\u5c06\u66f4\u52a0\u6e05\u6670\u6613\u8bfb\u3002<\/p>\n<p><strong>Python\u65e5\u5fd7\u8bb0\u5f55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u8bb0\u5f55\u65e5\u5fd7\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u5347\u65e5\u5fd7\u7684\u6709\u6548\u6027\u3002\u9996\u5148\uff0c\u5e94\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u786e\u4fdd\u91cd\u8981\u4fe1\u606f\u4e0d\u4f1a\u88ab\u5ffd\u7565\u3002\u5176\u6b21\uff0c\u8bb0\u5f55\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4f8b\u5982\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u6216\u5173\u952e\u53d8\u91cf\uff0c\u4ee5\u4fbf\u5728\u8c03\u8bd5\u65f6\u63d0\u4f9b\u66f4\u591a\u80cc\u666f\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u8f6e\u6362\u548c\u5f52\u6863\u65e5\u5fd7\u6587\u4ef6\u4e5f\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u4ee5\u9632\u6b62\u65e5\u5fd7\u6587\u4ef6\u8fc7\u5927\u800c\u5f71\u54cd\u6027\u80fd\u3002\u786e\u4fdd\u65e5\u5fd7\u6587\u4ef6\u7684\u5b58\u50a8\u4f4d\u7f6e\u5b89\u5168\u4e14\u6613\u4e8e\u8bbf\u95ee\uff0c\u4ee5\u65b9\u4fbf\u540e\u7eed\u7684\u5206\u6790\u548c\u6392\u67e5\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53d6\u65e5\u5fd7\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528logging\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u65e5\u5fd7\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7\u5b9a\u5236\u5316\u5b9e\u73b0\u65e5\u5fd7\u7ba1\u7406\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":932090,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932083"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=932083"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932083\/revisions"}],"predecessor-version":[{"id":932091,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932083\/revisions\/932091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932090"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}