为什么我的日志已打印两次

时间:2018-06-29 12:47:35

标签: python class logging

为什么我的日志已被打印两次 我有三个python文件 当我打电话给test2时: 我的日志已打印两次

2018-06-29 20:41:36,466 - test - INFO - 2222222222222222222
2018-06-29 20:41:36,466 - test - INFO - 2222222222222222222

我没有将AAA方法my_print调用。

handle_log.py

#-*-coding:utf-8-*-
import logging
import logging.handlers
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

        # #error日志发送邮件
        #
class Logger(object):
    def __init__(self, path='test',title=None):
        # 得到一个日志对象
        self.logger = logging.getLogger(path)
        # 修改默认级别
        self.logger.setLevel(logging.INFO)

        #将日志发送至邮件
        self.mail_log = logging.handlers.SMTPHandler(('mail.test.com',123),'u2_dev_send@test.com','test@test.com',title,credentials=('test@test.com','test'),secure=None)
        self.mail_log.setLevel(logging.ERROR)

        # 记录日志的文件和级别
        self.write_log = logging.FileHandler("all.log")
        self.write_log.setLevel(logging.INFO)

        # 打印日志的到窗口上
        self.print_log = logging.StreamHandler()
        self.print_log.setLevel(logging.INFO)

        # 设置日志格式
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        self.write_log.setFormatter(formatter)
        self.print_log.setFormatter(formatter)
        self.mail_log.setFormatter(formatter)

        # 将相应的发送目的地添加到logger对象中
        self.logger.addHandler(self.write_log)
        self.logger.addHandler(self.print_log)
        self.logger.addHandler(self.mail_log)

    # def __del__(self):
    #     self.logger.removeHandler(self.write_log)
    #     self.logger.removeHandler(self.print_log)
    #     self.logger.removeHandler(self.mail_log)

    def debug(self, message):
        self.logger.debug(message)
        # self.logger.removeHandler(self.print_log)
        # self.logger.removeHandler(self.mail_log)
        # self.logger.removeHandler(self.write_log)

    def info(self, message):
        self.logger.info(message)
        # self.logger.removeHandler(self.print_log)
        # self.logger.removeHandler(self.mail_log)
        # self.logger.removeHandler(self.write_log)

    def war(self, message):
        self.logger.warning(message)
        # self.logger.removeHandler(self.print_log)
        # self.logger.removeHandler(self.mail_log)
        # self.logger.removeHandler(self.write_log)

    def error(self, message):
        self.logger.error(message)
        # self.logger.removeHandler(self.print_log)
        # self.logger.removeHandler(self.mail_log)
        # self.logger.removeHandler(self.write_log)

    def cri(self, message):
        self.logger.critical(message)
        # self.logger.removeHandler(self.print_log)
        # self.logger.removeHandler(self.mail_log)
        # self.logger.removeHandler(self.write_log)

test1.py

#-*-coding:utf-8-*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# sys.path.append('/data1/test/app_sem')
sys.path.append('/root/request2_python/')
from sem.handle_log import Logger


class AAAA(object):
    def __init__(self):
        self.log = Logger(title='bbbbbbbbbbb')
    def my_print(self):
        self.log.info('1111111111111111111111')



# a = AAAA()
# a.my_print()

test2.py

#-*-coding:utf-8-*-

import sys
reload(sys)
sys.setdefaultencoding('utf8')
# sys.path.append('/data1/test/app_sem')
sys.path.append('/root/request2_python/')
from sem.handle_log import Logger
from sem.vivo.test1 import AAAA


class BBBB(object):
    def __init__(self):
        self.log_test2 = Logger(title='cccccccc')
    def my_print_b(self):
        self.log_test2.info('2222222222222222222')

b = BBBB()
a = AAAA()
# a.my_print()
b.my_print_b()
# b.my_print_b()

结果:

2018-06-29 20:41:36,466 - test - INFO - 2222222222222222222
2018-06-29 20:41:36,466 - test - INFO - 2222222222222222222

为什么相同的日志行打印两次?

1 个答案:

答案 0 :(得分:0)

您的所有Logger都具有相同的名称“ test”,因此,当您发送日志消息以“ test”时,它们都会做出反应。

要拥有多个记录器,请确保为它们提供不同的名称,例如,使用“ path”变量。

例如:

class Logger(object):
def __init__(self, path='test',title=None):
    self.logger = logging.getLogger(path + title)
    [...]