我有以下的测试脚本,它模仿我通常设置扩展的记录:实例的“RootLogger”没有“痕迹”成员(但有些类型无法推断)
import logging
TRACE_LL = 25
TRACE_LSTR = 'TRACE'
LOG_FORMATTER = '%(asctime)s - %(levelname)-10s - %(message)s'
class MyLogger(logging.Logger):
def __init__(self, log_name):
logging.Logger.__init__(self, log_name)
self.setLevel(TRACE_LL)
hdlr = logging.StreamHandler()
formatter = logging.Formatter(LOG_FORMATTER)
hdlr.setFormatter(formatter)
self.addHandler(hdlr)
def trace(self, txt, *args, **kwargs):
self.log(TRACE_LL, txt, *args, **kwargs)
def getlog(name):
return logging.getLogger(name)
def setup():
logging.setLoggerClass(MyLogger)
logging.addLevelName(TRACE_LL, TRACE_LSTR)
setup()
log = getlog('mylog')
log.trace('Trace this')
运行这种按预期工作:
2014-01-07 07:22:59,982 - TRACE - Trace this
但运行在这个pylint
引起麻烦:
» pylint -E getlog_test.py
No config file found, using default configuration
************* Module getlog_test
E: 29,0: Instance of 'RootLogger' has no 'trace' member (but some types could not be inferred)
我在代码库中收到了数百条这些消息,因为我正在广泛使用日志记录。
我该如何解决pylint错误?
作为一种替代方法,禁用它也是足够的,但仅限于RootLogger
实例:我仍然想知道代码的其他部分是否存在此问题。
谢谢,我正在测试这个。我的问题的错误代码是'E1103'('pylint --list-msgs | grep“,但某些类型无法推断出来)'') – dangonfast
@gonvaled谢谢。我更新了错误号并添加了一个链接到pylint的这个功能的文档。 – John1024
我测试过了。我可以在命令行中使用'--disable E1103'来禁用它,但是不能为受影响的模块(我的记录器模块)选择性地禁用它,我需要使用我的记录器使用'#pylint:disable = E1103',这是不可行的,并且还会掩盖与“RootLogger”无关的其他错误。因此,目前,使用命令行“--disable”标志,错误消息被全局禁用,对于'RootLogger' **和**其他受影响的类,我想避免。但我想我暂时不得不忍受这一点。 – dangonfast