python-3.x - 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。
解决方案
推荐阅读
- ios - 世博会推送通知 iOS APNS
- reactjs - React 放置自动完成多个类型的问题(Nextjs)
- c# - 我想通过单击将按钮移动到堆栈面板的最顶部
- python - 根据时间戳将数据帧拆分为多个子数据帧
- lustre - 如何为 MOFED 正确重建 Lustre
- c# - 单击保存按钮时发生未处理的异常
- angular - 可观察对象内的角度异步调用
- windows - 我运行我的第一个 tomcat,但出现错误,我该怎么办?
- amazon-web-services - AWS - boto3 EC2 启动/停止 - 自动化
- python - 多个 pandas 组的条件求和,每个组由一组重叠的列值定义