Skip to content

Commit

Permalink
fix logging crash if issue with telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
titulebolide committed Jul 25, 2022
1 parent acefec1 commit 0dd19e9
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions btb_manager_telegram/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import traceback
from telegram.utils.helpers import escape_markdown
from btb_manager_telegram import settings
from btb_manager_telegram.formating import escape_tg
from btb_manager_telegram.formating import escape_tg, telegram_text_truncator


class LoggerHandler(logging.Handler):
Expand All @@ -15,8 +15,6 @@ def __init__(self):
super().__init__()

def emit(self, record):
if settings.CHAT is None:
return
if record.levelno >= logging.WARNING: # warning, critical or error
emoji = ""
if record.levelno == logging.WARNING:
Expand All @@ -28,10 +26,15 @@ def emit(self, record):
else:
return
message = f"{emoji} {record.levelname.title()} : {record.msg}"
settings.CHAT.send_message(
escape_tg(message, exclude_parenthesis=True), parse_mode="MarkdownV2"
)

message_list = telegram_text_truncator(message)
for msg in message_list:
try:
settings.CHAT.send_message(
escape_tg(message, exclude_parenthesis=True), parse_mode="MarkdownV2"
)
except Exception as e:
#do not use logging.error here! it will end badly.
print(f"The latest error cannot be sent to telegram, reason : {e}")

logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
Expand Down

0 comments on commit 0dd19e9

Please sign in to comment.