From e3982c064d76e6ab174aa9b2a7112b361b597f4d Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Wed, 8 Jan 2025 08:56:32 -0500 Subject: [PATCH] feat: add proper logging --- commands/voice/utils.py | 4 +++- core.py | 8 ++++---- events.py | 3 ++- main.py | 11 +++++++++++ tasks.py | 7 +++++-- utils.py | 7 ++++--- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/commands/voice/utils.py b/commands/voice/utils.py index 9ff0a49..9537c7f 100644 --- a/commands/voice/utils.py +++ b/commands/voice/utils.py @@ -1,3 +1,5 @@ +from logging import error + import disnake import utils @@ -6,7 +8,7 @@ from state import client, players def play_after_callback(e, message, once): if e: - print(f"player error: {e}") + error(f"player error: {e}") if not once: play_next(message) diff --git a/core.py b/core.py index 420bebe..3cce118 100644 --- a/core.py +++ b/core.py @@ -6,6 +6,7 @@ import io import textwrap import time import traceback +from logging import debug import disnake import disnake_paginator @@ -60,7 +61,7 @@ async def on_message(message, edited=False): end = time.time() if __debug__: - print(f"reloaded {len(reloaded_modules)} in {round(end-start, 2)}s") + debug(f"reloaded {len(reloaded_modules)} modules in {round(end-start, 2)}s") await utils.add_check_reaction(message) case C.EXECUTE if message.author.id in constants.OWNERS: @@ -132,12 +133,11 @@ async def on_message(message, edited=False): case C.FAST_FORWARD: await commands.voice.fast_forward(message) except Exception as e: - formatted_exception = "".join(traceback.format_exception(e)) - print(formatted_exception) await utils.reply( message, - f"exception occurred while processing command: ```\n{formatted_exception.replace("`", "\\`")}```", + f"exception occurred while processing command: ```\n{"".join(traceback.format_exception(e)).replace("`", "\\`")}```", ) + raise e async def on_voice_state_update(_, before, after): diff --git a/events.py b/events.py index f787d0f..460cf05 100644 --- a/events.py +++ b/events.py @@ -1,6 +1,7 @@ import asyncio import threading import time +from logging import info import commands import core @@ -39,7 +40,7 @@ async def on_message_edit(before, after): async def on_ready(): - print(f"logged in as {client.user} in {round(time.time() - start_time, 1)}s") + info(f"logged in as {client.user} in {round(time.time() - start_time, 1)}s") async def on_voice_state_update(member, before, after): diff --git a/main.py b/main.py index 1e7d5e1..4c74997 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,18 @@ +import logging + import constants import events from state import client if __name__ == "__main__": + logging.basicConfig( + format=( + "%(asctime)s %(levelname)s %(name):%(module)s %(message)s" + if __debug__ + else "%(asctime)s %(levelname)s %(message)s" + ), + datefmt="%Y-%m-%d %T", + level=logging.DEBUG if __debug__ else logging.INFO, + ) events.prepare() client.run(constants.SECRETS["TOKEN"]) diff --git a/tasks.py b/tasks.py index ec7d862..6c6641b 100644 --- a/tasks.py +++ b/tasks.py @@ -1,5 +1,6 @@ import asyncio import time +from logging import debug, error import disnake @@ -7,6 +8,8 @@ from state import client, idle_tracker, players async def cleanup(): + debug("spawned cleanup thread") + while True: await asyncio.sleep(3600 * 12) @@ -17,7 +20,7 @@ async def cleanup(): for target in targets: del players[target] if __debug__: - print(f"cleanup removed {len(targets)} empty players") + debug(f"cleanup removed {len(targets)} empty players") if ( not idle_tracker["is_idle"] @@ -27,4 +30,4 @@ async def cleanup(): await client.change_presence(status=disnake.Status.idle) idle_tracker["is_idle"] = True except Exception as e: - print(f"failed to change status to idle: {e}") + error(f"failed to change status to idle: {e}") diff --git a/utils.py b/utils.py index 29096c5..299e1fe 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,5 @@ import os +from logging import error, info, warning import disnake @@ -103,13 +104,13 @@ def filter_secrets(text: str) -> str: def load_opus(): - print("opus wasn't automatically loaded! trying to load manually...") + warning("opus wasn't automatically loaded! trying to load manually...") for path in ["/usr/lib64/libopus.so.0", "/usr/lib/libopus.so.0"]: if os.path.exists(path): try: disnake.opus.load_opus(path) - print(f"successfully loaded opus from {path}") + info(f"successfully loaded opus from {path}") return except Exception as e: - print(f"failed to load opus from {path}: {e}") + error(f"failed to load opus from {path}: {e}") raise Exception("could not locate working opus library")