我有以下课程:
class Log(object):
# class new
#new is used instead of init because __new__ is able to return (where __init__ can't)
def __new__(self, name, consolelevel, filelevel):
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(name)s: %(message)s')
#Create consolehandler and set formatting (level is set in the ROOT)
consolehandler = StreamHandler()
consolehandler.setFormatter(formatter)
#Create filehandler, set level and set formatting
filehandler = FileHandler(name + '.log')
filehandler.setLevel(filelevel)
filehandler.setFormatter(formatter)
#Create the root logger, add console and file logger. Set the rootlevel == consolelevel.
self.root = logging.getLogger(name)
#causing me problems....
self.root.setLevel(consolelevel)
self.root.addHandler(consolehandler)
self.root.addHandler(filehandler)
self.root.propagate = True
return self.root
# Close the logger object
def close():
# to be implemented
pass
我使用这个类来记录控制台和文件(取决于设置的级别)。问题是根级别似乎是addhandlers的前导。有什么方法可以禁用它吗?现在我将rootlevel设置为与consolelevel相同的级别,但这不起作用。。。在
有什么建议吗?在
提前致谢并致以诚挚的问候
JR公司
这是我正在制作的模块的简化版本。该模块包含几个需要日志记录功能的类。每个类都会记录到一个不同的文件中,并且应该可以在类之间更改文件处理程序级别(例如,gamepad类:控制台.debug以及文件处理程序.info和MQTT类:控制台信息和filehandler.debug). 在
因此,我认为建立一个日志类是最简单的方法。请记住,我通常是做电子学的,但现在和python结合了。所以我的技能很基本。。。。在
来自未来进口部 从操作员导入* 导入日志记录 从日志导入文件处理程序 从日志导入StreamHandler 导入pygame 导入线程 从pygame.locals游戏进口* 进口莫斯基托 导入时间 从时间导入睡眠 导入系统
类ConsoleFileLogger(对象):
^{pr2}$类游戏板():
类MQTTClient():
def main(): logger=ConsoleFileLogger('BaseLogFiles/logControlCenterMain','ControlCenterMain',日志记录.DEBUG, 日志记录.DEBUG, 日志记录.DEBUG).set()
如果名称='main': 主()
我在您的代码中看到的一个问题是,每当您实例化
Log
类时,它会添加更多的处理程序。你可能不想要这个。在请记住,
getLogger
使用相同的参数调用时返回的实例总是相同的,并且基本上它实现了singleton模式。 因此,当您稍后调用addHandler
时,它每次都会添加一个新的处理程序。在处理
logging
的方法是在模块级别创建一个记录器并使用它。在我也会避免使用
__new__
。在您的例子中,您可以使用一个简单的函数。请注意,Log.close
方法无法工作,因为您的__new__
方法不返回Log
实例,因此返回的记录器没有该方法。在关于记录器的级别,我不明白为什么不在
consolehandler
上设置级别,而是在整个记录器上设置级别。在相关问题 更多 >
编程相关推荐