python stdout timeout_Python日志在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.

This is inside of the subclass...

# -----------

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"))

# -----------

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).

解决方案

Another shot: try to setFormatter to the stream_handler as well – alfasin 18 hours ago

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值