Python 日志记录 - 不同的日志但相同的输出

Posted

技术标签:

【中文标题】Python 日志记录 - 不同的日志但相同的输出【英文标题】:Python logging - Different logs but the same output 【发布时间】:2012-10-26 15:31:57 【问题描述】:

我有几个不同的解析器用于不同的网站,我还有一个名为 shared.py 的文件,它具有用于特殊解析的 lxml 函数和一个 base .py 文件,负责处理数据库(保存等)。 如果某事失败(未找到图像)或通过(我们找到图像),我需要将其记录到日志文件中,为此我使用标准日志记录模块。

我编写了一个 logger.py 文件,在其中创建了一个 Log 类,以便我可以在我的解析器或 base.py/shared.py 文件中调用它看起来像这样:

import logging
import os.path

__metaclass__ = type

class Log:
    def __init__(self, filename, loggername="fetchers", path="logs/", formater="%(asctime)s %(levelname)s %(message)s"):
        self.logger = logging.getLogger(loggername)
        self.hdlr = logging.FileHandler(path + os.path.splitext(filename)[0] + ".log")
        self.formater = logging.Formatter(formater)
        self.hdlr.setFormatter(self.formater)
        self.logger.addHandler(self.hdlr)
        self.logger.setLevel(logging.INFO)
    def info(self, msg):
        self.logger.info(msg)
    def warning(self, msg):
        self.logger.warning(msg)
    def error(self, msg):
        self.logger.error(msg)
    def critical(self, msg):
        self.logger.critical(msg)

if __name__ == "__main__":
    pass

文件夹层次结构如下所示;

/解析器 /Parsers/base.py /Parsers/shared.py /Parsers/parserone.py /Parsers/parsertwo.py /Parsers/parsersthree.py /解析器/... /Parsers/logs/base.log /Parsers/logs/shared.log /Parsers.logs/parserOne.log

每个解析器都会导入记录器文件(base.py 和 shared.py 也一样),并像这样初始化记录器:

logger = Log(os.path.basename(__file__))
logger.warning("Something has happened..")

这工作正常,它确实写入日志,但问题是,base.py 写入有关查询等的日志,以及有关失败等的解析器(与 shared.py 相同) 问题是它正在写日志,但它们看起来都一样..

➜  logs  tail parserOne.log 
2012-10-26 16:35:21,250 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/scherpe-omzetdaling-televisiereclame/
2012-10-26 16:35:21,286 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/nominaties-mercurs-bekend2/
2012-10-26 16:35:21,322 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/traditionele-media-nog-steeds-populair-bij-jongeren/
2012-10-26 16:35:21,371 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/persgroep-blijft-sponsor-san/
2012-10-26 16:35:21,407 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/pg-overtreft-verwachtingen/
2012-10-26 16:35:21,443 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/discovery-networks-introduceert-discovery-client-productions/
2012-10-26 16:35:21,479 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/stoelendans-bij-wehkamp.nl/
2012-10-26 16:35:21,563 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/amazon-duikt-in-rode-cijfers/
2012-10-26 16:35:21,599 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/linkedin-rabobank-meest-populaire-werkgever/
2012-10-26 16:35:21,683 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/veronica-uitgeverij-wil-naar-amsterdam/

➜  logs  tail base.log 
2012-10-26 16:35:21,250 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/scherpe-omzetdaling-televisiereclame/
2012-10-26 16:35:21,286 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/nominaties-mercurs-bekend2/
2012-10-26 16:35:21,322 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/traditionele-media-nog-steeds-populair-bij-jongeren/
2012-10-26 16:35:21,371 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/persgroep-blijft-sponsor-san/
2012-10-26 16:35:21,407 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/pg-overtreft-verwachtingen/
2012-10-26 16:35:21,443 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/discovery-networks-introduceert-discovery-client-productions/
2012-10-26 16:35:21,479 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/stoelendans-bij-wehkamp.nl/
2012-10-26 16:35:21,563 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/amazon-duikt-in-rode-cijfers/
2012-10-26 16:35:21,599 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/linkedin-rabobank-meest-populaire-werkgever/
2012-10-26 16:35:21,683 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/veronica-uitgeverij-wil-naar-amsterdam/

➜  logs  tail shared.log 
2012-10-26 16:35:21,250 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/scherpe-omzetdaling-televisiereclame/
2012-10-26 16:35:21,286 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/nominaties-mercurs-bekend2/
2012-10-26 16:35:21,322 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/traditionele-media-nog-steeds-populair-bij-jongeren/
2012-10-26 16:35:21,371 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/persgroep-blijft-sponsor-san/
2012-10-26 16:35:21,407 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/pg-overtreft-verwachtingen/
2012-10-26 16:35:21,443 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/discovery-networks-introduceert-discovery-client-productions/
2012-10-26 16:35:21,479 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/stoelendans-bij-wehkamp.nl/
2012-10-26 16:35:21,563 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/amazon-duikt-in-rode-cijfers/
2012-10-26 16:35:21,599 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/linkedin-rabobank-meest-populaire-werkgever/
2012-10-26 16:35:21,683 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/veronica-uitgeverij-wil-naar-amsterdam/

为什么所有的日志文件都一样?如果它们是不同的文件?!

干杯。

【问题讨论】:

@Lanaru 你是对的,按照你说的做为我解决了这个问题。 太棒了,我发表了我的评论作为答案。请接受它以解决此问题。谢谢! 【参考方案1】:

看起来它使用loggerName 获取记录器,并且始终设置为"fetchers"。因此,您在任何地方都使用相同的记录器,这解释了为什么输出相同。

【讨论】:

【参考方案2】:

您的记录器都称为“提取器”(因为您没有提供loggername 参数),因此您会在所有日志中看到相同的消息。

我建议为您的记录器添加一个名称,也许使用filters。

您可以将其应用于您的代码,如下所示:

logger = Log(os.path.basename(__file__), loggername='test')

def __init__(self, filename, loggername="fetchers", path="logs/", formater="%(asctime)s %(levelname)s %(message)s"):
    self.logger = logging.getLogger(loggername)
    self.hdlr = logging.FileHandler(path + os.path.splitext(filename)[0] + ".log")
    self.hdlr.addFilter(logging.Filter(name=loggername))
    ...

【讨论】:

这个问题也解决了,你和 Lanaru 发布了答案,因为我不能同时接受,所以我接受了第一个,尽管我个人都接受了 ;-) 谢谢! @BenMezger 不用担心,很高兴为您提供帮助! :)

以上是关于Python 日志记录 - 不同的日志但相同的输出的主要内容,如果未能解决你的问题,请参考以下文章

Python 中更优雅的日志记录方案

Python-subprocess执行命令并将输出劫持实现实时记录到日志

Python日志输出——logging模块

Python3之logging模块浅析

Python3 - Loguru 相见恨晚的输出日志工具

Python中的多行日志记录