Skip to content

Commit 41d23e2

Browse files
refactor(logging): make botstrap logging useful
- provide completion message - silence httpcore logger - filter out botcore's warning for patching send_typing - use short url for contributing link - only use logging.warning and up for things that need user action - use logging.fatal when exiting due to error - use % for all logging statements
1 parent 4edc1cb commit 41d23e2

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

botstrap.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import re
34
import sys
@@ -6,11 +7,16 @@
67
from dotenv import load_dotenv
78
from httpx import Client, HTTPStatusError, Response
89

9-
from bot.constants import Webhooks, _Categories, _Channels, _Roles
10-
from bot.log import get_logger
10+
# Filter out the send typing monkeypatch logs from bot core when we import to get constants
11+
logging.getLogger("pydis_core").setLevel(logging.WARNING)
12+
13+
from bot.constants import Webhooks, _Categories, _Channels, _Roles # noqa: E402
14+
from bot.log import get_logger # noqa: E402
1115

1216
load_dotenv()
13-
log = get_logger("Config Bootstrapper")
17+
log = get_logger("botstrap")
18+
# Silence noisy httpcore logger
19+
get_logger("httpcore").setLevel("INFO")
1420

1521
env_file_path = Path(".env.server")
1622
BOT_TOKEN = os.getenv("BOT_TOKEN", None)
@@ -51,10 +57,10 @@ def __getitem__(self, item: str):
5157
try:
5258
return super().__getitem__(item)
5359
except KeyError:
54-
log.warning(f"Couldn't find key: {item} in dict: {self.name} ")
60+
log.fatal("Couldn't find key: %s in dict: %s", item, self.name)
5561
log.warning(
56-
"Please make sure to follow our contribution guideline "
57-
"https://www.pythondiscord.com/pages/guides/pydis-guides/contributing/bot/ "
62+
"Please follow our contribution guidelines "
63+
"https://pydis.com/contributing-bot "
5864
"to guarantee a successful run of botstrap "
5965
)
6066
sys.exit(-1)
@@ -85,18 +91,14 @@ def upgrade_server_to_community_if_necessary(
8591
payload = response.json()
8692

8793
if COMMUNITY_FEATURE not in payload["features"]:
88-
log.warning("This server is currently not a community, upgrading.")
94+
log.info("This server is currently not a community, upgrading.")
8995
payload["features"].append(COMMUNITY_FEATURE)
9096
payload["rules_channel_id"] = rules_channel_id_
9197
payload["public_updates_channel_id"] = announcements_channel_id_
9298
self.patch(f"/guilds/{self.guild_id}", json=payload)
9399
log.info(f"Server {self.guild_id} has been successfully updated to a community.")
94100

95-
def create_forum_channel(
96-
self,
97-
channel_name_: str,
98-
category_id_: int | str | None = None
99-
) -> int:
101+
def create_forum_channel(self, channel_name_: str, category_id_: int | str | None = None) -> int:
100102
"""Creates a new forum channel."""
101103
payload = {"name": channel_name_, "type": GUILD_FORUM_TYPE}
102104
if category_id_:
@@ -177,10 +179,9 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
177179
all_roles = discord_client.get_all_roles()
178180

179181
for role_name in _Roles.model_fields:
180-
181182
role_id = all_roles.get(role_name, None)
182183
if not role_id:
183-
log.warning(f"Couldn't find the role {role_name} in the guild, PyDis' default values will be used.")
184+
log.warning("Couldn't find the role %s in the guild, PyDis' default values will be used.", role_name)
184185
continue
185186

186187
config_str += f"roles_{role_name}={role_id}\n"
@@ -212,9 +213,7 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
212213
for channel_name in _Channels.model_fields:
213214
channel_id = all_channels.get(channel_name, None)
214215
if not channel_id:
215-
log.warning(
216-
f"Couldn't find the channel {channel_name} in the guild, PyDis' default values will be used."
217-
)
216+
log.warning("Couldn't find the channel %s in the guild, PyDis' default values will be used.", channel_name)
218217
continue
219218

220219
config_str += f"channels_{channel_name}={channel_id}\n"
@@ -226,7 +225,7 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
226225
category_id = all_categories.get(category_name, None)
227226
if not category_id:
228227
log.warning(
229-
f"Couldn't find the category {category_name} in the guild, PyDis' default values will be used."
228+
"Couldn't find the category %s in the guild, PyDis' default values will be used.", category_name
230229
)
231230
continue
232231

@@ -251,3 +250,5 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
251250

252251
with env_file_path.open("wb") as file:
253252
file.write(config_str.encode("utf-8"))
253+
254+
log.info("Botstrap completed successfully. Configuration has been written to %s", env_file_path)

0 commit comments

Comments
 (0)