feat: add debug logging gated by APP_DEBUG environment variable

- Added APP_DEBUG environment variable to enable debug mode
- Debug mode prints detailed logs to console and file (bot.log)
- Debug mode includes Discord library internals (discord, discord.ext.commands, etc.)
- File handler rotates at 10MB, keeping 5 backup files
- Console handler only active when DEBUG is enabled
- Default logging level is WARNING (non-debug) to reduce noise
This commit is contained in:
Benjamyn 2026-08-27 16:30:10 +10:00
parent 1b08e58aa0
commit f7bdec8195

55
main.py
View File

@ -8,6 +8,7 @@ import asyncio
import os import os
import sys import sys
import logging import logging
import logging.handlers
# Add project root to path # Add project root to path
project_root = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.dirname(os.path.abspath(__file__))
@ -18,11 +19,63 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
# ─── Debug Mode ──────────────────────────────────────────────────────────────
DEBUG = os.getenv("APP_DEBUG", "0") == "1"
if DEBUG:
print("[DEBUG] Debug mode is ENABLED. Logs will be printed.")
else:
print("[DEBUG] Debug mode is DISABLED. Logs will NOT be printed.")
# ─── Logging Configuration ───────────────────────────────────────────────────
log_file = os.path.join(project_root, "bot.log")
# Create a rotating file handler (max 10 MB, keep 5 files)
file_handler = logging.handlers.RotatingFileHandler(
log_file,
maxBytes=10 * 1024 * 1024, # 10 MB
backupCount=5,
)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
))
# Console handler (only when DEBUG is enabled)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.DEBUG if DEBUG else logging.WARNING)
console_handler.setFormatter(logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
))
# Root logger
root_logger = logging.getLogger()
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
root_logger.setLevel(logging.DEBUG if DEBUG else logging.WARNING)
# Suppress noisy third-party logs
logging.getLogger("discord").setLevel(logging.DEBUG if DEBUG else logging.INFO)
logging.getLogger("discord.http").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.gateway").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.utils").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.app_commands").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.ext.commands").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.errors").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.utils").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.client").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
# ─── Logging Configuration ───────────────────────────────────────────────────
# Configure logging # Configure logging
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.DEBUG if DEBUG else logging.WARNING,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[file_handler, console_handler],
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)