mirror of
https://github.com/fishyboteso/fishyboteso.git
synced 2024-08-30 18:32:13 +00:00
76e17c4502
logger is connected to gui after gui is ready moved all logging configuration to log_config file
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
import logging
|
|
from logging import StreamHandler, Formatter
|
|
|
|
from fishy.helper.config import config
|
|
|
|
|
|
class GuiLogger(StreamHandler):
|
|
def __init__(self):
|
|
StreamHandler.__init__(self)
|
|
|
|
self.renderer = None
|
|
self._temp_buffer = []
|
|
|
|
formatter = Formatter('%(levelname)s - %(message)s')
|
|
self.setFormatter(formatter)
|
|
logging_config = {"comtypes": logging.INFO,
|
|
"PIL": logging.INFO,
|
|
"urllib3": logging.WARNING,
|
|
"": logging.DEBUG}
|
|
for name, level in logging_config.items():
|
|
_logger = logging.getLogger(name)
|
|
_logger.setLevel(level)
|
|
self.setLevel(logging.DEBUG if config.get("debug", False) else logging.INFO)
|
|
logging.getLogger("").addHandler(self)
|
|
|
|
def emit(self, record):
|
|
msg = self.format(record)
|
|
if self.renderer:
|
|
self.renderer(msg)
|
|
else:
|
|
self._temp_buffer.append(msg)
|
|
|
|
def connect(self, gui):
|
|
self.renderer = lambda m: gui.call_in_thread(lambda: gui.write_to_console(m))
|
|
while self._temp_buffer:
|
|
self.renderer(self._temp_buffer.pop(0))
|