Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable discord.py logger by default. #3216

Merged
merged 8 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ however, insignificant breaking changes do not guarantee a major version bump, s
- Loading the blocked list with the `?blocked` command takes a long time when the list is large. ([PR #3242](https://github.com/kyb3r/modmail/pull/3242))
- Reply not being forwarded from DM. (PR [#3239](https://github.com/modmail-dev/modmail/pull/3239))

# [UNRELEASED]

### Added
- New .env config option: `REGISTRY_PLUGINS_ONLY`, restricts to only allow adding registry plugins. ([PR #3247](https://github.com/modmail-dev/modmail/pull/3247))

### Changed
- Repo moved to https://github.com/modmail-dev/modmail.
- Discord.py internal logging is now enabled by default. ([PR #3216](https://github.com/modmail-dev/Modmail/pull/3216))

### Internal
- Renamed `Bot.log_file_name` to `Bot.log_file_path`. Log files are now created at `temp/logs/modmail.log`. ([PR #3216](https://github.com/modmail-dev/Modmail/pull/3216))

# v4.0.2

Expand Down
41 changes: 5 additions & 36 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@

logger = getLogger(__name__)


temp_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp")
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
Expand Down Expand Up @@ -84,8 +83,11 @@ def __init__(self):

self.threads = ThreadManager(self)

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

self.plugin_db = PluginDatabaseClient(self) # Deprecated
self.startup()
Expand Down Expand Up @@ -178,29 +180,6 @@ async def load_extensions(self):
logger.exception("Failed to load %s.", cog)
logger.line("debug")

def _configure_logging(self):
level_text = self.config["log_level"].upper()
logging_levels = {
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"WARNING": logging.WARNING,
"INFO": logging.INFO,
"DEBUG": logging.DEBUG,
}
logger.line()

log_level = logging_levels.get(level_text)
if log_level is None:
log_level = self.config.remove("log_level")
logger.warning("Invalid logging level set: %s.", level_text)
logger.warning("Using default logging level: INFO.")
else:
logger.info("Logging level: %s", level_text)

logger.info("Log file: %s", self.log_file_name)
configure_logging(self.log_file_name, log_level)
logger.debug("Successfully configured logging.")

@property
def version(self):
return parse_version(__version__)
Expand Down Expand Up @@ -1797,16 +1776,6 @@ def main():
)
sys.exit(0)

# Set up discord.py internal logging
Jerrie-Aries marked this conversation as resolved.
Show resolved Hide resolved
if os.environ.get("LOG_DISCORD"):
logger.debug(f"Discord logging enabled: {os.environ['LOG_DISCORD'].upper()}")
d_logger = logging.getLogger("discord")

d_logger.setLevel(os.environ["LOG_DISCORD"].upper())
handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="w")
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
d_logger.addHandler(handler)

bot = ModmailBot()
bot.run()

Expand Down
21 changes: 3 additions & 18 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,7 @@ async def sponsors(self, ctx):
async def debug(self, ctx):
"""Shows the recent application logs of the bot."""

log_file_name = self.bot.token.split(".")[0]

with open(
os.path.join(os.path.dirname(os.path.abspath(__file__)), f"../temp/{log_file_name}.log"),
"r+",
encoding="utf-8",
) as f:
with open(self.bot.log_file_path, "r+", encoding="utf-8") as f:
logs = f.read().strip()

if not logs:
Expand Down Expand Up @@ -455,12 +449,8 @@ async def debug_hastebin(self, ctx):
"""Posts application-logs to Hastebin."""

haste_url = os.environ.get("HASTE_URL", "https://hastebin.cc")
log_file_name = self.bot.token.split(".")[0]

with open(
os.path.join(os.path.dirname(os.path.abspath(__file__)), f"../temp/{log_file_name}.log"),
"rb+",
) as f:
with open(self.bot.log_file_path, "rb+") as f:
logs = BytesIO(f.read().strip())

try:
Expand Down Expand Up @@ -491,12 +481,7 @@ async def debug_hastebin(self, ctx):
async def debug_clear(self, ctx):
"""Clears the locally cached logs."""

log_file_name = self.bot.token.split(".")[0]

with open(
os.path.join(os.path.dirname(os.path.abspath(__file__)), f"../temp/{log_file_name}.log"),
"w",
):
with open(self.bot.log_file_path, "w"):
pass
await ctx.send(
embed=discord.Embed(color=self.bot.main_color, description="Cached logs are now cleared.")
Expand Down
1 change: 1 addition & 0 deletions core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class ConfigManager:
"disable_updates": False,
# Logging
"log_level": "INFO",
"discord_log_level": "INFO",
# data collection
"data_collection": True,
}
Expand Down
9 changes: 9 additions & 0 deletions core/config_help.json
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,15 @@
"This configuration can only to be set through `.env` file or environment (config) variables."
]
},
"discord_log_level": {
"default": "INFO",
"description": "The `discord.py` library logging level for logging to stdout.",
"examples": [
],
"notes": [
"This configuration can only to be set through `.env` file or environment (config) variables."
]
},
"enable_plugins": {
"default": "Yes",
"description": "Whether plugins should be enabled and loaded into Modmail.",
Expand Down
Loading