如何将不同的数据写入两个不同的日志?

时间:2019-09-14 00:19:43

标签: python python-2.7 logging

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

我不是很懂日志。

导入模块

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()

旋转日志保留其自己的唯一数据,但普通日志具有旋转日志中的数据。我希望该数据对于每个日志而言都是唯一的,因为我是分别写入它们的,但事实并非如此。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

首先:将名称用于普通记录器-即onsubmit="return false"

第二:对于普通记录器,使用submitLogin()代替function countdown() { var counter = 60; var timeCountdown = setInterval(function(){ counter-- $('.timer-container .time').html(counter); if (counter === 0) { clearInterval(timeCountdown); $('.timer-container .time').html("Times Up"); points(); } }, 1000); $('.timer-container').toggle(); } let trivia = [ { question: "How many wheels are on a skateboard?", choices: ["2", "4", "6", "8"], answer: "2", }, { question: "Who invented the kickflip?", choices: ["Tony Hawk", "Bam Magera", "Rodney Mullen", "Chad Muska"], answer: "Rodney Mullen" }, { question: "Who did the first 900?", choices: ["Tony Hawk", "Tas Pappas", "Danny Way", "bob burnquist"], answer: "Tony Hawk", }, { question: "What is another word for a 360 flip?", choices: ["Impossible Flip", "3 1/2 flip", "Tre Bomb", "Tri Flip"], answer: "Tre Bomb", } ]; function triviaQuestions() { for(var i = 0; i < trivia.length; i++) { var questionHeader = $('<h2 class="question-' + i + '">'); var questionHeaderContent = questionHeader.text(trivia[i].question); $('.question-container').append(questionHeaderContent).append("<form class='choices choices-container-" + i + " '>"); for (var j = 0; j < trivia.length; j++) { console.log(trivia[i].choices[j]); var questionChoices = $('<input type="radio"' + 'name="' + i + '"'+ 'value="' + trivia[i].choices[j] + '">' + '<label>' + trivia[i].choices[j] + '</label>'); var questionChoicesContent = questionChoices.text(trivia[i].choices[j]); $('.choices-container-' + i).append(questionChoices).append(questionChoicesContent); } } } $( document ).ready(function() { $('.start-button').on('click', function() { $(this).toggle(); countdown(); triviaQuestions(); }); });

logging.getLogger(fn1)

我记得FileHandler创建了一个记录器,以后所有记录器都使用它,因此它们使用相同的设置。