Fix logging

This commit is contained in:
Nexus 2023-12-05 18:06:39 +00:00
parent 4c28bec11c
commit de3cbda7aa
Signed by: nex
GPG key ID: 0FA334385D0B689F
2 changed files with 40 additions and 12 deletions

46
main.py
View file

@ -1,4 +1,5 @@
import asyncio import os
import signal
import sys import sys
import logging import logging
import textwrap import textwrap
@ -13,15 +14,42 @@ from utils import JimmyBanException, JimmyBans, console, get_or_none
from utils.client import bot from utils.client import bot
logging.basicConfig( logging.basicConfig(
filename=getattr(config, "LOG_FILE", "jimmy.log"), format="%(asctime)s:%(levelname)s:%(name)s: %(message)s",
filemode=getattr(config, "LOG_MODE", "a"),
format="%(asctime)s:%(level)s:%(name)s: %(message)s",
datefmt="%Y-%m-%d:%H:%M", datefmt="%Y-%m-%d:%H:%M",
level=getattr(config, "LOG_LEVEL", logging.INFO) level=logging.DEBUG,
force=True,
handlers=[
RichHandler(
getattr(config, "LOG_LEVEL", logging.INFO),
console=console,
markup=True,
rich_tracebacks=True,
show_path=False,
show_time=False
),
logging.FileHandler(
"jimmy.log",
"a"
)
]
) )
# echo to stdout logging.getLogger("discord.gateway").setLevel(logging.WARNING)
handler = RichHandler(getattr(config, "LOG_LEVEL", logging.INFO), console=console, markup=True) for _ln in [
logging.getLogger().addHandler(handler) "discord.client",
"httpcore.connection",
"httpcore.http2",
"hpack.hpack",
"discord.http",
"discord.bot",
"httpcore.http11",
"aiosqlite",
"httpx"
]:
logging.getLogger(_ln).setLevel(logging.INFO)
if os.name != "nt":
signal.signal(signal.SIGTERM, lambda: bot.loop.run_until_complete(bot.close()))
@bot.listen() @bot.listen()
@ -78,7 +106,7 @@ async def on_application_command(ctx: discord.ApplicationContext):
@bot.event @bot.event
async def on_ready(): async def on_ready():
bot.log.info("(READY) Logged in as", bot.user) bot.log.info("(READY) Logged in as %r", bot.user)
if getattr(config, "CONNECT_MODE", None) == 1: if getattr(config, "CONNECT_MODE", None) == 1:
bot.log.critical("Bot is now ready and exit target 1 is set, shutting down.") bot.log.critical("Bot is now ready and exit target 1 is set, shutting down.")
await bot.close() await bot.close()

View file

@ -9,14 +9,13 @@ import orm
from discord.ui import View from discord.ui import View
from utils import ( from utils import (
TOKEN_LENGTH,
BannedStudentID, BannedStudentID,
Student, Student,
VerifyCode, VerifyCode,
console, console,
get_or_none, get_or_none,
send_verification_code,
) )
TOKEN_LENGTH = 16
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from cogs.timetable import TimeTableCog from cogs.timetable import TimeTableCog
@ -133,7 +132,8 @@ class VerifyView(View):
) )
try: try:
_code = await send_verification_code(interaction.user, st) # _code = await send_verification_code(interaction.user, st)
raise RuntimeError("Disabled.")
except Exception as e: except Exception as e:
return await interaction.followup.send(f"\N{cross mark} Failed to send email - {e}. Try again?") return await interaction.followup.send(f"\N{cross mark} Failed to send email - {e}. Try again?")
console.log(f"Sending verification email to {interaction.user} ({interaction.user.id}/{st})...") console.log(f"Sending verification email to {interaction.user} ({interaction.user.id}/{st})...")