首页 > 解决方案 > Python日志打印双重到控制台

问题描述

似乎在几台机器上我得到了这样的双重输出:

INFO LED NOTIFICATION STARTED
INFO:output_logger:LED NOTIFICATION STARTED

这是我正在使用的功能:

def setup_logger(name, log_file, level=logging.INFO, ContentFormat='%(asctime)s %(levelname)s %(message)s', DateTimeFormat="%Y-%m-%d %H:%M:%S", CreateConsoleLogger=False):
    """Function setup as many loggers as you want"""

    logger = logging.getLogger(name)
    logger.setLevel(level)

    if CreateConsoleLogger:
        # create console handler
        handler = logging.StreamHandler()
        handler.setLevel(level)
        formatter = logging.Formatter("%(levelname)s %(message)s")
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    # create a file handler
    handler = RotatingFileHandler(log_file, maxBytes=2000000, backupCount=5)
    handler.setLevel(level)
    formatter = logging.Formatter(ContentFormat, DateTimeFormat)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    return logger

这就是我创建记录器的方式:

output_logger = setup_logger('output_logger', 'log/autofy.log', level=logging.DEBUG, CreateConsoleLogger=True)

这就是我所说的:

output_logger.info("LED NOTIFICATION STARTED")

在大多数计算机上,我只是看到打印到控制台的相同消息按预期保存到文件中("INFO LED NOTIFICATION STARTED"),但在其他计算机上,它正在执行这种奇怪的双输出操作。我的代码从一台计算机到另一台计算机是完全相同的,所以有什么想法会在某些计算机上而不是其他计算机上导致这种情况吗?

编辑

我正在使用 notepad++ 编写脚本并在 Ubuntu 16.04 机器上的终端窗口中运行它。我正在使用python3。

标签: python-3.xlogging

解决方案


推荐阅读