我正在设置python日志记录如下:
def setup_logging():
loggers = (logging.getLogger("amcat"), logging.getLogger("scrapers"),logging.getLogger(__name__))
filename = "somefile.txt"
sys.stderr = open(filename, 'a')
handlers = (logging.StreamHandler(sys.stdout),logging.FileHandler(filename))
formatter = AmcatFormatter(date = True)
for handler in handlers:
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
for logger in loggers:
logger.propagate = False
logger.setLevel(logging.INFO)
for handler in handlers:
logger.addHandler(handler)
logging.getLogger().handlers = []
启用了 2 个主要模块记录器,它们都应该记录到控制台以及文件。错误被重定向到文件(理想情况下,错误也会显示在控制台中,但我还没有实现)
紧接着,我检查事情是否正确:
should_work = [
"amcat.scraping.scraper",
"amcat.scraping.htmltools",
"amcat.scraping.controller",
"__main__"]
loggerdict = logging.Logger.manager.loggerDict #all loggers
for name, logger in loggerdict.items():
if name in should_work:
print("\nlogger: "+name)
#iterate through parents see if effective handlers are set correctly
print(effectivehandlers(logger))
#test logger
logger.info("test for {name}".format(**locals()))
def effectivehandlers(logger):
handlers = logger.handlers
while True:
logger = logger.parent
handlers.extend(logger.handlers)
if not (logger.parent and logger.propagate):
break
return handlers
控制台输出:
logger: __main__
[<logging.StreamHandler object at 0x3425d50>, <logging.FileHandler object at 0x3425dd0>]
[2013-10-24 10:27:30 daily.py:133 INFO] test for __main__
logger: amcat.scraping.controller
[<logging.StreamHandler object at 0x3425d50>, <logging.FileHandler object at 0x3425dd0>]
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.controller
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.controller
logger: amcat.scraping.htmltools
[<logging.StreamHandler object at 0x3425d50>, <logging.FileHandler object at 0x3425dd0>]
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.htmltools
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.htmltools
logger: amcat.scraping.scraper
[<logging.StreamHandler object at 0x3425d50>, <logging.FileHandler object at 0x3425dd0>]
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.scraper
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.scraper
这是文件输出:
[2013-10-24 10:27:30 daily.py:133 INFO] test for __main__
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.controller
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.controller
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.htmltools
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.htmltools
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.scraper
[2013-10-24 10:27:30 daily.py:133 INFO] test for amcat.scraping.scraper
如您所见,尽管禁用了传播并确保不存在重复的处理程序,但输出是双倍的。这哪里出错了?