{"id":1130816,"date":"2025-01-08T20:43:29","date_gmt":"2025-01-08T12:43:29","guid":{"rendered":""},"modified":"2025-01-08T20:43:32","modified_gmt":"2025-01-08T12:43:32","slug":"%e5%a6%82%e4%bd%95%e9%87%8d%e5%ae%9a%e5%90%91python%e7%9a%84%e6%a0%87%e5%87%86%e8%be%93%e5%87%ba%e5%8f%8a%e6%a0%87%e5%87%86%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1130816.html","title":{"rendered":"\u5982\u4f55\u91cd\u5b9a\u5411python\u7684\u6807\u51c6\u8f93\u51fa\u53ca\u6807\u51c6\u9519\u8bef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100751\/3b0e4e32-f4dc-4624-864d-8ba85f51e723.webp\" alt=\"\u5982\u4f55\u91cd\u5b9a\u5411python\u7684\u6807\u51c6\u8f93\u51fa\u53ca\u6807\u51c6\u9519\u8bef\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u91cd\u5b9a\u5411Python\u7684\u6807\u51c6\u8f93\u51fa\u53ca\u6807\u51c6\u9519\u8bef<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u5e93<code>sys<\/code>\u548c\u6587\u4ef6\u5bf9\u8c61\u6765\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002<strong>\u4f7f\u7528<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\u91cd\u5b9a\u5411\u8f93\u51fa\u3001\u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5b9e\u73b0\u91cd\u5b9a\u5411\u3001\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7075\u6d3b\u5730\u7ba1\u7406\u7a0b\u5e8f\u7684\u8f93\u51fa\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\u91cd\u5b9a\u5411\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6807\u51c6\u8f93\u51fa\uff08stdout\uff09\u548c\u6807\u51c6\u9519\u8bef\uff08stderr\uff09\u662f\u7cfb\u7edf\u7ea7\u7684\u8f93\u51fa\u6d41\uff0c\u901a\u5e38\u7528\u4e8e\u663e\u793a\u7a0b\u5e8f\u8fd0\u884c\u7684\u7ed3\u679c\u548c\u9519\u8bef\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u91cd\u5b9a\u5411\u8fd9\u4e9b\u8f93\u51fa\u6d41\uff0c\u5c06\u8f93\u51fa\u4fe1\u606f\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u5176\u4ed6\u5730\u65b9\u3002<\/p>\n<\/p>\n<p><h4>1. \u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4e86\u89e3\u5982\u4f55\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u7528\u4e8e\u5199\u5165<\/strong><\/h2>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as f:<\/p>\n<p>    # \u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6<\/p>\n<p>    sys.stdout = f<\/p>\n<p>    print(&quot;This will be written to the file.&quot;)<\/p>\n<h2><strong>\u6062\u590d\u6807\u51c6\u8f93\u51fa<\/strong><\/h2>\n<p>sys.stdout = sys.__stdout__<\/p>\n<p>print(&quot;This will be printed to the console.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6253\u5f00\u4e86\u4e00\u4e2a\u6587\u4ef6<code>output.txt<\/code>\u7528\u4e8e\u5199\u5165\uff0c\u5e76\u5c06<code>sys.stdout<\/code>\u91cd\u5b9a\u5411\u5230\u8be5\u6587\u4ef6\u3002\u6b64\u65f6\uff0c\u4efb\u4f55<code>print<\/code>\u8bed\u53e5\u7684\u8f93\u51fa\u90fd\u4f1a\u5199\u5165\u6587\u4ef6\u800c\u4e0d\u662f\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e0a\u3002\u91cd\u5b9a\u5411\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c06<code>sys.stdout<\/code>\u6062\u590d\u4e3a\u9ed8\u8ba4\u7684\u6807\u51c6\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2. \u91cd\u5b9a\u5411\u6807\u51c6\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u5b9a\u5411\u6807\u51c6\u9519\u8bef\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c06\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u7528\u4e8e\u5199\u5165<\/strong><\/h2>\n<p>with open(&#39;error.txt&#39;, &#39;w&#39;) as f:<\/p>\n<p>    # \u5c06\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u6587\u4ef6<\/p>\n<p>    sys.stderr = f<\/p>\n<p>    # \u89e6\u53d1\u4e00\u4e2a\u9519\u8bef<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;This will be written to the file.&quot;)<\/p>\n<h2><strong>\u6062\u590d\u6807\u51c6\u9519\u8bef<\/strong><\/h2>\n<p>sys.stderr = sys.__stderr__<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06<code>sys.stderr<\/code>\u91cd\u5b9a\u5411\u5230<code>error.txt<\/code>\u6587\u4ef6\uff0c\u5f53\u89e6\u53d1\u4e00\u4e2a<code>ValueError<\/code>\u65f6\uff0c\u9519\u8bef\u4fe1\u606f\u4f1a\u88ab\u5199\u5165\u8be5\u6587\u4ef6\u800c\u4e0d\u662f\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5b9e\u73b0\u91cd\u5b9a\u5411<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u66f4\u52a0\u4f18\u96c5\u5730\u5b9e\u73b0\u8f93\u51fa\u91cd\u5b9a\u5411\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u5728\u4e0a\u4e0b\u6587\u7ed3\u675f\u65f6\u81ea\u52a8\u6062\u590d\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>1. \u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from contextlib import contextmanager<\/p>\n<p>@contextmanager<\/p>\n<p>def redirect_output(stdout=None, stderr=None):<\/p>\n<p>    old_stdout, old_stderr = sys.stdout, sys.stderr<\/p>\n<p>    try:<\/p>\n<p>        if stdout:<\/p>\n<p>            sys.stdout = stdout<\/p>\n<p>        if stderr:<\/p>\n<p>            sys.stderr = stderr<\/p>\n<p>        yield<\/p>\n<p>    finally:<\/p>\n<p>        sys.stdout, sys.stderr = old_stdout, old_stderr<\/p>\n<h2><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u91cd\u5b9a\u5411\u8f93\u51fa<\/strong><\/h2>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as out, open(&#39;error.txt&#39;, &#39;w&#39;) as err:<\/p>\n<p>    with redirect_output(out, err):<\/p>\n<p>        print(&quot;This will be written to output.txt&quot;)<\/p>\n<p>        raise ValueError(&quot;This will be written to error.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>redirect_output<\/code>\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u5b83\u53ef\u4ee5\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684<code>try<\/code>\u5757\u4e2d\uff0c\u6211\u4eec\u5c06<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\u91cd\u5b9a\u5411\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u3002\u5728<code>finally<\/code>\u5757\u4e2d\uff0c\u6211\u4eec\u786e\u4fdd\u5c06\u5b83\u4eec\u6062\u590d\u4e3a\u539f\u59cb\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5c24\u5176\u662f\u9700\u8981\u8bb0\u5f55\u5927\u91cf\u4fe1\u606f\u6216\u5904\u7406\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\u7684\u573a\u666f\uff0cPython\u7684<code>logging<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6e<code>logging<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u914d\u7f6e<code>logging<\/code>\u6a21\u5757\uff0c\u5c06\u65e5\u5fd7\u4fe1\u606f\u5199\u5165\u6587\u4ef6\uff0c\u540c\u65f6\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff08\u5982DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.DEBUG,<\/p>\n<p>                    format=&#39;%(asctime)s - %(name)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<h2><strong>\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f<\/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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>logging.basicConfig<\/code>\u914d\u7f6e\u4e86\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u5c06\u65e5\u5fd7\u4fe1\u606f\u5199\u5165<code>app.log<\/code>\u6587\u4ef6\uff0c\u5e76\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3aDEBUG\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u5e76\u8bb0\u5f55\u4e86\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2. \u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411\u5230<code>logging<\/code>\u6a21\u5757\uff0c\u4ee5\u4fbf\u6355\u83b7\u6240\u6709\u8f93\u51fa\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import logging<\/p>\n<p>class StreamToLogger:<\/p>\n<p>    def __init__(self, logger, log_level):<\/p>\n<p>        self.logger = logger<\/p>\n<p>        self.log_level = log_level<\/p>\n<p>        self.linebuf = &#39;&#39;<\/p>\n<p>    def write(self, buf):<\/p>\n<p>        for line in buf.rstrip().splitlines():<\/p>\n<p>            self.logger.log(self.log_level, line.rstrip())<\/p>\n<p>    def flush(self):<\/p>\n<p>        pass<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.DEBUG,<\/p>\n<p>                    format=&#39;%(asctime)s - %(name)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(&#39;STDOUT&#39;)<\/p>\n<p>stderr_logger = logging.getLogger(&#39;STDERR&#39;)<\/p>\n<h2><strong>\u521b\u5efaStreamToLogger\u5b9e\u4f8b<\/strong><\/h2>\n<p>stdout_logger = StreamToLogger(logger, logging.INFO)<\/p>\n<p>stderr_logger = StreamToLogger(stderr_logger, logging.ERROR)<\/p>\n<h2><strong>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef<\/strong><\/h2>\n<p>sys.stdout = stdout_logger<\/p>\n<p>sys.stderr = stderr_logger<\/p>\n<h2><strong>\u793a\u4f8b\u8f93\u51fa<\/strong><\/h2>\n<p>print(&quot;This will be logged as an INFO message&quot;)<\/p>\n<p>raise ValueError(&quot;This will be logged as an ERROR message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>StreamToLogger<\/code>\u7c7b\uff0c\u5b83\u5c06\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411\u5230<code>logging<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u521b\u5efa<code>StreamToLogger<\/code>\u5b9e\u4f8b\uff0c\u6211\u4eec\u5c06<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\u91cd\u5b9a\u5411\u5230\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u5b9e\u73b0\u4e86\u5c06\u6240\u6709\u8f93\u51fa\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1. \u8c03\u8bd5\u548c\u5f00\u53d1<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u9636\u6bb5\uff0c\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6355\u83b7\u6240\u6709\u8f93\u51fa\u4fe1\u606f\uff0c\u4fbf\u4e8e\u5206\u6790\u548c\u6392\u67e5\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u9519\u8bef\u4fe1\u606f\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u67e5\u770b\u548c\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def main():<\/p>\n<p>    try:<\/p>\n<p>        # \u4e3b\u7a0b\u5e8f\u903b\u8f91<\/p>\n<p>        print(&quot;Program is running&quot;)<\/p>\n<p>        raise ValueError(&quot;An error occurred&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        with open(&#39;error.log&#39;, &#39;a&#39;) as f:<\/p>\n<p>            sys.stderr = f<\/p>\n<p>            print(str(e))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u9519\u8bef\u4fe1\u606f\u4fdd\u5b58\u5230<code>error.log<\/code>\u6587\u4ef6\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u67e5\u770b\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>2. \u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u6211\u4eec\u53ef\u4ee5\u914d\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u5c06\u65e5\u5fd7\u4fe1\u606f\u5199\u5165\u6587\u4ef6\u6216\u53d1\u9001\u5230\u8fdc\u7a0b\u65e5\u5fd7\u670d\u52a1\u5668\uff0c\u4ee5\u4fbf\u96c6\u4e2d\u7ba1\u7406\u548c\u76d1\u63a7\u3002\u4f8b\u5982\uff1a<\/p>\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\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>handler = RotatingFileHandler(&#39;app.log&#39;, maxBytes=2000, backupCount=5)<\/p>\n<p>logging.basicConfig(handlers=[handler], level=logging.INFO,<\/p>\n<p>                    format=&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<h2><strong>\u793a\u4f8b\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logger.info(&quot;Application started&quot;)<\/p>\n<p>try:<\/p>\n<p>    # \u4e3b\u7a0b\u5e8f\u903b\u8f91<\/p>\n<p>    raise ValueError(&quot;An error occurred&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    logger.error(&quot;Error occurred&quot;, exc_info=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>RotatingFileHandler<\/code>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u5b9e\u73b0\u65e5\u5fd7\u6587\u4ef6\u7684\u8f6e\u8f6c\uff0c\u4ee5\u9632\u6b62\u65e5\u5fd7\u6587\u4ef6\u8fc7\u5927\u5f71\u54cd\u7cfb\u7edf\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u91cd\u5b9a\u5411Python\u7684\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002<strong>\u4f7f\u7528<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\u91cd\u5b9a\u5411\u8f93\u51fa\u3001\u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5b9e\u73b0\u91cd\u5b9a\u5411\u3001\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52bf\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8c03\u8bd5\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u4e2d\u7684<code>stdout<\/code>\u548c<code>stderr<\/code>\u5bf9\u8c61\u6765\u5b9e\u73b0\u91cd\u5b9a\u5411\u3002\u901a\u8fc7\u5c06\u8fd9\u4e9b\u5bf9\u8c61\u6307\u5411\u4e00\u4e2a\u6587\u4ef6\u6216\u81ea\u5b9a\u4e49\u7684\u6d41\uff0c\u53ef\u4ee5\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u5e76\u5c06\u5176\u4f5c\u4e3a\u8f93\u51fa\u76ee\u6807\uff0c\u6216\u8005\u4f7f\u7528<code>io.StringIO<\/code>\u7c7b\u6765\u5b58\u50a8\u8f93\u51fa\u4ee5\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n<p><strong>\u5728\u91cd\u5b9a\u5411\u540e\uff0c\u5982\u4f55\u6062\u590d\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff1f<\/strong><br \/>\u91cd\u5b9a\u5411\u540e\uff0c\u60a8\u53ef\u4ee5\u5c06<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\u91cd\u65b0\u6307\u5411\u539f\u6765\u7684\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002\u53ef\u4ee5\u5728\u91cd\u5b9a\u5411\u4e4b\u524d\u4fdd\u5b58\u539f\u59cb\u7684<code>stdout<\/code>\u548c<code>stderr<\/code>\uff0c\u7136\u540e\u5728\u9700\u8981\u65f6\u5c06\u5b83\u4eec\u6062\u590d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>original_stdout = sys.stdout<\/code>\u6765\u4fdd\u5b58\u539f\u59cb\u8f93\u51fa\u6d41\uff0c\u5e76\u5728\u5b8c\u6210\u91cd\u5b9a\u5411\u540e\u4f7f\u7528<code>sys.stdout = original_stdout<\/code>\u6765\u6062\u590d\u3002<\/p>\n<p><strong>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u65f6\uff0c\u5982\u4f55\u540c\u65f6\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5c06<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\u540c\u65f6\u91cd\u5b9a\u5411\u5230\u540c\u4e00\u4e2a\u6587\u4ef6\u6216\u6d41\u4e2d\u6765\u6355\u83b7\u4e24\u8005\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7684\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u90fd\u4f1a\u88ab\u5199\u5165\u540c\u4e00\u4e2a\u76ee\u6807\uff0c\u65b9\u4fbf\u540e\u7eed\u67e5\u770b\u548c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u5e76\u5c06\u5176\u540c\u65f6\u8d4b\u503c\u7ed9<code>sys.stdout<\/code>\u548c<code>sys.stderr<\/code>\uff0c\u8fd9\u6837\u5728\u8fd0\u884c\u4ee3\u7801\u65f6\uff0c\u6240\u6709\u4fe1\u606f\u90fd\u4f1a\u8bb0\u5f55\u5728\u8be5\u6587\u4ef6\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u91cd\u5b9a\u5411Python\u7684\u6807\u51c6\u8f93\u51fa\u53ca\u6807\u51c6\u9519\u8bef \u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u5e93sys\u548c\u6587\u4ef6\u5bf9\u8c61\u6765\u91cd\u5b9a\u5411\u6807 [&hellip;]","protected":false},"author":3,"featured_media":1130825,"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\/1130816"}],"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=1130816"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1130816\/revisions"}],"predecessor-version":[{"id":1130828,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1130816\/revisions\/1130828"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1130825"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1130816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1130816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1130816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}