如何从 Telegram 机器人控制台记录错误

How to log errors from the Telegram bot console

telegram bot中时不时出现错误,我想记录一下。在所有“try:except”中,我都设置了日志记录,但由于某种原因,这些错误会在控制台中弹出,我找不到抓取它们的地方

我需要的不是问题的原因,而是如何从控制台记录它

(__init__.py:445 MainThread) ERROR - TeleBot: "A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
[b'{"ok":false,"error_code":400,"description":"Bad Request: message can\'t be edited"}']"


在文件末尾我有这样的构造,但由于某种原因它没有捕获我上面指出的错误

while True:
    try:
        bot.polling(none_stop=True, interval=1, timeout=20)
    except Exception as E:
        log(E)

has his own Exception Handling;

示例;

from telegram.error import (TelegramError, Unauthorized, BadRequest, 
                            TimedOut, ChatMigrated, NetworkError)

def error_callback(update, context):
    try:
        raise context.error
    except Unauthorized:
        # remove update.message.chat_id from conversation list
    except BadRequest:
        # handle malformed requests - read more below!
    except TimedOut:
        # handle slow connection problems
    except NetworkError:
        # handle other connection problems
    except ChatMigrated as e:
        # the chat_id of a group has changed, use e.new_chat_id instead
    except TelegramError:
        # handle all other telegram related errors

dispatcher.add_error_handler(error_callback)

Dispatcher 将捕获并记录任何其他错误。 (如 git 页所述)。

配置调度器的小例子;

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