我试图通过代码工作三个输出来获得模块级别的日志记录:文件,控制台和应用程序内部(QTextEdit)。
我可以让所有三个记录器使用下面的代码,但应用程序内部记录器不记录所有事件,而控制台记录器(仅)打印每行两次。
我尝试过使用
logging.getLogger(__name__)
用于文件记录器而不是root(没有生成日志),对于控制台是相同的(每个日志输出只有1行正常工作)和MyLogHandler(没有生成日志)相同,并尝试了根记录器和&的各种组合#39;名称'记录器,但不能使所有日志工作,控制台每个日志事件只打印一行。
def configCodeRootExample_(self):
logFileName = self.getLogLocation()
rootLogger = logging.getLogger('')
#This logger works
fileLogger = logging.FileHandler(logFileName)
fileLogger.setLevel(logging.INFO)
fileFormatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
fileLogger.setFormatter(fileFormatter)
rootLogger.addHandler(fileLogger)
#This logger works but prints output twice
consoleFormatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s')
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(consoleFormatter)
rootLogger.addHandler(console)
#This logger works but only logs a subset of DEBUG events and no INFO events
myLogHandler = GSLLogHandler()
myLogHandler.setLevel(logging.DEBUG)
myLogHandler.setFormatter(fileFormatter)
rootLogger.addHandler(myLogHandler)
此处的记录也是输出到监听QTextEdit的日志处理程序:
import logging
from loggerpackage.logsignals import LogSignals
class MyLogHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
self.logSignals = LogSignals()
def emit(self, logMsg):
logMsg = self.format(logMsg)
self.logSignals.logEventTriggered.emit(logMsg)
如果我将控制台记录器更改为模块级别:
logger = logging.getLogger(__name__)
consoleFormatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s')
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(consoleFormatter)
logger.addHandler(console)
然后每个日志事件只打印一行,但格式不正确,似乎是某种默认格式化程序