【发布时间】: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