来自多个模块的Python日志记录问题 [英] Python logging issues from multiple modules
问题描述
我有3个python模块.
I have 3 python modules.
LogManager.py
Runner.py
Other.py
Runner.py
是事件链中的第一个主要模块,并且从该模块中调用Other.py
内部的功能.
Runner.py
is the first main module in the chain of events, and from that module functions inside Other.py
are called.
因此,在Runner.py
内部,我有一个对LogManager.py
So, inside Runner.py
I have a function call to the LogManager.py
logger = LogManager.get_log()
从那里,我可以制作简单的日志,例如logger.critical("OHNOES")
and from there, I can make simple logs, e.g. logger.critical("OHNOES")
我想要get_log
函数要做的事情类似于单例模式,如果未设置记录器,它将设置记录器并返回它.否则,它将返回记录器.
What I WANT the get_log
function to do, is something similar to a singleton pattern, where if the logger has not been set up, it will set up the logger and return it. Else, it will just return the logger.
LogManager.py的内容:
Contents of LogManager.py:
import logging
def get_log():
logger = logging.getLogger('PyPro')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('pypro.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)
# create formatter and add it to the handlers
fhFormatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
chFormatter = logging.Formatter('%(levelname)s - %(filename)s - Line: %(lineno)d - %(message)s')
fh.setFormatter(fhFormatter)
ch.setFormatter(chFormatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
logger.info("-----------------------------------")
logger.info("Log system successfully initialised")
logger.info("-----------------------------------")
return logger
如您所见,LogManager.get_log()将在每次调用日志时尝试设置日志.真的,我对正在发生的事情有些困惑...
As you can see, LogManager.get_log() will attempt to set up a log each time it is called. Really, I am a bit confused as to exactly what is happening...
Runner.py在其main方法中调用get_log函数. Other.py在全局范围内调用get_log(在导入后立即调用,而不是在任何函数中)
Runner.py calls the get_log function in it's main method. Other.py calls the get_log in the global scope (right after imports, not in any function)
结果是,我所做的所有日志都记录了两次,因为为记录器创建了两次处理程序.
The result is that all of the logs I make are logged twice, as handlers are made twice for the logger.
使get_log函数以其他方式返回同一日志的实例的最简单方法是什么?
What is the simplest way that I am missing to make the get_log function to return an instance of the same log elsewise?
推荐答案
logging
模块已经为您实现了单例模式-当您调用logger.getLogger(name)
时,它将创建记录器(如果尚未创建)并退回.尽管并不是您要的完全是 ,但我建议您将get_log()
重命名为setup_log()
,因为这样做是正确的.然后,您只需在代码开头调用一次setup_log()
.然后,当您实际需要记录器时,只需使用logging.getLogger()
,它将返回已配置的记录器.
The logging
module already implements a singleton pattern for you - when you call logger.getLogger(name)
, it will create the logger if it hasn't done so already and return it. Although it's not exactly what you're asking for, I would suggest just renaming get_log()
to setup_log()
, since that's what it does. Then you can just call setup_log()
once, at the beginning of your code. Afterwards, when you actually need the logger, just use logging.getLogger()
and it will return the already-configured logger.
这篇关于来自多个模块的Python日志记录问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!