feat: add proper logging

This commit is contained in:
Ryan 2025-01-08 08:56:32 -05:00
parent d56bac1b2f
commit e3982c064d
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
6 changed files with 29 additions and 11 deletions

View File

@ -1,3 +1,5 @@
from logging import error
import disnake import disnake
import utils import utils
@ -6,7 +8,7 @@ from state import client, players
def play_after_callback(e, message, once): def play_after_callback(e, message, once):
if e: if e:
print(f"player error: {e}") error(f"player error: {e}")
if not once: if not once:
play_next(message) play_next(message)

View File

@ -6,6 +6,7 @@ import io
import textwrap import textwrap
import time import time
import traceback import traceback
from logging import debug
import disnake import disnake
import disnake_paginator import disnake_paginator
@ -60,7 +61,7 @@ async def on_message(message, edited=False):
end = time.time() end = time.time()
if __debug__: 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) await utils.add_check_reaction(message)
case C.EXECUTE if message.author.id in constants.OWNERS: 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: case C.FAST_FORWARD:
await commands.voice.fast_forward(message) await commands.voice.fast_forward(message)
except Exception as e: except Exception as e:
formatted_exception = "".join(traceback.format_exception(e))
print(formatted_exception)
await utils.reply( await utils.reply(
message, 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): async def on_voice_state_update(_, before, after):

View File

@ -1,6 +1,7 @@
import asyncio import asyncio
import threading import threading
import time import time
from logging import info
import commands import commands
import core import core
@ -39,7 +40,7 @@ async def on_message_edit(before, after):
async def on_ready(): 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): async def on_voice_state_update(member, before, after):

11
main.py
View File

@ -1,7 +1,18 @@
import logging
import constants import constants
import events import events
from state import client from state import client
if __name__ == "__main__": 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() events.prepare()
client.run(constants.SECRETS["TOKEN"]) client.run(constants.SECRETS["TOKEN"])

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
import time import time
from logging import debug, error
import disnake import disnake
@ -7,6 +8,8 @@ from state import client, idle_tracker, players
async def cleanup(): async def cleanup():
debug("spawned cleanup thread")
while True: while True:
await asyncio.sleep(3600 * 12) await asyncio.sleep(3600 * 12)
@ -17,7 +20,7 @@ async def cleanup():
for target in targets: for target in targets:
del players[target] del players[target]
if __debug__: if __debug__:
print(f"cleanup removed {len(targets)} empty players") debug(f"cleanup removed {len(targets)} empty players")
if ( if (
not idle_tracker["is_idle"] not idle_tracker["is_idle"]
@ -27,4 +30,4 @@ async def cleanup():
await client.change_presence(status=disnake.Status.idle) await client.change_presence(status=disnake.Status.idle)
idle_tracker["is_idle"] = True idle_tracker["is_idle"] = True
except Exception as e: except Exception as e:
print(f"failed to change status to idle: {e}") error(f"failed to change status to idle: {e}")

View File

@ -1,4 +1,5 @@
import os import os
from logging import error, info, warning
import disnake import disnake
@ -103,13 +104,13 @@ def filter_secrets(text: str) -> str:
def load_opus(): 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"]: for path in ["/usr/lib64/libopus.so.0", "/usr/lib/libopus.so.0"]:
if os.path.exists(path): if os.path.exists(path):
try: try:
disnake.opus.load_opus(path) disnake.opus.load_opus(path)
print(f"successfully loaded opus from {path}") info(f"successfully loaded opus from {path}")
return return
except Exception as e: 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") raise Exception("could not locate working opus library")