fix(logging): fix bot-log sending events

This commit is contained in:
Romain J 2019-12-16 18:19:32 +01:00
parent f42b2194cd
commit e7d4a9ee43

30
bot.py
View file

@ -3,6 +3,7 @@ import logging
import sys import sys
from collections import deque, Counter from collections import deque, Counter
from typing import List from typing import List
import contextlib
import aiohttp import aiohttp
import discord import discord
@ -157,6 +158,32 @@ class TuxBot(commands.AutoShardedBot):
super().run(self.config.get("bot", "Token"), reconnect=True) super().run(self.config.get("bot", "Token"), reconnect=True)
@contextlib.contextmanager
def setup_logging():
try:
logging.getLogger('discord').setLevel(logging.INFO)
logging.getLogger('discord.http').setLevel(logging.WARNING)
log = logging.getLogger()
log.setLevel(logging.INFO)
handler = logging.FileHandler(filename='logs/tuxbot.log',
encoding='utf-8', mode='w')
fmt = logging.Formatter('[{levelname:<7}] [{asctime}]'
' {name}: {message}',
'%Y-%m-%d %H:%M:%S', style='{')
handler.setFormatter(fmt)
log.addHandler(handler)
yield
finally:
handlers = log.handlers[:]
for handler in handlers:
handler.close()
log.removeHandler(handler)
if __name__ == "__main__": if __name__ == "__main__":
log = logging.getLogger() log = logging.getLogger()
@ -164,6 +191,7 @@ if __name__ == "__main__":
bot = TuxBot() bot = TuxBot()
try: try:
bot.run() with setup_logging():
bot.run()
except KeyboardInterrupt: except KeyboardInterrupt:
bot.close() bot.close()