feat(bot): ✨ Add rich logging.

This commit is contained in:
Nyeki.py
2024-10-27 01:30:04 -03:00
parent ec00a557fa
commit 09d2808bd4
6 changed files with 141 additions and 47 deletions
+28 -11
View File
@@ -1,3 +1,11 @@
import asyncio
from payphone.core import logging
log = logging.logger
async def flush():
import orjson
@@ -22,10 +30,10 @@ async def flush():
call_ids = [int(call_id) for call_id in call_ids]
if len(call_ids) == 0:
print("Skipping flushing. No calls to flush.")
log.info("Skipping flushing. No calls to flush.")
return
print(f"Flushing messages for {len(call_ids)} calls.")
log.info("Flushing messages for %s calls." % len(call_ids))
pipe = redis.pipeline()
@@ -72,8 +80,9 @@ async def flush():
asyncpg.exceptions.PostgresError,
asyncpg.exceptions.InterfaceError,
) as e:
print(
f"[Call ID: {call_id}] An error occured trying to flush messages for call ID. {e}"
log.error(
"[Call ID: %s] An error occured trying to flush messages for call ID. %s"
% (call_id, e)
)
continue
@@ -88,14 +97,18 @@ async def flush():
asyncpg.exceptions.PostgresError,
asyncpg.exceptions.InterfaceError,
) as e:
print(
f"[Call ID: {call_id}] An error occured trying to flush message contents. {e}"
log.error(
"[Call ID: %s] An error occured trying to flush message contents. %s"
% (call_id, e)
)
continue
print(f"[Call ID: {call_id}] Flushed {len(messages)} messages and {len(message_contents)} message contents.")
log.info(
"[Call ID: %s] Flushed %s messages and %s message contents."
% (call_id, len(messages), len(message_contents))
)
print("Finished flushing messages.")
log.info("Finished flushing messages.")
async def loop() -> None:
@@ -107,7 +120,11 @@ async def loop() -> None:
await setup.init()
while True:
await asyncio.sleep(30)
try:
while True:
await asyncio.sleep(30)
await flush()
await flush()
except (KeyboardInterrupt, asyncio.exceptions.CancelledError) as e:
log.exception(e)