{"id":1180539,"date":"2025-01-15T18:38:05","date_gmt":"2025-01-15T10:38:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180539.html"},"modified":"2025-01-15T18:38:09","modified_gmt":"2025-01-15T10:38:09","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0log%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1180539.html","title":{"rendered":"python\u5982\u4f55\u6253\u5370log\u6587\u4ef6\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25125224\/e6a442cb-b601-4324-b695-7fb1ee8a1802.webp\" alt=\"python\u5982\u4f55\u6253\u5370log\u6587\u4ef6\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6253\u5370\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u3001<code>open<\/code>\u51fd\u6570\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>loguru<\/code>\uff0c\u4ee5\u53ca\u901a\u8fc7\u81ea\u5b9a\u4e49\u65e5\u5fd7\u5904\u7406\u51fd\u6570\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u7684\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u548c\u6253\u5370\u65e5\u5fd7<\/h3>\n<\/p>\n<p><p><code>logging<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u65e5\u5fd7\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u548c\u7ba1\u7406\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u914d\u7f6e\u57fa\u672c\u65e5\u5fd7\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u914d\u7f6e\u57fa\u672c\u7684\u65e5\u5fd7\u8bbe\u7f6e\uff0c\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u8def\u5f84\u548c\u65e5\u5fd7\u7ea7\u522b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, <\/p>\n<p>                    level=logging.DEBUG, <\/p>\n<p>                    format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\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><h4>2\u3001\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9\u5e76\u6253\u5370<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>open<\/code>\u51fd\u6570\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;app.log&#39;, &#39;r&#39;) as log_file:<\/p>\n<p>    log_content = log_file.read()<\/p>\n<h2><strong>\u6253\u5370\u65e5\u5fd7\u5185\u5bb9<\/strong><\/h2>\n<p>print(log_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>loguru<\/code>\u5e93\u8bb0\u5f55\u548c\u6253\u5370\u65e5\u5fd7<\/h3>\n<\/p>\n<p><p><code>loguru<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u7684\u65e5\u5fd7\u5e93\uff0c\u76f8\u6bd4<code>logging<\/code>\u6a21\u5757\uff0c<code>loguru<\/code>\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684API\u548c\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5<code>loguru<\/code>\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install loguru<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bb0\u5f55\u548c\u6253\u5370\u65e5\u5fd7<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from loguru import logger<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u6587\u4ef6<\/strong><\/h2>\n<p>logger.add(&#39;app.log&#39;, format=&quot;{time} {level} {message}&quot;, level=&quot;DEBUG&quot;)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\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<h2><strong>\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;app.log&#39;, &#39;r&#39;) as log_file:<\/p>\n<p>    log_content = log_file.read()<\/p>\n<h2><strong>\u6253\u5370\u65e5\u5fd7\u5185\u5bb9<\/strong><\/h2>\n<p>print(log_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u81ea\u5b9a\u4e49\u65e5\u5fd7\u5904\u7406\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u65e5\u5fd7\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u548c\u6253\u5370\u65e5\u5fd7\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u65e5\u5fd7\u5904\u7406\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>class CustomHandler(logging.Handler):<\/p>\n<p>    def __init__(self, filename):<\/p>\n<p>        logging.Handler.__init__(self)<\/p>\n<p>        self.filename = filename<\/p>\n<p>    def emit(self, record):<\/p>\n<p>        log_entry = self.format(record)<\/p>\n<p>        with open(self.filename, &#39;a&#39;) as log_file:<\/p>\n<p>            log_file.write(log_entry + &#39;\\n&#39;)<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7<\/strong><\/h2>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<p>handler = CustomHandler(&#39;custom_app.log&#39;)<\/p>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>handler.setFormatter(formatter)<\/p>\n<p>logger.addHandler(handler)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\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\u8bfb\u53d6\u548c\u6253\u5370\u65e5\u5fd7\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;custom_app.log&#39;, &#39;r&#39;) as log_file:<\/p>\n<p>    log_content = log_file.read()<\/p>\n<h2><strong>\u6253\u5370\u65e5\u5fd7\u5185\u5bb9<\/strong><\/h2>\n<p>print(log_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u6216<code>loguru<\/code>\u5e93\u65f6\uff0c\u4e86\u89e3\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u65e5\u5fd7\u7ea7\u522b<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u7ea7\u522b\u7528\u4e8e\u6307\u5b9a\u65e5\u5fd7\u7684\u4e25\u91cd\u6027\uff0c\u4ece\u4f4e\u5230\u9ad8\u5206\u522b\u662f\uff1a<\/p>\n<\/p>\n<ul>\n<li>DEBUG: \u8c03\u8bd5\u4fe1\u606f<\/li>\n<li>INFO: \u666e\u901a\u4fe1\u606f<\/li>\n<li>WARNING: \u8b66\u544a\u4fe1\u606f<\/li>\n<li>ERROR: \u9519\u8bef\u4fe1\u606f<\/li>\n<li>CRITICAL: \u4e25\u91cd\u9519\u8bef\u4fe1\u606f<\/li>\n<\/ul>\n<p><h4>2\u3001\u65e5\u5fd7\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u683c\u5f0f\u6307\u5b9a\u4e86\u65e5\u5fd7\u8bb0\u5f55\u7684\u7ed3\u6784\u548c\u5185\u5bb9\uff0c\u53ef\u4ee5\u5305\u542b\u65f6\u95f4\u3001\u65e5\u5fd7\u7ea7\u522b\u3001\u6d88\u606f\u7b49\u4fe1\u606f\u3002\u5e38\u89c1\u7684\u65e5\u5fd7\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>%(asctime)s<\/code>: \u8bb0\u5f55\u7684\u65f6\u95f4<\/li>\n<li><code>%(levelname)s<\/code>: \u65e5\u5fd7\u7ea7\u522b<\/li>\n<li><code>%(message)s<\/code>: \u65e5\u5fd7\u6d88\u606f<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u65e5\u5fd7\u8f6e\u8f6c<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u957f\u671f\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u65e5\u5fd7\u6587\u4ef6\u53ef\u80fd\u4f1a\u53d8\u5f97\u975e\u5e38\u5927\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u65e5\u5fd7\u8f6e\u8f6c\u529f\u80fd\uff0c\u5c06\u65e5\u5fd7\u6587\u4ef6\u6309\u5927\u5c0f\u6216\u65f6\u95f4\u8fdb\u884c\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6309\u5927\u5c0f\u8f6e\u8f6c\u65e5\u5fd7<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>from logging.handlers import RotatingFileHandler<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8f6e\u8f6c<\/strong><\/h2>\n<p>handler = RotatingFileHandler(&#39;app.log&#39;, maxBytes=2000, backupCount=5)<\/p>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>handler.setFormatter(formatter)<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<p>logger.addHandler(handler)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>for i in range(100):<\/p>\n<p>    logger.debug(f&#39;This is debug message {i}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6309\u65f6\u95f4\u8f6e\u8f6c\u65e5\u5fd7<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>from logging.handlers import TimedRotatingFileHandler<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8f6e\u8f6c<\/strong><\/h2>\n<p>handler = TimedRotatingFileHandler(&#39;app.log&#39;, when=&#39;midnight&#39;, interval=1, backupCount=7)<\/p>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>handler.setFormatter(formatter)<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<p>logger.addHandler(handler)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>for i in range(100):<\/p>\n<p>    logger.debug(f&#39;This is debug message {i}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u548c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u540c\u65f6\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u548c\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u591a\u4e2a\u65e5\u5fd7\u5904\u7406\u5668\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u6587\u4ef6\u65e5\u5fd7\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<p>file_formatter = logging.Formatter(&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>file_handler.setFormatter(file_formatter)<\/p>\n<h2><strong>\u914d\u7f6e\u63a7\u5236\u53f0\u65e5\u5fd7\u5904\u7406\u5668<\/strong><\/h2>\n<p>console_handler = logging.StreamHandler()<\/p>\n<p>console_handler.setLevel(logging.DEBUG)<\/p>\n<p>console_formatter = logging.Formatter(&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>console_handler.setFormatter(console_formatter)<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7<\/strong><\/h2>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<p>logger.addHandler(file_handler)<\/p>\n<p>logger.addHandler(console_handler)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\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>\u4e03\u3001\u65e5\u5fd7\u7684\u9ad8\u7ea7\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6216\u5b57\u5178\u914d\u7f6e\u65e5\u5fd7\uff0c\u4ee5\u4fbf\u66f4\u7075\u6d3b\u5730\u7ba1\u7406\u65e5\u5fd7\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u914d\u7f6e\u65e5\u5fd7<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging.config<\/p>\n<h2><strong>\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>config = &quot;&quot;&quot;<\/p>\n<p>[loggers]<\/p>\n<p>keys=root<\/p>\n<p>[handlers]<\/p>\n<p>keys=fileHandler,consoleHandler<\/p>\n<p>[formatters]<\/p>\n<p>keys=simpleFormatter<\/p>\n<p>[logger_root]<\/p>\n<p>level=DEBUG<\/p>\n<p>handlers=fileHandler,consoleHandler<\/p>\n<p>[handler_fileHandler]<\/p>\n<p>class=FileHandler<\/p>\n<p>level=DEBUG<\/p>\n<p>formatter=simpleFormatter<\/p>\n<p>args=(&#39;app.log&#39;, &#39;a&#39;)<\/p>\n<p>[handler_consoleHandler]<\/p>\n<p>class=StreamHandler<\/p>\n<p>level=DEBUG<\/p>\n<p>formatter=simpleFormatter<\/p>\n<p>args=(sys.stdout,)<\/p>\n<p>[formatter_simpleFormatter]<\/p>\n<p>format=%(asctime)s - %(levelname)s - %(message)s<\/p>\n<p>datefmt=<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<h2><strong>\u5199\u5165\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;logging.conf&#39;, &#39;w&#39;) as config_file:<\/p>\n<p>    config_file.write(config)<\/p>\n<h2><strong>\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>logging.config.fileConfig(&#39;logging.conf&#39;)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logger = logging.getLogger()<\/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\u65e5\u5fd7<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging.config<\/p>\n<h2><strong>\u914d\u7f6e\u5b57\u5178<\/strong><\/h2>\n<p>config = {<\/p>\n<p>    &#39;version&#39;: 1,<\/p>\n<p>    &#39;formatters&#39;: {<\/p>\n<p>        &#39;simple&#39;: {<\/p>\n<p>            &#39;format&#39;: &#39;%(asctime)s - %(levelname)s - %(message)s&#39;<\/p>\n<p>        },<\/p>\n<p>    },<\/p>\n<p>    &#39;handlers&#39;: {<\/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;simple&#39;,<\/p>\n<p>            &#39;filename&#39;: &#39;app.log&#39;,<\/p>\n<p>        },<\/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;simple&#39;,<\/p>\n<p>        },<\/p>\n<p>    },<\/p>\n<p>    &#39;root&#39;: {<\/p>\n<p>        &#39;level&#39;: &#39;DEBUG&#39;,<\/p>\n<p>        &#39;handlers&#39;: [&#39;file&#39;, &#39;console&#39;]<\/p>\n<p>    },<\/p>\n<p>}<\/p>\n<h2><strong>\u52a0\u8f7d\u914d\u7f6e\u5b57\u5178<\/strong><\/h2>\n<p>logging.config.dictConfig(config)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logger = logging.getLogger()<\/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>\u516b\u3001\u65e5\u5fd7\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u9ad8\u6027\u80fd\u5e94\u7528\u4e2d\uff0c\u65e5\u5fd7\u8bb0\u5f55\u53ef\u80fd\u6210\u4e3a\u6027\u80fd\u74f6\u9888\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u4f18\u5316\u65e5\u5fd7\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5f02\u6b65\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<code>concurrent-log-handler<\/code>\u6216<code>logging.handlers.QueueHandler<\/code>\u5b9e\u73b0\u5f02\u6b65\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>from logging.handlers import QueueHandler, QueueListener<\/p>\n<p>import queue<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u961f\u5217<\/strong><\/h2>\n<p>log_queue = queue.Queue()<\/p>\n<h2><strong>\u914d\u7f6e\u6587\u4ef6\u65e5\u5fd7\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<p>file_formatter = logging.Formatter(&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>file_handler.setFormatter(file_formatter)<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u5904\u7406\u5668<\/strong><\/h2>\n<p>queue_handler = QueueHandler(log_queue)<\/p>\n<p>queue_listener = QueueListener(log_queue, file_handler)<\/p>\n<p>queue_listener.start()<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7<\/strong><\/h2>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<p>logger.addHandler(queue_handler)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\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<h2><strong>\u505c\u6b62\u961f\u5217\u76d1\u542c<\/strong><\/h2>\n<p>queue_listener.stop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6279\u91cf\u5199\u5165\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6279\u91cf\u5199\u5165\u65e5\u5fd7\uff0c\u51cf\u5c11\u78c1\u76d8I\/O\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>class BatchHandler(logging.Handler):<\/p>\n<p>    def __init__(self, filename, batch_size=10):<\/p>\n<p>        logging.Handler.__init__(self)<\/p>\n<p>        self.filename = filename<\/p>\n<p>        self.batch_size = batch_size<\/p>\n<p>        self.buffer = []<\/p>\n<p>    def emit(self, record):<\/p>\n<p>        log_entry = self.format(record)<\/p>\n<p>        self.buffer.append(log_entry)<\/p>\n<p>        if len(self.buffer) &gt;= self.batch_size:<\/p>\n<p>            self.flush()<\/p>\n<p>    def flush(self):<\/p>\n<p>        with open(self.filename, &#39;a&#39;) as log_file:<\/p>\n<p>            for entry in self.buffer:<\/p>\n<p>                log_file.write(entry + &#39;\\n&#39;)<\/p>\n<p>        self.buffer = []<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7<\/strong><\/h2>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<p>batch_handler = BatchHandler(&#39;batch_app.log&#39;, batch_size=10)<\/p>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>batch_handler.setFormatter(formatter)<\/p>\n<p>logger.addHandler(batch_handler)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>for i in range(100):<\/p>\n<p>    logger.debug(f&#39;This is debug message {i}&#39;)<\/p>\n<h2><strong>\u624b\u52a8\u5237\u65b0\u5269\u4f59\u65e5\u5fd7<\/strong><\/h2>\n<p>batch_handler.flush()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u8bb0\u5f55\u548c\u6253\u5370\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9\uff0c\u5305\u62ec\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u3001<code>loguru<\/code>\u5e93\u3001\u81ea\u5b9a\u4e49\u65e5\u5fd7\u5904\u7406\u51fd\u6570\u7b49\u65b9\u6cd5\u3002\u540c\u65f6\uff0c\u4ecb\u7ecd\u4e86\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f\u3001\u65e5\u5fd7\u8f6e\u8f6c\u3001\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u548c\u6587\u4ef6\u3001\u65e5\u5fd7\u7684\u9ad8\u7ea7\u914d\u7f6e\u548c\u6027\u80fd\u4f18\u5316\u7b49\u5185\u5bb9\u3002\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7ba1\u7406\u548c\u8c03\u8bd5\u5e94\u7528\u7a0b\u5e8f\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u548c\u6253\u5370log\u6587\u4ef6\u5185\u5bb9\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8bfb\u53d6log\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002\u901a\u5e38\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>read()<\/code>\u6216<code>readlines()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5185\u5bb9\uff0c\u6700\u540e\u6253\u5370\u51fa\u6765\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;your_log_file.log&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6574\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u4e00\u6b21\u6027\u8bfb\u53d6\u5e76\u6253\u5370\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7ba1\u7406log\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684<code>logging<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ba1\u7406\u548c\u6253\u5370log\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\u6765\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u4ee5\u53ca\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u6216\u63a7\u5236\u53f0\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import logging\n\nlogging.basicConfig(filename=&#39;your_log_file.log&#39;, level=logging.DEBUG)\nlogging.debug(&#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u65e5\u5fd7\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ecelog\u6587\u4ef6\u4e2d\u7b5b\u9009\u7279\u5b9a\u4fe1\u606f\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u53ea\u60f3\u6253\u5370log\u6587\u4ef6\u4e2d\u5305\u542b\u7279\u5b9a\u5173\u952e\u5b57\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\uff0c\u5e76\u5224\u65ad\u662f\u5426\u5305\u542b\u6240\u9700\u7684\u5173\u952e\u5b57\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">keyword = &#39;ERROR&#39;\nwith open(&#39;your_log_file.log&#39;, &#39;r&#39;) as file:\n    for line in file:\n        if keyword in line:\n            print(line)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u5e2e\u52a9\u4f60\u5feb\u901f\u7b5b\u9009\u51fa\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u63d0\u9ad8\u65e5\u5fd7\u5206\u6790\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6253\u5370\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528logging\u6a21\u5757\u3001open\u51fd\u6570\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982l [&hellip;]","protected":false},"author":3,"featured_media":1180554,"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\/1180539"}],"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=1180539"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180539\/revisions"}],"predecessor-version":[{"id":1180556,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180539\/revisions\/1180556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1180554"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}