Skip to content
This repository was archived by the owner on Mar 2, 2026. It is now read-only.

Commit 099c3f4

Browse files
committed
Sync with modmail-dev#3305
1 parent 47c70f1 commit 099c3f4

5 files changed

Lines changed: 189 additions & 128 deletions

File tree

bot.py

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import asyncio
55
import copy
66
import hashlib
7-
import logging
87
import os
98
import re
109
import string
@@ -87,8 +86,11 @@ def __init__(self):
8786

8887
self.threads = ThreadManager(self)
8988

90-
self.log_file_name = os.path.join(temp_dir, f"{self.token.split('.')[0]}.log")
91-
self._configure_logging()
89+
log_dir = os.path.join(temp_dir, "logs")
90+
if not os.path.exists(log_dir):
91+
os.mkdir(log_dir)
92+
self.log_file_path = os.path.join(log_dir, "modmail.log")
93+
configure_logging(self)
9294

9395
self.plugin_db = PluginDatabaseClient(self) # Deprecated
9496

@@ -188,29 +190,6 @@ async def load_extensions(self):
188190
logger.exception("Failed to load %s.", cog)
189191
logger.line("debug")
190192

191-
def _configure_logging(self):
192-
level_text = self.config["log_level"].upper()
193-
logging_levels = {
194-
"CRITICAL": logging.CRITICAL,
195-
"ERROR": logging.ERROR,
196-
"WARNING": logging.WARNING,
197-
"INFO": logging.INFO,
198-
"DEBUG": logging.DEBUG,
199-
}
200-
logger.line()
201-
202-
log_level = logging_levels.get(level_text)
203-
if log_level is None:
204-
log_level = self.config.remove("log_level")
205-
logger.warning("Invalid logging level set: %s.", level_text)
206-
logger.warning("Using default logging level: INFO.")
207-
else:
208-
logger.info("Logging level: %s", level_text)
209-
210-
logger.info("Log file: %s", self.log_file_name)
211-
configure_logging(self.log_file_name, log_level)
212-
logger.debug("Successfully configured logging.")
213-
214193
@property
215194
def version(self):
216195
return version.parse(__version__)
@@ -1850,16 +1829,6 @@ def main():
18501829
discord.__version__,
18511830
)
18521831

1853-
# Set up discord.py internal logging
1854-
if os.environ.get("LOG_DISCORD"):
1855-
logger.debug(f"Discord logging enabled: {os.environ['LOG_DISCORD'].upper()}")
1856-
d_logger = logging.getLogger("discord")
1857-
1858-
d_logger.setLevel(os.environ["LOG_DISCORD"].upper())
1859-
handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="w")
1860-
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
1861-
d_logger.addHandler(handler)
1862-
18631832
bot = ModmailBot()
18641833
bot.run()
18651834

cogs/utility.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,7 @@ async def about(self, ctx):
384384
async def debug(self, ctx):
385385
"""Shows the recent application logs of the bot."""
386386

387-
log_file_name = self.bot.token.split(".")[0]
388-
389-
with open(
390-
os.path.join(os.path.dirname(os.path.abspath(__file__)), f"../temp/{log_file_name}.log"),
391-
"r+",
392-
encoding="utf-8",
393-
) as f:
387+
with open(self.bot.log_file_path, "r+", encoding="utf-8") as f:
394388
logs = f.read().strip()
395389

396390
if not logs:
@@ -416,7 +410,7 @@ async def debug(self, ctx):
416410
msg = "```Haskell\n"
417411
msg += line
418412
if len(msg) + 3 > 2000:
419-
msg = msg[:1993] + "[...]```"
413+
msg = msg[:1992] + "[...]```"
420414
messages.append(msg)
421415
msg = "```Haskell\n"
422416

@@ -438,12 +432,7 @@ async def debug_hastebin(self, ctx):
438432
"""Posts application-logs to Hastebin."""
439433

