<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 中的记录器

        How to redirect stdout and stderr to logger in Python(如何将标准输出和标准错误重定向到 Python 中的记录器)

        <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 中的记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                    <tbody id='ODoY3'></tbody>
                • <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>