{"id":1176707,"date":"2025-01-15T17:48:21","date_gmt":"2025-01-15T09:48:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176707.html"},"modified":"2025-01-15T17:48:23","modified_gmt":"2025-01-15T09:48:23","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e6%97%a5%e5%bf%97%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176707.html","title":{"rendered":"python\u5982\u4f55\u505a\u65e5\u5fd7\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111816\/d39afeb4-c0a3-418f-9e1c-721c2a6af40a.webp\" alt=\"python\u5982\u4f55\u505a\u65e5\u5fd7\u7a0b\u5e8f\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e5\u5fd7\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684<code>logging<\/code>\u6a21\u5757\u3002<strong>Python\u7684<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u8bb0\u5f55\u65e5\u5fd7\u7684\u529f\u80fd\u3001\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u3001\u53ef\u4ee5\u914d\u7f6e\u65e5\u5fd7\u8f93\u51fa\u7684\u683c\u5f0f\u548c\u76ee\u6807<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u6765\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u7684\u65e5\u5fd7\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165logging\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>logging<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u57fa\u672c\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528<code>basicConfig<\/code>\u51fd\u6570\u6765\u914d\u7f6e\u65e5\u5fd7\u7cfb\u7edf\u3002\u8fd9\u5305\u62ec\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u3001\u65e5\u5fd7\u683c\u5f0f\u548c\u65e5\u5fd7\u8f93\u51fa\u76ee\u6807\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.DEBUG, <\/p>\n<p>                    format=&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;, <\/p>\n<p>                    datefmt=&#39;%Y-%m-%d %H:%M:%S&#39;,<\/p>\n<p>                    handlers=[logging.FileHandler(&quot;app.log&quot;), logging.StreamHandler()])<\/p>\n<p>logging.debug(&#39;This is a debug message&#39;)<\/p>\n<p>logging.info(&#39;This is an info message&#39;)<\/p>\n<p>logging.warning(&#39;This is a warning message&#39;)<\/p>\n<p>logging.error(&#39;This is an error message&#39;)<\/p>\n<p>logging.critical(&#39;This is a critical message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>basicConfig<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>level<\/code>\u53c2\u6570\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3a<code>DEBUG<\/code>\uff0c\u8868\u793a\u8bb0\u5f55\u6240\u6709\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/li>\n<li><code>format<\/code>\u53c2\u6570\u8bbe\u7f6e\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u5305\u62ec\u65f6\u95f4\u3001\u65e5\u5fd7\u5668\u540d\u79f0\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u6d88\u606f\u3002<\/li>\n<li><code>datefmt<\/code>\u53c2\u6570\u8bbe\u7f6e\u65f6\u95f4\u683c\u5f0f\u3002<\/li>\n<li><code>handlers<\/code>\u53c2\u6570\u8bbe\u7f6e\u65e5\u5fd7\u8f93\u51fa\u76ee\u6807\uff0c\u5305\u62ec\u6587\u4ef6\u548c\u63a7\u5236\u53f0\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u65e5\u5fd7\u7ea7\u522b<\/h3>\n<\/p>\n<p><p><code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u65e5\u5fd7\u7ea7\u522b\uff0c\u7528\u4e8e\u8868\u793a\u65e5\u5fd7\u7684\u4e25\u91cd\u7a0b\u5ea6\u3002\u65e5\u5fd7\u7ea7\u522b\u4ece\u4f4e\u5230\u9ad8\u4f9d\u6b21\u4e3a\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>DEBUG<\/code>\uff1a\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u901a\u5e38\u53ea\u5728\u8bca\u65ad\u95ee\u9898\u65f6\u4f7f\u7528\u3002<\/li>\n<li><code>INFO<\/code>\uff1a\u786e\u8ba4\u7a0b\u5e8f\u6309\u9884\u671f\u5de5\u4f5c\u7684\u4fe1\u606f\u3002<\/li>\n<li><code>WARNING<\/code>\uff1a\u8868\u793a\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u7684\u8b66\u544a\u4fe1\u606f\u3002<\/li>\n<li><code>ERROR<\/code>\uff1a\u7531\u4e8e\u66f4\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u7a0b\u5e8f\u5df2\u4e0d\u80fd\u6267\u884c\u67d0\u4e9b\u529f\u80fd\u3002<\/li>\n<li><code>CRITICAL<\/code>\uff1a\u4e25\u91cd\u9519\u8bef\uff0c\u8868\u793a\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u521b\u5efa\u65e5\u5fd7\u5668<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4e3a\u6bcf\u4e2a\u6a21\u5757\u6216\u7c7b\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u65e5\u5fd7\u5668\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u65e5\u5fd7\u3002\u53ef\u4ee5\u4f7f\u7528<code>logging.getLogger<\/code>\u51fd\u6570\u521b\u5efa\u65e5\u5fd7\u5668\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\u63a7\u5236\u53f0\u5904\u7406\u5668<\/strong><\/h2>\n<p>console_handler = logging.StreamHandler()<\/p>\n<p>console_handler.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u521b\u5efa\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\u683c\u5f0f\u5668\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5904\u7406\u5668<\/strong><\/h2>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>console_handler.setFormatter(formatter)<\/p>\n<p>file_handler.setFormatter(formatter)<\/p>\n<h2><strong>\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u5668<\/strong><\/h2>\n<p>logger.addHandler(console_handler)<\/p>\n<p>logger.addHandler(file_handler)<\/p>\n<p>logger.debug(&#39;This is a debug message&#39;)<\/p>\n<p>logger.info(&#39;This is an info message&#39;)<\/p>\n<p>logger.warning(&#39;This is a warning message&#39;)<\/p>\n<p>logger.error(&#39;This is an error message&#39;)<\/p>\n<p>logger.critical(&#39;This is a critical message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>logging.getLogger<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u65e5\u5fd7\u5668\u5bf9\u8c61\u3002<\/li>\n<li>\u521b\u5efa\u4e86\u4e24\u4e2a\u5904\u7406\u5668\uff0c\u5206\u522b\u7528\u4e8e\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u548c\u6587\u4ef6\u3002<\/li>\n<li>\u521b\u5efa\u4e86\u4e00\u4e2a\u683c\u5f0f\u5668\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5904\u7406\u5668\u3002<\/li>\n<li>\u5c06\u5904\u7406\u5668\u6dfb\u52a0\u5230\u65e5\u5fd7\u5668\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u65e5\u5fd7\u7684\u9ad8\u7ea7\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u590d\u6742\u7684\u65e5\u5fd7\u914d\u7f6e\uff0c\u4f8b\u5982\u4e3a\u4e0d\u540c\u6a21\u5757\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u4e3a\u4e0d\u540c\u7684\u5904\u7406\u5668\u8bbe\u7f6e\u4e0d\u540c\u7684\u683c\u5f0f\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6216\u5b57\u5178\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u65e5\u5fd7\u914d\u7f6e\u5199\u5165\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># logging.conf<\/p>\n<p>[loggers]<\/p>\n<p>keys=root,sampleLogger<\/p>\n<p>[handlers]<\/p>\n<p>keys=consoleHandler,fileHandler<\/p>\n<p>[formatters]<\/p>\n<p>keys=sampleFormatter<\/p>\n<p>[logger_root]<\/p>\n<p>level=DEBUG<\/p>\n<p>handlers=consoleHandler<\/p>\n<p>[logger_sampleLogger]<\/p>\n<p>level=DEBUG<\/p>\n<p>handlers=consoleHandler,fileHandler<\/p>\n<p>qualname=sampleLogger<\/p>\n<p>propagate=0<\/p>\n<p>[handler_consoleHandler]<\/p>\n<p>class=StreamHandler<\/p>\n<p>level=DEBUG<\/p>\n<p>formatter=sampleFormatter<\/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=sampleFormatter<\/p>\n<p>args=(&#39;app.log&#39;, &#39;a&#39;)<\/p>\n<p>[formatter_sampleFormatter]<\/p>\n<p>format=%(asctime)s - %(name)s - %(levelname)s - %(message)s<\/p>\n<p>datefmt=%Y-%m-%d %H:%M:%S<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u52a0\u8f7d\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<p>logger = logging.getLogger(&#39;sampleLogger&#39;)<\/p>\n<p>logger.debug(&#39;This is a debug message&#39;)<\/p>\n<p>logger.info(&#39;This is an info message&#39;)<\/p>\n<p>logger.warning(&#39;This is a warning message&#39;)<\/p>\n<p>logger.error(&#39;This is an error message&#39;)<\/p>\n<p>logger.critical(&#39;This is a critical message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b57\u5178\u914d\u7f6e<\/h4>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u914d\u7f6e\u65e5\u5fd7\u7cfb\u7edf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import logging.config<\/p>\n<p>log_config = {<\/p>\n<p>    &#39;version&#39;: 1,<\/p>\n<p>    &#39;disable_existing_loggers&#39;: False,<\/p>\n<p>    &#39;formatters&#39;: {<\/p>\n<p>        &#39;default&#39;: {<\/p>\n<p>            &#39;format&#39;: &#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;,<\/p>\n<p>            &#39;datefmt&#39;: &#39;%Y-%m-%d %H:%M:%S&#39;,<\/p>\n<p>        },<\/p>\n<p>    },<\/p>\n<p>    &#39;handlers&#39;: {<\/p>\n<p>        &#39;console&#39;: {<\/p>\n<p>            &#39;class&#39;: &#39;logging.StreamHandler&#39;,<\/p>\n<p>            &#39;level&#39;: &#39;DEBUG&#39;,<\/p>\n<p>            &#39;formatter&#39;: &#39;default&#39;,<\/p>\n<p>        },<\/p>\n<p>        &#39;file&#39;: {<\/p>\n<p>            &#39;class&#39;: &#39;logging.FileHandler&#39;,<\/p>\n<p>            &#39;level&#39;: &#39;DEBUG&#39;,<\/p>\n<p>            &#39;formatter&#39;: &#39;default&#39;,<\/p>\n<p>            &#39;filename&#39;: &#39;app.log&#39;,<\/p>\n<p>        },<\/p>\n<p>    },<\/p>\n<p>    &#39;loggers&#39;: {<\/p>\n<p>        &#39;&#39;: {<\/p>\n<p>            &#39;level&#39;: &#39;DEBUG&#39;,<\/p>\n<p>            &#39;handlers&#39;: [&#39;console&#39;, &#39;file&#39;],<\/p>\n<p>        },<\/p>\n<p>    },<\/p>\n<p>}<\/p>\n<p>logging.config.dictConfig(log_config)<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.debug(&#39;This is a debug message&#39;)<\/p>\n<p>logger.info(&#39;This is an info message&#39;)<\/p>\n<p>logger.warning(&#39;This is a warning message&#39;)<\/p>\n<p>logger.error(&#39;This is an error message&#39;)<\/p>\n<p>logger.critical(&#39;This is a critical message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6355\u83b7\u5f02\u5e38\u65e5\u5fd7<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6355\u83b7\u548c\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u7684<code>exception<\/code>\u65b9\u6cd5\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>try:<\/p>\n<p>    1 \/ 0<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    logger.exception(&#39;An exception occurred&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>logger.exception<\/code>\u65b9\u6cd5\u4f1a\u81ea\u52a8\u8bb0\u5f55\u5f02\u5e38\u7684\u5806\u6808\u4fe1\u606f\uff0c\u975e\u5e38\u9002\u5408\u5728\u6355\u83b7\u5f02\u5e38\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u65e5\u5fd7<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u65e5\u5fd7\u7684\u5904\u7406\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u7684<code>QueueHandler<\/code>\u548c<code>QueueListener<\/code>\u6765\u5904\u7406\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u591a\u8fdb\u7a0b\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>QueueHandler<\/code>\u548c<code>QueueListener<\/code>\u6765\u5b89\u5168\u5730\u8bb0\u5f55\u65e5\u5fd7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import logging.handlers<\/p>\n<p>import multiprocessing<\/p>\n<p>def logger_process(queue):<\/p>\n<p>    root = logging.getLogger()<\/p>\n<p>    h = logging.handlers.QueueHandler(queue)<\/p>\n<p>    root.addHandler(h)<\/p>\n<p>    root.setLevel(logging.DEBUG)<\/p>\n<p>    listener = logging.handlers.QueueListener(queue, *root.handlers)<\/p>\n<p>    listener.start()<\/p>\n<p>    try:<\/p>\n<p>        logger = logging.getLogger(&#39;logger_process&#39;)<\/p>\n<p>        logger.info(&#39;Logger process started&#39;)<\/p>\n<p>        while True:<\/p>\n<p>            pass<\/p>\n<p>    finally:<\/p>\n<p>        listener.stop()<\/p>\n<p>def worker_process(queue, worker_id):<\/p>\n<p>    h = logging.handlers.QueueHandler(queue)<\/p>\n<p>    root = logging.getLogger()<\/p>\n<p>    root.addHandler(h)<\/p>\n<p>    root.setLevel(logging.DEBUG)<\/p>\n<p>    logger = logging.getLogger(f&#39;worker_{worker_id}&#39;)<\/p>\n<p>    logger.info(f&#39;Worker {worker_id} started&#39;)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    queue = multiprocessing.Queue(-1)<\/p>\n<p>    logger_p = multiprocessing.Process(target=logger_process, args=(queue,))<\/p>\n<p>    logger_p.start()<\/p>\n<p>    workers = [multiprocessing.Process(target=worker_process, args=(queue, i)) for i in range(4)]<\/p>\n<p>    for w in workers:<\/p>\n<p>        w.start()<\/p>\n<p>    for w in workers:<\/p>\n<p>        w.join()<\/p>\n<p>    logger_p.terminate()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>logger_process<\/code>\u51fd\u6570\u8d1f\u8d23\u542f\u52a8\u65e5\u5fd7\u8bb0\u5f55\u8fdb\u7a0b\uff0c<code>worker_process<\/code>\u51fd\u6570\u8d1f\u8d23\u5de5\u4f5c\u8fdb\u7a0b\u3002\u65e5\u5fd7\u8bb0\u5f55\u8fdb\u7a0b\u4f7f\u7528<code>QueueListener<\/code>\u6765\u76d1\u542c\u65e5\u5fd7\u6d88\u606f\u961f\u5217\uff0c\u5de5\u4f5c\u8fdb\u7a0b\u4f7f\u7528<code>QueueHandler<\/code>\u5c06\u65e5\u5fd7\u6d88\u606f\u53d1\u9001\u5230\u6d88\u606f\u961f\u5217\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u591a\u7ebf\u7a0b\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>QueueHandler<\/code>\u548c<code>QueueListener<\/code>\u6765\u5b89\u5168\u5730\u8bb0\u5f55\u65e5\u5fd7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import logging.handlers<\/p>\n<p>import threading<\/p>\n<p>import queue<\/p>\n<p>def logger_thread(log_queue):<\/p>\n<p>    root = logging.getLogger()<\/p>\n<p>    h = logging.handlers.QueueHandler(log_queue)<\/p>\n<p>    root.addHandler(h)<\/p>\n<p>    root.setLevel(logging.DEBUG)<\/p>\n<p>    listener = logging.handlers.QueueListener(log_queue, *root.handlers)<\/p>\n<p>    listener.start()<\/p>\n<p>    try:<\/p>\n<p>        logger = logging.getLogger(&#39;logger_thread&#39;)<\/p>\n<p>        logger.info(&#39;Logger thread started&#39;)<\/p>\n<p>        while True:<\/p>\n<p>            pass<\/p>\n<p>    finally:<\/p>\n<p>        listener.stop()<\/p>\n<p>def worker_thread(log_queue, worker_id):<\/p>\n<p>    h = logging.handlers.QueueHandler(log_queue)<\/p>\n<p>    root = logging.getLogger()<\/p>\n<p>    root.addHandler(h)<\/p>\n<p>    root.setLevel(logging.DEBUG)<\/p>\n<p>    logger = logging.getLogger(f&#39;worker_{worker_id}&#39;)<\/p>\n<p>    logger.info(f&#39;Worker {worker_id} started&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    log_queue = queue.Queue(-1)<\/p>\n<p>    logger_t = threading.Thread(target=logger_thread, args=(log_queue,))<\/p>\n<p>    logger_t.start()<\/p>\n<p>    workers = [threading.Thread(target=worker_thread, args=(log_queue, i)) for i in range(4)]<\/p>\n<p>    for w in workers:<\/p>\n<p>        w.start()<\/p>\n<p>    for w in workers:<\/p>\n<p>        w.join()<\/p>\n<p>    logger_t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>logger_thread<\/code>\u51fd\u6570\u8d1f\u8d23\u542f\u52a8\u65e5\u5fd7\u8bb0\u5f55\u7ebf\u7a0b\uff0c<code>worker_thread<\/code>\u51fd\u6570\u8d1f\u8d23\u5de5\u4f5c\u7ebf\u7a0b\u3002\u65e5\u5fd7\u8bb0\u5f55\u7ebf\u7a0b\u4f7f\u7528<code>QueueListener<\/code>\u6765\u76d1\u542c\u65e5\u5fd7\u6d88\u606f\u961f\u5217\uff0c\u5de5\u4f5c\u7ebf\u7a0b\u4f7f\u7528<code>QueueHandler<\/code>\u5c06\u65e5\u5fd7\u6d88\u606f\u53d1\u9001\u5230\u6d88\u606f\u961f\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u5982\u4f55\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u7684\u65e5\u5fd7\u7a0b\u5e8f\u3002<strong>Python\u7684<code>logging<\/code>\u6a21\u5757\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u65e5\u5fd7\u8bb0\u5f55\u7684\u9700\u6c42<\/strong>\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u914d\u7f6e\u548c\u6269\u5c55\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u65e5\u5fd7\u529f\u80fd\u3002\u901a\u8fc7\u5408\u7406\u5730\u8bb0\u5f55\u65e5\u5fd7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u8bca\u65ad\u548c\u8c03\u8bd5\u95ee\u9898\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5fd7\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>logging<\/code>\u6a21\u5757\u6765\u521b\u5efa\u65e5\u5fd7\u7a0b\u5e8f\u3002\u9996\u5148\uff0c\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>basicConfig<\/code>\u65b9\u6cd5\u914d\u7f6e\u65e5\u5fd7\u7684\u7ea7\u522b\u3001\u683c\u5f0f\u548c\u6587\u4ef6\u540d\u7b49\u3002\u63a5\u7740\uff0c\u5229\u7528\u4e0d\u540c\u7684\u65e5\u5fd7\u65b9\u6cd5\uff08\u5982<code>debug()<\/code>, <code>info()<\/code>, <code>warning()<\/code>, <code>error()<\/code>, <code>exception()<\/code>, <code>critical()<\/code>\uff09\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ffd\u8e2a\u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\u548c\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>Python\u65e5\u5fd7\u7a0b\u5e8f\u53ef\u4ee5\u8bb0\u5f55\u54ea\u4e9b\u7c7b\u578b\u7684\u4fe1\u606f\uff1f<\/strong><br \/>Python\u7684\u65e5\u5fd7\u7a0b\u5e8f\u53ef\u4ee5\u8bb0\u5f55\u591a\u79cd\u7c7b\u578b\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u8c03\u8bd5\u4fe1\u606f\u3001\u8fd0\u884c\u65f6\u4fe1\u606f\u3001\u8b66\u544a\u3001\u9519\u8bef\u548c\u4e25\u91cd\u9519\u8bef\u7b49\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u53ea\u8bb0\u5f55\u91cd\u8981\u7684\u4fe1\u606f\uff0c\u6216\u662f\u8bb0\u5f55\u66f4\u8be6\u7ec6\u7684\u8c03\u8bd5\u4fe1\u606f\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u65e5\u5fd7\u7a0b\u5e8f\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u800c\u4e0d\u662f\u63a7\u5236\u53f0\uff1f<\/strong><br \/>\u4e3a\u4e86\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728<code>logging.basicConfig<\/code>\u4e2d\u8bbe\u7f6e<code>filename<\/code>\u53c2\u6570\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7684\u65e5\u5fd7\u4fe1\u606f\u90fd\u4f1a\u88ab\u5199\u5165\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u663e\u793a\u5728\u63a7\u5236\u53f0\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u6587\u4ef6\u7684\u6a21\u5f0f\uff08\u5982\u8ffd\u52a0\u6a21\u5f0f\u6216\u8986\u76d6\u6a21\u5f0f\uff09\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u65e5\u5fd7\u8bb0\u5f55\u3002\u8fd9\u6837\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6240\u6709\u7684\u65e5\u5fd7\u4fe1\u606f\u90fd\u4f1a\u88ab\u4fdd\u5b58\u4e0b\u6765\uff0c\u65b9\u4fbf\u540e\u7eed\u67e5\u770b\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e5\u5fd7\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684logging\u6a21\u5757\u3002Python\u7684loggi [&hellip;]","protected":false},"author":3,"featured_media":1176711,"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\/1176707"}],"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=1176707"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176707\/revisions"}],"predecessor-version":[{"id":1176714,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176707\/revisions\/1176714"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1176711"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}