<legend id='jgXZl'><style id='jgXZl'><dir id='jgXZl'><q id='jgXZl'></q></dir></style></legend>
      <tfoot id='jgXZl'></tfoot>

      • <bdo id='jgXZl'></bdo><ul id='jgXZl'></ul>
      1. <small id='jgXZl'></small><noframes id='jgXZl'>

      2. <i id='jgXZl'><tr id='jgXZl'><dt id='jgXZl'><q id='jgXZl'><span id='jgXZl'><b id='jgXZl'><form id='jgXZl'><ins id='jgXZl'></ins><ul id='jgXZl'></ul><sub id='jgXZl'></sub></form><legend id='jgXZl'></legend><bdo id='jgXZl'><pre id='jgXZl'><center id='jgXZl'></center></pre></bdo></b><th id='jgXZl'></th></span></q></dt></tr></i><div id='jgXZl'><tfoot id='jgXZl'></tfoot><dl id='jgXZl'><fieldset id='jgXZl'></fieldset></dl></div>
      3. 如何将标准输出和标准错误重定向到 Python 中的记录器

        时间:2024-04-21

        <i id='wxZj3'><tr id='wxZj3'><dt id='wxZj3'><q id='wxZj3'><span id='wxZj3'><b id='wxZj3'><form id='wxZj3'><ins id='wxZj3'></ins><ul id='wxZj3'></ul><sub id='wxZj3'></sub></form><legend id='wxZj3'></legend><bdo id='wxZj3'><pre id='wxZj3'><center id='wxZj3'></center></pre></bdo></b><th id='wxZj3'></th></span></q></dt></tr></i><div id='wxZj3'><tfoot id='wxZj3'></tfoot><dl id='wxZj3'><fieldset id='wxZj3'></fieldset></dl></div>
        <tfoot id='wxZj3'></tfoot>
        <legend id='wxZj3'><style id='wxZj3'><dir id='wxZj3'><q id='wxZj3'></q></dir></style></legend>

              <bdo id='wxZj3'></bdo><ul id='wxZj3'></ul>
              1. <small id='wxZj3'></small><noframes id='wxZj3'>

                    <tbody id='wxZj3'></tbody>

                  本文介绍了如何将标准输出和标准错误重定向到 Python 中的记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a logger that has a RotatingFileHandler. I want to redirect all Stdout and Stderr to the logger. How to do so?

                  解决方案

                  Not enough rep to comment, but I wanted to add the version of this that worked for me in case others are in a similar situation.

                  class LoggerWriter:
                      def __init__(self, level):
                          # self.level is really like using log.debug(message)
                          # at least in my case
                          self.level = level
                  
                      def write(self, message):
                          # if statement reduces the amount of newlines that are
                          # printed to the logger
                          if message != '
                  ':
                              self.level(message)
                  
                      def flush(self):
                          # create a flush method so things can be flushed when
                          # the system wants to. Not sure if simply 'printing'
                          # sys.stderr is the correct way to do it, but it seemed
                          # to work properly for me.
                          self.level(sys.stderr)
                  

                  and this would look something like:

                  log = logging.getLogger('foobar')
                  sys.stdout = LoggerWriter(log.debug)
                  sys.stderr = LoggerWriter(log.warning)
                  

                  这篇关于如何将标准输出和标准错误重定向到 Python 中的记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python,带有基本身份验证的 HTTPS GET 下一篇:在 Python 中使用 try/except 将字符串转换为 Int

                  相关文章

                • <legend id='ODoY3'><style id='ODoY3'><dir id='ODoY3'><q id='ODoY3'></q></dir></style></legend>

                  <small id='ODoY3'></small><noframes id='ODoY3'>

                    <bdo id='ODoY3'></bdo><ul id='ODoY3'></ul>
                    <tfoot id='ODoY3'></tfoot>

                    <i id='ODoY3'><tr id='ODoY3'><dt id='ODoY3'><q id='ODoY3'><span id='ODoY3'><b id='ODoY3'><form id='ODoY3'><ins id='ODoY3'></ins><ul id='ODoY3'></ul><sub id='ODoY3'></sub></form><legend id='ODoY3'></legend><bdo id='ODoY3'><pre id='ODoY3'><center id='ODoY3'></center></pre></bdo></b><th id='ODoY3'></th></span></q></dt></tr></i><div id='ODoY3'><tfoot id='ODoY3'></tfoot><dl id='ODoY3'><fieldset id='ODoY3'></fieldset></dl></div>