matrix-bot/matrix_bot/utils.py

31 lines
719 B
Python
Raw Normal View History

2020-08-05 23:13:23 +02:00
import asyncio
import logging
from .config import Config
def run_async(future: asyncio.Future) -> None:
global logger
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(future)
except asyncio.exceptions.CancelledError:
loop.run_until_complete(asyncio.sleep(0.25))
finally:
loop.stop()
def setup_logger(name: str = __name__) -> logging.Logger:
logger = logging.getLogger(name)
logger.setLevel(Config.LOGLEVEL)
ch = logging.StreamHandler()
ch.setLevel(Config.LOGLEVEL)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger