Reputation: 695
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).
Upvotes: 1
Views: 295
Reputation: 695
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
Upvotes: 1