Python日志在stdout中显示多次 [英] Python Logs displaying multiple times in stdout
问题描述
我目前有两节课.子类和超类.我所有的超类所做的就是线程化子类中的方法.子类的日志已损坏,并被告知要对其进行修复.现在,日志已经井然有序,但是日志将以某种格式和无格式重新打印.我整天都在努力,无法解决该问题.任何和所有建设性的帮助,将不胜感激.
I currently have two classes. A subclass and a superclass. All my superclass does is thread the methods in the subclass. The subclass's logs were out of order, and I was told to fix it. The logs are now in order, but the log reprints with a format and without format. I've been working on this all day and can't figure out how to fix it. Any and all constructive help would be appreciated.
这是在子类中...
# -----------
self.component_logger = logging.getLogger(self.__class__.__name__)
self.stream_handler = logging.StreamHandler(sys.stdout)
self.buffered_handler = logging.handlers.MemoryHandler(500000, flushLevel='ERROR', target=self.stream_handler)
self.component_logger.addHandler(self.stream_handler)
self.component_logger.addHandler(self.buffered_handler)
self.buffered_handler.setFormatter(logging.Formatter("[%(threadName)s] %(levelname)s - %(asctime)s [%(lineno)d]: %(message)s"))
# -----------
我在超类中调用flush
I call flush in the super class
for component in components:
t = threading.Thread(target=threaded_start, name=component.component_id, args=(component, exceptions_queue))
t.start()
threads.append(t)
for t in threads:
t.join(timeout=60)
for component in components:
component.buffered_handler.flush()
#Reset log formatting
logging.getLogger().handlers[0].setFormatter(logging.Formatter("%(levelname)s - %(asctime)s [%(lineno)d]: %(message)s"))
#Gather results and reformat logs
logging.info("Finished threaded starts.")
目前仍在像这样打印...(一般示例)
Currently still printing like this... (general example)
Starting l2-ilogin active
Starting l2-ilogin active
[L2-0] DEBUG - 2015-06-03 14:58:18,129 [57]: Starting l2-ilogin active
Starting l2-mgmt active
Starting l2-mgmt active
[L2-0] DEBUG - 2015-06-03 14:58:18,129 [57]: Starting l2-mgmt active
前两个是重复的,第三个是格式化的版本(这是输出).
Where the first two are repeats and the third is the formatted version (this is the output).
推荐答案
另一种方法:尝试将setFormatter也设置为stream_handler – alfasin 18小时前
Another shot: try to setFormatter to the stream_handler as well – alfasin 18 hours ago
感谢,它可用于格式化重复的输入.我最终将self.component_logger.propogate设置为False来停止输出的重复.谢谢@alfasin.现在唯一的问题是组织子流程的输出,我应该能够做到.谢谢! –伊恩45分钟前
Thanks, it worked for formatting the duplicated input. I ended up setting self.component_logger.propogate to False to stop the duplication of output. Thanks @alfasin . Now the only problem is organizing the output from sub processes and I should be able to do that. Thanks! – Ian 45 mins ago
self.component_logger = logging.getLogger(self.__class__.__name__)
self.stream_handler = logging.StreamHandler(sys.stdout)
self.buffered_handler = logging.handlers.MemoryHandler(500000, flushLevel='ERROR', target=self.stream_handler)
self.stream_handler.setFormatter((logging.Formatter("[%(threadName)s] %(levelname)s - %(asctime)s [%(lineno)d]: %(message)s")))
# self.component_logger.addHandler(self.stream_handler)
self.component_logger.addHandler(self.buffered_handler)
self.component_logger.propagate = False
这篇关于Python日志在stdout中显示多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!