我写了一个类并尝试使用python日志记录,但遇到了错误。 基本上,我在python doc:https://docs.python.org/2/howto/logging.html
使用代码引用class HDialog(QtGui.QDialog):
def __init__(self, parent=None):
self.logger = None
test_function() # This is the problematic call
self.setup_logger()
# test_function() should be called here
...
def setup_logger(self):
# create logger
self.logger = logging.getLogger('gui')
self.logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s: %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
self.logger.addHandler(ch)
# 'application' code
self.logger.debug('debug message')
def test_function(self):
self.logger.debug('Test debug')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_dialog = HDialog()
main_dialog.show()
sys.exit(app.exec_())
'调试消息'输出结果,但对于'测试调试'它给了我错误:
AttributeError:' NoneType'对象没有属性' debug'
我感觉setup_logger()中的self.logger是该函数的本地文件 如何在python中以正确的方式解决这个问题?