我从事一个开源项目,在该项目中,我们曾允许所有模块登录根记录器:
import logging
def my_function():
logging.info('Logging something on the root- logger')
我们正在转向使用每个模块的记录器,比如:
import logging
_LOGGER = logging.getLogger(__name__)
def my_function():
_LOGGER.info('Logging something on the root- logger')
有没有办法通过Linter/Static检查来强制执行此策略,这样其他人就不会登录根记录器了?
如果你想找到所有发生这种情况的情况,那么静态检查绝对不可能做到这一点。您所能达到的最佳效果基本上是在源代码中查找字符串logging.[info|debug|...]
。静态检查器很容易被这样的东西愚弄:
logger_name = 'root' # might even read this from a config file
_LOGGER = logging.getLogger(logger_name) # static check has no way to know here that we get the root logger
_LOGGER.info('logging this to root')
除此之外,如果您没有在模块级记录器上将propagate
设置为false,它们仍然会将日志传播到根记录器。