如何隐藏 telegram.vendor.ptb 错误和警告信息?

How to hide telegram.vendor.ptb error and warning messages?

我正在 运行使用以下代码创建电报机器人。

 def main():

 """Start the bot."""
 
 global token, allowedUsers

 # Create the Updater and pass it your bot's token.
 updater = Updater(token)

 # Get the dispatcher to register handlers
 dispatcher = updater.dispatcher

 # Registering handlers

 dispatcher.add_handler(CommandHandler("start", start))
 dispatcher.add_handler(CommandHandler("help", help_info))
 dispatcher.add_handler(CommandHandler("learn", learn))
 dispatcher.add_handler(CommandHandler("revise", revise))
 dispatcher.add_handler(CommandHandler("quote", quote))
 dispatcher.add_handler(CommandHandler("more", more))
 dispatcher.add_handler(CommandHandler("quiz", quiz))

 # on non command i.e message

 dispatcher.add_handler(MessageHandler(Filters.text, messageHandler))

 # Start the Bot
 updater.start_polling(drop_pending_updates=True)

 updater.idle()

代码运行良好,正在做需要做的事情。我面临的唯一问题是在我的终端中,当代码 运行 数小时和数小时时,我醒来并看到这个烂摊子(一遍又一遍!):

 2022-04-02 08:53:21,539 - apscheduler.scheduler - INFO - Scheduler
 started 2022-04-02 15:59:10,367 -
 telegram.vendor.ptb_urllib3.urllib3.connectionpool - WARNING -
 Retrying (Retry(total=2, connect=None, read=None, redirect=None))
 after connection broken by
 'NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection
 object at 0x0000028CB212F160>: Failed to establish a new connection:
 [Errno 11001] getaddrinfo failed')':
 /bot98956256546:THISisJustRandomStuffdon'tmindme/getUpdates 2022-04-02
 15:59:10,378 - telegram.vendor.ptb_urllib3.urllib3.connectionpool -
 WARNING - Retrying (Retry(total=1, connect=None, read=None,
 redirect=None)) after connection broken by
 'NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection
 object at 0x0000028CB21330A0>: Failed to establish a new connection:
 [Errno 11001] getaddrinfo failed')':
 /bot98956256546:THISisJustRandomStuffdon'tmindme/getUpdates 2022-04-02
 15:59:10,379 - telegram.vendor.ptb_urllib3.urllib3.connectionpool -
 WARNING - Retrying (Retry(total=0, connect=None, read=None,
 redirect=None)) after connection broken by
 'NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection
 object at 0x0000028CB212E400>: Failed to establish a new connection:
 [Errno 11001] getaddrinfo failed')':
 /bot98956256546:THISisJustRandomStuffdon'tmindme/getUpdates 2022-04-02
 15:59:10,380 - telegram.ext.updater - ERROR - Error while getting
 Updates: urllib3 HTTPError
 HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries
 exceeded with url:
 /bot98956256546:THISisJustRandomStuffdon'tmindme/getUpdates (Caused by
 NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection
 object at 0x0000028CB212E580>: Failed to establish a new connection:
 [Errno 11001] getaddrinfo failed'))

但是,即使在所有这些 warning/error 消息之后,机器人 运行 仍然正常。那里没有问题。唯一的问题是经过大量谷歌搜索后的网络错误消息,我确信,我无法摆脱。我只想隐藏这些消息,这样我就可以有一个更干净的终端来获取我需要的相关信息。

这是我尝试过但 none 有效的方法。

尝试 1:

import logging

logging.getLogger("telegram").setLevel(logging.CRITICAL)
logger = logging.getLogger(__name__)

尝试 2:

import logging

logging.getLogger("telegram.vendor,ptb_urllib3.urllib3").setLevel(
    logging.CRITICAL)
logger = logging.getLogger(__name__)

似乎没有任何效果,我有点失望。请告诉我我缺少什么!这不应该如此令人沮丧。这是一个简单的机器人。

有一个简单的解决方案:

import logging
import (other modules)

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

logging.getLogger("telegram.vendor.ptb_urllib3.urllib3").setLevel(logging.ERROR)
logger = logging.getLogger(__name__)

主要功能将保持不变:

def main():

 """Start the bot."""
 
 global token, allowedUsers

 # Create the Updater and pass it your bot's token.
 updater = Updater(token)

 # Get the dispatcher to register handlers
 dispatcher = updater.dispatcher

 # Registering handlers

 dispatcher.add_handler(CommandHandler("start", start))
 dispatcher.add_handler(CommandHandler("help", help_info))
 dispatcher.add_handler(CommandHandler("learn", learn))
 dispatcher.add_handler(CommandHandler("revise", revise))
 dispatcher.add_handler(CommandHandler("quote", quote))
 dispatcher.add_handler(CommandHandler("more", more))
 dispatcher.add_handler(CommandHandler("quiz", quiz))

 # on non command i.e message

 dispatcher.add_handler(MessageHandler(Filters.text, messageHandler))

 # Start the Bot
 updater.start_polling(drop_pending_updates=True)

 updater.idle()