Skip to content

Commit

Permalink
Enable discord.py logger by default. (#3216)
Browse files Browse the repository at this point in the history
* Enable `discord.py` logger by default.

* Revert:

- Restore import orders
- Logging stuff is now completely handled in `core.models.configure_logging`

* Update logging configurations

* Updated changelog

* Fix overflow characters in logs when using `?debug` command.

* Update changelog

---------

Co-authored-by: Taku <[email protected]>
  • Loading branch information
Jerrie-Aries and Taaku18 authored Jul 15, 2023
1 parent b1f3645 commit 43fbc31
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 112 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ however, insignificant breaking changes do not guarantee a major version bump, s
- Reply not being forwarded from DM. (PR [#3239](https://github.com/modmail-dev/modmail/pull/3239))

### 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))
- `?log key <key>` to retrieve the log link and view a preview using a log key. ([PR #3196](https://github.com/modmail-dev/Modmail/pull/3196))
- `REGISTRY_PLUGINS_ONLY`, environment variable, when set, restricts to only allow adding registry plugins. ([PR #3247](https://github.com/modmail-dev/modmail/pull/3247))
- `DISCORD_LOG_LEVEL` environment variable to set the log level of discord.py. ([PR #3216](https://github.com/modmail-dev/Modmail/pull/3216))

### Changed
- Guild icons in embed footers and author urls now have a fixed size of 128. ([PR #3261](https://github.com/modmail-dev/modmail/pull/3261))
- Repo moved to https://github.com/modmail-dev/modmail.
- Guild icons in embed footers and author urls now have a fixed size of 128. ([PR #3261](https://github.com/modmail-dev/modmail/pull/3261))
- 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 @@ -182,29 +184,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 @@ -1801,16 +1780,6 @@ def main():
)
sys.exit(0)

# Set up discord.py internal logging
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
23 changes: 4 additions & 19 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 All @@ -433,7 +427,7 @@ async def debug(self, ctx):
msg = "```Haskell\n"
msg += line
if len(msg) + 3 > 2000:
msg = msg[:1993] + "[...]```"
msg = msg[:1992] + "[...]```"
messages.append(msg)
msg = "```Haskell\n"

Expand All @@ -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

0 comments on commit 43fbc31

Please sign in to comment.