Skip to content

Commit

Permalink
chore: truncate webhook error max size + log webhook send exceptions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed authored Sep 7, 2024
1 parent 3ceee03 commit d50a81d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion canary/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import aiosqlite
import contextlib
import logging
import sys
import traceback

from canary.config import Config
Expand Down Expand Up @@ -71,10 +72,22 @@ def __init__(self, webhook_id, webhook_token, username=None):
self.username = username or "Bot Logs"
logging.Handler.__init__(self)
self.webhook = Webhook.partial(webhook_id, webhook_token, adapter=RequestsWebhookAdapter())
self.max_webhook_payload_size: int = 1800

def emit(self, record):
msg = self.format(record)
self.webhook.send(f"```\n{msg}```", username=self.username)
try:
self.webhook.send(
f"```\n{msg[:self.max_webhook_payload_size]}"
f"{'[...]' if len(msg) > self.max_webhook_payload_size else ''}```",
username=self.username,
)
except Exception as e:
logger.critical(
"An exception (%s) was encountered while trying to send a log message to a webhook:", str(e)
)
logger.critical(traceback.format_exc())
logger.critical("The attempted log message was: %s", msg)


if config.dev_log_webhook_id and config.dev_log_webhook_token:
Expand Down

0 comments on commit d50a81d

Please sign in to comment.