440434
haste_url = os.environ.get("HASTE_URL", "https://hastebin.cc")
441-
log_file_name = self.bot.token.split(".")[0]
442-
443-
with open(
444-
os.path.join(os.path.dirname(os.path.abspath(__file__)), f"../temp/{log_file_name}.log"),
445-
"rb+",
446-
) as f:
435+
with open(self.bot.log_file_path, "rb+") as f:
447436
logs = BytesIO(f.read().strip())
448437

449438
try:
@@ -474,12 +463,7 @@ async def debug_hastebin(self, ctx):
474463
async def debug_clear(self, ctx):
475464
"""Clears the locally cached logs."""
476465

477-
log_file_name = self.bot.token.split(".")[0]
478-
479-
with open(
480-
os.path.join(os.path.dirname(os.path.abspath(__file__)), f"../temp/{log_file_name}.log"),
481-
"w",
482-
):
466+
with open(self.bot.log_file_path, "w"):
483467
pass
484468
await ctx.send(
485469
embed=discord.Embed(color=self.bot.main_color, description="Cached logs are now cleared.")

core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class ConfigManager:
178178
"disable_updates": False,
179179
# Logging
180180
"log_level": "INFO",
181+
"discord_log_level": "INFO",
181182
# data collection
182183
"data_collection": True,
183184
}

core/config_help.json

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,17 @@
373373
"To disable thread cooldown, do `{prefix}config del thread_cooldown`."
374374
]
375375
},
376-
"log_expiration": {
377-
"default": "Never",
378-
"description": "The duration closed threads will be stored within the database before deletion. Logs that have been closed for longer than this duration will be deleted automatically.",
379-
"examples": [
380-
"`{prefix}config set log_expiration P12DT3H` (stands for 12 days and 3 hours in [ISO-8601 Duration Format](https://en.wikipedia.org/wiki/ISO_8601#Durations))",
381-
"`{prefix}config set log_expiration 3 days and 5 hours` (accepted readable time)"
382-
],
383-
"notes": [
384-
"To disable log expiration, do `{prefix}config del log_expiration`."
385-
]
386-
},
376+
"log_expiration": {
377+
"default": "Never",
378+
"description": "The duration closed threads will be stored within the database before deletion. Logs that have been closed for longer than this duration will be deleted automatically.",
379+
"examples": [
380+
"`{prefix}config set log_expiration P12DT3H` (stands for 12 days and 3 hours in [ISO-8601 Duration Format](https://en.wikipedia.org/wiki/ISO_8601#Durations))",
381+
"`{prefix}config set log_expiration 3 days and 5 hours` (accepted readable time)"
382+
],
383+
"notes": [
384+
"To disable log expiration, do `{prefix}config del log_expiration`."
385+
]
386+
},
387387
"thread_cancelled": {
388388
"default": "\"Cancelled\"",
389389
"description": "This is the message to display when a thread times out and creation is cancelled.",
@@ -1182,12 +1182,20 @@
11821182
"If you would like to display the top role of a user regardless of if it's hoisted or not, disable `use_hoisted_top_role`."
11831183
]
11841184
},
1185-
"registry_plugins_only": {
1186-
"default": "True",
1187-
"description": "Controls if the bot is only allowed to install plugins specified in the registry.",
1188-
"examples": [
1189-
"`{prefix}config set registry_plugins_only False`"
1190-
],
1191-
"notes": []
1192-
}
1185+
"registry_plugins_only": {
1186+
"default": "True",
1187+
"description": "Controls if the bot is only allowed to install plugins specified in the registry.",
1188+
"examples": [
1189+
"`{prefix}config set registry_plugins_only False`"
1190+
],
1191+
"notes": []
1192+
},
1193+
"discord_log_level": {
1194+
"default": "INFO",
1195+
"description": "The `discord.py` library logging level for logging to stdout.",
1196+
"examples": [],
1197+
"notes": [
1198+
"This configuration can only to be set through `.env` file or environment (config) variables."
1199+
]
1200+
}
11931201
}

0 commit comments

Comments
 (0)