Skip to content

Commit df14081

Browse files
Fix logging noise within the logging channel (#39)
* remove pythonista-api logging noise * fix reply shims as per earlier edits
1 parent d6856b8 commit df14081

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Diff for: core/utils/logging.py

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def emit(self, record: logging.LogRecord) -> None:
2525
self.bot.logging_queue.put_nowait(record)
2626

2727

28+
class PAPILoggingFilter(logging.Filter):
29+
def __init__(self) -> None:
30+
super().__init__(name="modules.api")
31+
32+
def filter(self, record: logging.LogRecord) -> bool:
33+
return not ("Received HELLO" in record.msg or "added our subscription" in record.msg)
34+
35+
2836
class LogHandler:
2937
def __init__(self, *, bot: Bot, stream: bool = True) -> None:
3038
self.log: logging.Logger = logging.getLogger()
@@ -47,6 +55,7 @@ def __enter__(self: Self) -> Self:
4755
logging.getLogger("discord.http").setLevel(logging.INFO)
4856
logging.getLogger("discord.state").setLevel(logging.WARNING)
4957
logging.getLogger("discord.gateway").setLevel(logging.WARNING)
58+
logging.getLogger("modules.api").addFilter(PAPILoggingFilter())
5059

5160
self.log.setLevel(logging.INFO)
5261
handler = RotatingFileHandler(

Diff for: modules/api.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self, bot: core.Bot) -> None:
4949
self.bot = bot
5050

5151
self.session: aiohttp.ClientSession | None = None
52-
self.backoff: ExponentialBackoff = ExponentialBackoff() # type: ignore
52+
self.backoff: ExponentialBackoff[bool] = ExponentialBackoff()
5353
self.websocket: aiohttp.ClientWebSocketResponse | None = None
5454

5555
self.connection_task: asyncio.Task[None] | None = None
@@ -68,7 +68,7 @@ async def cog_unload(self) -> None:
6868
try:
6969
self.connection_task.cancel()
7070
except Exception as e:
71-
LOGGER.debug(f'Unable to cancel Pythonista API connection_task in "cog_unload": {e}')
71+
LOGGER.error(f'Unable to cancel Pythonista API connection_task in "cog_unload": {e}')
7272

7373
if self.is_connected():
7474
assert self.websocket
@@ -78,7 +78,7 @@ async def cog_unload(self) -> None:
7878
try:
7979
self.keep_alive_task.cancel()
8080
except Exception as e:
81-
LOGGER.debug(f'Unable to cancel Pythonista API keep_alive_task in "cog_unload": {e}')
81+
LOGGER.error(f'Unable to cancel Pythonista API keep_alive_task in "cog_unload": {e}')
8282

8383
def dispatch(self, *, data: dict[str, Any]) -> None:
8484
subscription: str = data["subscription"]
@@ -98,7 +98,7 @@ async def connect(self) -> None:
9898
try:
9999
self.keep_alive_task.cancel()
100100
except Exception as e:
101-
LOGGER.debug(f"Failed to cancel Pythonista API Websocket keep alive. This is likely not a problem: {e}")
101+
LOGGER.warning(f"Failed to cancel Pythonista API Websocket keep alive. This is likely not a problem: {e}")
102102

103103
while True:
104104
try:
@@ -108,13 +108,13 @@ async def connect(self) -> None:
108108
LOGGER.critical("Unable to connect to Pythonista API Websocket, due to an incorrect token.")
109109
return
110110
else:
111-
LOGGER.debug(f"Unable to connect to Pythonista API Websocket: {e}.")
111+
LOGGER.error(f"Unable to connect to Pythonista API Websocket: {e}.")
112112

113113
if self.is_connected():
114114
break
115115
else:
116116
delay: float = self.backoff.delay() # type: ignore
117-
LOGGER.debug(f'Retrying Pythonista API Websocket connection in "{delay}" seconds.')
117+
LOGGER.warning(f'Retrying Pythonista API Websocket connection in "{delay}" seconds.')
118118

119119
await asyncio.sleep(delay)
120120

@@ -148,7 +148,7 @@ async def keep_alive(self) -> None:
148148
op: int | None = data.get("op")
149149

150150
if op == PAPIWebsocketOPCodes.HELLO:
151-
LOGGER.info(f'Received HELLO from Pythonista API: user={data["user_id"]}')
151+
LOGGER.debug(f'Received HELLO from Pythonista API: user={data["user_id"]}')
152152

153153
elif op == PAPIWebsocketOPCodes.EVENT:
154154
self.dispatch(data=data)

Diff for: modules/manuals.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ async def rtfm(self, ctx: core.Context, *, query: str) -> None:
128128
await ctx.reply("Sorry, I couldn't apply a default library to this channel. Try again with a library?")
129129
return
130130

131-
await ctx.reply(str(lib.value), reference=ctx.replied_message)
131+
await ctx.send(str(lib.value), reference=ctx.replied_message)
132132
return
133133

134134
labels = False
@@ -161,7 +161,7 @@ async def rtfm(self, ctx: core.Context, *, query: str) -> None:
161161
return
162162

163163
if not final_query:
164-
await ctx.reply(str(lib.value[0]), reference=ctx.replied_message)
164+
await ctx.send(str(lib.value[0]), reference=ctx.replied_message)
165165
return
166166

167167
url = self.target.with_path("/api/public/rtfm.sphinx").with_query(
@@ -226,7 +226,7 @@ async def rtfs(self, ctx: core.Context, *, query: str) -> None:
226226
await ctx.reply("Sorry, I couldn't apply a default library to this channel. Try again with a library?")
227227
return
228228

229-
await ctx.reply(str(lib.value), reference=ctx.replied_message)
229+
await ctx.send(str(lib.value), reference=ctx.replied_message)
230230
return
231231

232232
source = False
@@ -297,7 +297,7 @@ async def rtfs(self, ctx: core.Context, *, query: str) -> None:
297297

298298
else:
299299
n = next(iter(nodes.items()))
300-
await ctx.reply(f"Showing source for `{n[0]}`\nCommit: {matches['commit'][:6]}", reference=ctx.replied_message)
300+
await ctx.send(f"Showing source for `{n[0]}`\nCommit: {matches['commit'][:6]}", reference=ctx.replied_message)
301301

302302
pages = TextPager(ctx, n[1], prefix="```py")
303303
await pages.paginate()

0 commit comments

Comments
 (0)