From e7d4a9ee43719c15d3982c9048bf9029a13b33f6 Mon Sep 17 00:00:00 2001 From: Romain J Date: Mon, 16 Dec 2019 18:19:32 +0100 Subject: [PATCH] fix(logging): fix bot-log sending events --- bot.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 0c6d01c..dc97a0d 100755 --- a/bot.py +++ b/bot.py @@ -3,6 +3,7 @@ import logging import sys from collections import deque, Counter from typing import List +import contextlib import aiohttp import discord @@ -157,6 +158,32 @@ class TuxBot(commands.AutoShardedBot): 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__": log = logging.getLogger() @@ -164,6 +191,7 @@ if __name__ == "__main__": bot = TuxBot() try: - bot.run() + with setup_logging(): + bot.run() except KeyboardInterrupt: bot.close()