mirror of
https://github.com/UiO-Gaming/ShiteMusicBot.git
synced 2026-11-05 13:56:51 +01:00
Add colors and formatting to bot log, remove print statements
This commit is contained in:
1 parent
68b1b6d888
commit
897c0cc987
4 files changed
+52
-20
No files matched your search
@@ -86,9 +86,11 @@ 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.info("")
|
||||
if self.user:
|
||||
self.logger.info(f'Logged in as: {self.user.name}' +
|
||||
f' in {len(self.guilds)} servers.')
|
||||
self.logger.info(f'Version: {discord.__version__}\n')
|
||||
self.logger.debug("Bot Ready\n\n\n")
|
||||
|
||||
self.session = aiohttp.ClientSession(loop=self.loop)
|
||||
@@ -102,7 +104,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):
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+37
-11
@@ -3,8 +3,33 @@ 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
|
||||
|
||||
@@ -14,25 +39,26 @@ class BotLogger(object):
|
||||
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)
|
||||
@@ -1,6 +1,7 @@
|
||||
# Discord Packages
|
||||
from lavalink import AudioTrack
|
||||
|
||||
import logging
|
||||
from collections import OrderedDict, deque
|
||||
from itertools import chain, cycle, islice
|
||||
from random import shuffle
|
||||
@@ -28,6 +29,8 @@ class MixQueue:
|
||||
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):
|
||||
tmp = ''
|
||||
@@ -91,9 +94,9 @@ class MixQueue:
|
||||
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:
|
||||
next_track = self.queues[self.first_queue].pop(0)
|
||||
|
||||
Reference in new issue
Block a user