设置跨包模块日志的有效方法 [英] Efficient way of setting Logging across a Package Module
问题描述
我有一个包含多个组件的软件包,使用日志记录和输出有用信息会大大受益.
我不想做的是为每个单独的文件设置"正确的日志记录,并按以下方式进行操作:
import logging
logging.basicConfig(level=DEBUG)
my_function = logging.getLogger("my_function")
my_class = logging.getLogger("my_class")
我尝试了几种方法,其中一种是将样板代码添加到实用程序模块中的类中,然后尝试执行以下操作:
from util import setlogging
set_logging()
但是,即使上述解决方案对我来说也不是很干净,并且会引起问题,因为setLogger没有__call__
方法.我喜欢的是,我的"set_logging"类将从配置文件中读取并具有一些默认值,因此无论我希望使用哪种级别或哪种日志格式都可以正确设置它.
有没有一种方法可以在我的包裹中初始化全面的日志记录?也许在__init__.py
文件中?
为了尽可能详尽,这就是setlogging(现在是函数,而不是类)的样子:
def setlogging(config=None):
if config == None:
config = config_options() # sets default values
levels = {
'debug': DEBUG,
'info': INFO
}
level = levels.get(config['log_level'])
log_format = config['log_format']
datefmt = config['log_datefmt']
basicConfig(
level = level,
format = log_format,
datefmt = datefmt)
如果您希望包的各个模块中的所有代码都使用同一记录器对象,则只需(使该记录器可用-稍后再介绍) -和)致电
mylogger.warning("Attenzione!")
等,而不是logging.warning
& c.因此,问题减少到为整个程序包创建一个mylogger
对象,并使该对象在程序包中的所有模块中都可用. (或者,可以使用命名记录器,其名称以程序包名称开头,后跟一个点,但这是logging
程序包功能的一部分,但我个人从来没有发现它是一种自然的操作方式.) /p>
因此,您的util.setlogging
函数可以简单地跟在后面,例如
mylogger = logging.getLogger(package_name)
,每个导入util
的模块都可以简单地使用
util.mylogger.warning('Watch out!')
之类的.在我看来,这是最简单的方法,只要适用于包中所有代码都应以相同方式记录的概念即可.
I have a package that has several components in it that would benefit greatly from using logging and outputting useful information.
What I do not want to do is to 'setup' proper logging for every single file with somewhere along these lines:
import logging
logging.basicConfig(level=DEBUG)
my_function = logging.getLogger("my_function")
my_class = logging.getLogger("my_class")
I have tried a couple of approaches, one of them being adding the boilerplate code into a class within a utility module and try and do something like this:
from util import setlogging
set_logging()
But even the above solution doesn't look clean to me and would cause issues because setLogger doesn't have a __call__
method. What I did liked was that my "set_logging" class would read form a config file and have some default values so it wouldn't matter what level or what type of logging format I wanted it would set it up correctly.
Is there a way to initialize proper logging across the board in my package? Maybe in the __init__.py
file?
And just to be as verbose as possible, this is what setlogging (now a function, not a class) looks like:
def setlogging(config=None):
if config == None:
config = config_options() # sets default values
levels = {
'debug': DEBUG,
'info': INFO
}
level = levels.get(config['log_level'])
log_format = config['log_format']
datefmt = config['log_datefmt']
basicConfig(
level = level,
format = log_format,
datefmt = datefmt)
If you want all the code in the various modules of your package to use the same logger object, you just need to (make that logger available -- see later -- and) call
mylogger.warning("Attenzione!")
or the like, rather than logging.warning
&c. So, the problem reduces to making one mylogger
object for the whole package and making it available throughout the modules in the package. (Alternatively, you could used named loggers with names starting with the package's name followed by a dot, but while that's very much a part of the logging
package functionality, I've personally never found it a natural way to operate).
So, your util.setlogging
function could simply be followed by, say,
mylogger = logging.getLogger(package_name)
and every module that imports util
can simply use
util.mylogger.warning('Watch out!')
and the like. This seems to me to be the simplest approach, as long as the concept that all the code in the package should be logging in the same way applies.
这篇关于设置跨包模块日志的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!