Try to make log collector thread safe

This commit is contained in:
Tulir Asokan 2020-04-05 16:39:33 +03:00
parent 92bfe37436
commit 8409b2a5bb
2 changed files with 7 additions and 5 deletions

View File

@ -44,17 +44,17 @@ config.update()
logging.config.dictConfig(copy.deepcopy(config["logging"]))
loop = asyncio.get_event_loop()
stop_log_listener = None
if config["api_features.log"]:
from .management.api.log import init as init_log_listener, stop_all as stop_log_listener
init_log_listener()
init_log_listener(loop)
log = logging.getLogger("maubot.init")
log.info(f"Initializing maubot {__version__}")
loop = asyncio.get_event_loop()
init_zip_loader(config)
db_engine = init_db(config)
clients = init_client_class(loop)

View File

@ -38,6 +38,7 @@ class LogCollector(logging.Handler):
lines: Deque[dict]
formatter: logging.Formatter
listeners: List[web.WebSocketResponse]
loop: asyncio.AbstractEventLoop
def __init__(self, level=logging.NOTSET) -> None:
super().__init__(level)
@ -69,7 +70,7 @@ class LogCollector(logging.Handler):
for name, value in content.items():
if isinstance(value, datetime):
content[name] = value.astimezone().isoformat()
asyncio.ensure_future(self.send(content))
asyncio.run_coroutine_threadsafe(self.send(content), loop=self.loop)
self.lines.append(content)
async def send(self, record: dict) -> None:
@ -86,8 +87,9 @@ log = logging.getLogger("maubot.server.websocket")
sockets = []
def init() -> None:
def init(loop: asyncio.AbstractEventLoop) -> None:
log_root.addHandler(handler)
handler.loop = loop
async def stop_all() -> None: