feat(bot): ✨ Add rich logging.

This commit is contained in:
Nyeki.py
2024-10-27 01:30:04 -03:00
parent ec00a557fa
commit 09d2808bd4
6 changed files with 141 additions and 47 deletions
+17 -15
View File
@@ -8,7 +8,7 @@ import sys
import discord
from discord import abc
from discord.ext import commands, tasks
from discord.ext import commands
from discord.ext.commands import errors
from tortoise import expressions, transactions, exceptions as tortoise_exc
@@ -18,7 +18,17 @@ import redis.asyncio as aredis
import asyncpg.exceptions
from payphone.db import setup, models
from payphone.core import constants, exceptions, formatting, help_command, config
from payphone.core import (
constants,
exceptions,
formatting,
help_command,
config,
logging
)
logging.setup()
class Payphone(commands.AutoShardedBot):
@@ -42,6 +52,8 @@ class Payphone(commands.AutoShardedBot):
`shutdown`.
"""
log = logging.logger
redis: aredis.Redis = aredis.Redis(host="localhost" if config.IS_DEBUG else "redis")
def __init__(self) -> None:
@@ -72,7 +84,7 @@ class Payphone(commands.AutoShardedBot):
await self.redis.ping()
print(f"Ready! Logged in as {self.user.display_name}.")
self.log.info("Ready! Logged in as %s." % self.user.display_name)
async def on_shard_ready(self, shard_id: int) -> None:
"""Sets the activity for the shard.
@@ -89,7 +101,7 @@ class Payphone(commands.AutoShardedBot):
shard_id=shard_id,
)
print(f"Shard {shard_id} is ready!")
self.log.info("Shard %s is ready!" % shard_id)
async def on_shard_resumed(self, shard_id: int) -> None:
"""Sets the activity for the shard.
@@ -106,17 +118,7 @@ class Payphone(commands.AutoShardedBot):
shard_id=shard_id,
)
print(f"Shard {shard_id} was resumed!")
async def on_shard_connect(self, shard_id: int) -> None:
try:
self.update_heartbeat.start()
except RuntimeError:
pass
async def on_shard_disconnect(self, shard_id: int) -> None:
self.update_heartbeat.cancel()
self.log.info("Shard %s was resumed!" % shard_id)
async def get_or_fetch_channel(
self, channel_id: int
+19
View File
@@ -0,0 +1,19 @@
import logging
from rich import logging as rlogging
FORMAT = "%(message)s"
handler = rlogging.RichHandler(level=logging.INFO, rich_tracebacks=True)
formatter = logging.Formatter(FORMAT)
handler.setFormatter(formatter)
logger = logging.getLogger("payphone")
def setup():
"""Sets up pycord logging."""
logging.basicConfig(level=logging.DEBUG, handlers=[handler])