diff --git a/bot.py b/bot.py index 1a3e4db..70d1351 100644 --- a/bot.py +++ b/bot.py @@ -86,10 +86,14 @@ class Bot(commands.Bot): except Exception: self.logger.exception("Loading of extension %s failed" % extension) - print(f'\nLogged in as: {self.user.name}' + - f' in {len(self.guilds)} servers.') - print(f'Version: {discord.__version__}\n') - self.logger.debug("Bot Ready\n\n\n") + if self.user: + info = f'Logged in as: {self.user.name} in {len(self.guilds)} servers.' + border = "="*len(info) + self.logger.info(border) + self.logger.info(info) + self.logger.info(f'Version: {discord.__version__}') + self.logger.info(border) + self.logger.debug("Bot Ready") self.session = aiohttp.ClientSession(loop=self.loop) await self.change_presence(activity=discord.Game(type=0, @@ -102,7 +106,7 @@ class Bot(commands.Bot): except Exception as e: tb = e.__traceback__ traceback.print_tb(tb) - print(e) + self.logger.error(e) def run_bot(datadir, debug: bool = False): diff --git a/musicbot/cogs/helpformatter/helpformatter.py b/musicbot/cogs/helpformatter/helpformatter.py index 62ec5b0..db49186 100644 --- a/musicbot/cogs/helpformatter/helpformatter.py +++ b/musicbot/cogs/helpformatter/helpformatter.py @@ -57,8 +57,9 @@ async def coghelper(ctx, cog, ignore_subcommands=True): if cmd_dict: paginator.add_command_field(cmd_dict) except Exception as e: - print(e) - print("Help failed for command: ", cmd) + logger = ctx.bot.logger.getChild("helpformat") + logger.exception(e) + logger.error("Help failed for command: ", cmd) paginator.add_page_indicator(ctx.localizer, "{pageindicator}", _prefix=ctx.prefix) return paginator diff --git a/musicbot/utils/logger.py b/musicbot/utils/logger.py index 9a921b0..bd718fe 100644 --- a/musicbot/utils/logger.py +++ b/musicbot/utils/logger.py @@ -3,36 +3,59 @@ import logging.handlers import os +class ConsoleFormatter(logging.Formatter): + + def format(self, record): + gray = "\x1b[38;20m" + green = "\x1b[32;20m" + yellow = "\x1b[33;20m" + blue = "\x1b[34;20m" + red = "\x1b[31;20m" + reset = "\x1b[0m" + match record.levelno: + case logging.DEBUG: + color = yellow + case logging.INFO: + color = green + case logging.ERROR: + color = red + case _: + color = gray + format = blue + "[%(asctime)s] " + reset + "%(name)-20s" + \ + "[" + color + "%(levelname)-8s" + reset + "] " + "%(message)s" + formatter = logging.Formatter(format) + return formatter.format(record) + + class BotLogger(object): def __init__(self, debug=None, data_location=None): self.log_location = data_location - self.log_level = debug - if self.log_level: + if debug: self.log_level = logging.DEBUG - else: self.log_level = logging.INFO - self.bot_logger = logging.getLogger("logger") + self.bot_logger = logging.getLogger("musicbot") self.bot_logger.setLevel(logging.DEBUG) - log_formatter = logging.Formatter("%(asctime)s:%(name)s:%(levelname)s:%(message)s") + console_formatter = ConsoleFormatter() + file_format = logging.Formatter("[%(asctime)s] %(name)-20s[%(levelname)-8s]%(message)s") + + console_handler = logging.StreamHandler() + console_handler.setLevel(self.log_level) + console_handler.setFormatter(console_formatter) + + self.bot_logger.addHandler(console_handler) if self.log_location is not None: if not os.path.isdir(self.log_location): os.makedirs(self.log_location) - print(f"Creating logging directory: {self.log_location}") + self.bot_logger.info(f"Creating logging directory: {self.log_location}") if self.log_level: - print(f"Logging directory: {self.log_location}") + self.bot_logger.info(f"Logging directory: {self.log_location}") file_logger = logging.handlers.RotatingFileHandler(f'{self.log_location}/bot.log', mode='a', maxBytes=5000, encoding="UTF-8", delay=0, backupCount=5) file_logger.setLevel(self.log_level) - file_logger.setFormatter(log_formatter) + file_logger.setFormatter(file_format) self.bot_logger.addHandler(file_logger) - - console_handler = logging.StreamHandler() - console_handler.setLevel(self.log_level) - console_handler.setFormatter(log_formatter) - - self.bot_logger.addHandler(console_handler) diff --git a/musicbot/utils/mixplayer/mixqueue.py b/musicbot/utils/mixplayer/mixqueue.py index 305bf61..73c2a19 100644 --- a/musicbot/utils/mixplayer/mixqueue.py +++ b/musicbot/utils/mixplayer/mixqueue.py @@ -1,3 +1,4 @@ +import logging from collections import OrderedDict, deque from itertools import chain, cycle, islice from random import shuffle @@ -29,6 +30,8 @@ class MixQueue(Generic[T]): self._history = deque(maxlen=11) # 10 + current self.looping = False self.loop_offset = 0 + self.logger = logging.getLogger("musicbot").getChild("Queue") + self.logger = self.logger.getChild("Queue") def __str__(self) -> str: tmp = '' @@ -93,9 +96,9 @@ class MixQueue(Generic[T]): self._history.append(next_track) return next_track except IndexError as e: - print(e) - print(self.loop_offset) - print(len(self)) + self.logger.error(e) + self.logger.error(self.loop_offset) + self.logger.error(len(self)) self.loop_offset = min(self.loop_offset, len(self)-1) else: if first_queue := self.first_queue: