【问题标题】:How can I write different data into two different logs?如何将不同的数据写入两个不同的日志?
【发布时间】:2019-09-14 00:19:43
【问题描述】:

我创建了两个具有两个不同名称(窗口)的独立日志。一个是旋转日志,另一个不是。如何防止我写入循环日志的数据记录到非循环日志?

我不是很精通日志。

导入模块

import logging
from logging.handlers import RotatingFileHandler

def main():

    def normalLogging(fn1):
        global normalLogger
        #Create and configure logger
        logging.basicConfig(filename=fn1, format='%(asctime)s %(message)s', filemode='w')

        #Creating an object
        normalLogger=logging.getLogger()

        #Setting the threshold of logger to DEBUG
        normalLogger.setLevel(logging.DEBUG)

    def rotatingLog(fn2):
        global rotatingLogger
        # start the roll-screen logger too
        rotatingLogger = logging.getLogger(fn2)
        rotatingLogger.setLevel(logging.DEBUG)

        # add a rotating logger
        handlerScreen = RotatingFileHandler(fn2, maxBytes=1000, backupCount=3)
        rotatingLogger.addHandler(handlerScreen)

    def normalTest():
        #Test messages to the rotating log
        normalLogger.debug("normalLogger.debug - Harmless debug Message")
        normalLogger.info("normalLogger.info - Just an information")
        normalLogger.warning("normalLogger.warning - Its a Warning")
        normalLogger.error("normalLogger.error - Did you try to divide by zero")
        normalLogger.critical("normalLogger.critical - Internet is down")

    def rotatorTest():
        for i in range(1, 100):
            #Test messages to the rotating log
            rotatingLogger.debug("rotatingLogger.debug %s - Harmless debug Message" % i)
            rotatingLogger.info("rotatingLogger.info %s - Just an information" % i)
            rotatingLogger.warning("rotatingLogger.warning %s - Its a Warning" % i)
            rotatingLogger.error("rotatingLogger.error %s - Did you try to divide by zero" % i)
            rotatingLogger.critical("rotatingLogger.critical %s - Internet is down" % i)

    # fn2 = "rotatorLog"
    rotatingLog("rotatorLog")
    rotatorTest()

    # fn1 = "normalLog"
    normalLogging("normalLog")
    normalTest()

    rotatorTest()

if __name__ == '__main__':
    main()

循环日志维护它自己的唯一数据,但普通日志有来自循环日志的数据。我希望每个日志的数据都是唯一的,因为我分别写入它们,但事实并非如此。

【问题讨论】:

  • 普通记录器的第一个使用名称 - logging.getLogger(fn1)。接下来使用FileHandler 而不是basicConfig

标签: python python-2.7 logging


【解决方案1】:

您需要做的就是rotatingLogger.propagate = False 停止将其日志发送到用于 normalLogger 的根记录器。 basicConfig 配置根记录器,不带名称的 logging.getLogger 返回根记录器。

【讨论】:

    【解决方案2】:

    首先:使用普通记录器的名称 - 即。 logging.getLogger(fn1)

    第二:使用FileHandler 而不是basicConfig 用于普通记录器。

    import logging
    from logging.handlers import RotatingFileHandler
    from logging import FileHandler
    
    def main():
    
        def normalLogging(fn1):
            global normalLogger
            global normalLogger
    
            normalLogger = logging.getLogger(fn1)
            normalLogger.setLevel(logging.DEBUG)
    
            handlerScreen = FileHandler(fn1)
            handlerScreen.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
            normalLogger.addHandler(handlerScreen)
    
        def rotatingLog(fn2):
            global rotatingLogger
    
            rotatingLogger = logging.getLogger(fn2)
            rotatingLogger.setLevel(logging.DEBUG)
    
            handlerScreen = RotatingFileHandler(fn2, maxBytes=1000, backupCount=3)
            handlerScreen.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
            rotatingLogger.addHandler(handlerScreen)
    
     # ... rest ...
    

    我记得basicConfig 创建了记录器,后来被所有记录器使用,所以它们使用相同的设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多