我的日志记录类中的异常行为 [英] Strange behaviour on my logging class

查看:77
本文介绍了我的日志记录类中的异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽量恢复.我有我写的这堂课:

I will try to resume as much as possible. I have this class I wrote:

import logging, logging.handlers.TimedRotatingFileHandler

class Logger(object):
    def __init__(self, log_filename):
        logging.basicConfig(format='%(asctime)s %(message)s')
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        loghandler = TimedRotatingFileHandler(
            log_filename, when="midnight", backupCount=50
        )
        loghandler.setFormatter(formatter)
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(loghandler)

    def getLogger(self):
        return self.logger

它确实运行良好,现在当我有一个使用Logger实例的脚本时出现了问题,并且在该脚本中我也实例化了一个使用Logger的类,如下所示:

It works good indeed, now the problem arises when I have a script that uses a Logger instance and within that script I instantiate a class that uses a Logger too, something like this:

import ClassA

A = ClassA()
log = Logger(log_filename='script_logger.log')
logger = log.getLogger()
logger.info('Initiated Script')

while True:
    logger.info('Looping')
    A.run()

我的班级样子:

class ClassA(object):
    def __init__(self):
        log = Logger(log_filename='class_logger.log')
        self.logger = log.getLogger()
        self.logger.info('Started ClassA')

    def run(self):
        self.logger.info('Into method run')

现在,我希望有2个单独的日志文件,class_logger.logscript_logger.log可以正常工作,但是两个文件的内容逐行完全相同.

Now I expect to have 2 separate log files, class_logger.log and script_logger.log that works OK, but both files have exactly the same content line by line.

所以script_logger.logclass_logger.log具有以下内容:

Started classA
Initiated Script
Looping
Into method run
Looping
Into method run
...

有任何线索吗?

推荐答案

原因是当您进行logging.getLogger()时,类和脚本具有相同的记录器对象.这是一个单身人士.如果您想要其他记录器,则应传递记录器名称,例如logging.getLogger('logger1') 通常,库会执行logging.getLogger(__name__),以便每个模块都使用不同的记录器.请参考 http://docs.python.org/2/library/logging. html#logger-objects

The reason is the class and script have the same logger object when you do logging.getLogger(). It's a singleton. If you want different loggers then you should pass the logger name e.g logging.getLogger('logger1') Typically libraries do logging.getLogger(__name__) so that each module gets a different logger. refer http://docs.python.org/2/library/logging.html#logger-objects

我已经修改了您的代码,以便现在可以正常使用

I have modified your code so that it works as expected now

import logging, logging.handlers
from logging.handlers import TimedRotatingFileHandler

class Logger(object):
    def __init__(self, log_filename, name):
        logging.basicConfig(format='%(asctime)s %(message)s')
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        loghandler = TimedRotatingFileHandler(
            log_filename, when="midnight", backupCount=50
        )
        loghandler.setFormatter(formatter)
        self.logger = logging.getLogger(name)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(loghandler)

    def getLogger(self):
        return self.logger


class ClassA(object):
    def __init__(self):
        log = Logger(log_filename='class_logger.log', name="Class")
        self.logger = log.getLogger()
        self.logger.info('Started ClassA')

    def run(self):
        self.logger.info('Into method run')


A = ClassA()
log = Logger(log_filename='script_logger.log', name="Script")
logger = log.getLogger()
logger.info('Initiated Script')

for x in range(5):
    logger.info('Looping')
    A.run()

这篇关于我的日志记录类中的异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