如何隐藏telegram.dendor.ptb错误和警告消息?
我正在使用下面的代码运行一个电报机器人。
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'))
但是,即使在所有这些警告/错误消息之后,机器人还是运行良好。那里没有问题。唯一的问题是网络错误消息,经过大量谷歌搜索后,我确信我无法摆脱它。我想要的只是隐藏这些消息,以便我可以有一个更干净的终端来获取我需要的相关信息。
这是我尝试过但没有成功的方法。
尝试 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__)
似乎没有任何效果,我有点失望。请告诉我我缺少什么!这不应该这么令人沮丧。这是一个简单的机器人。
I am running a telegram bot with the code below.
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()
The code is working fine and it is doing what needs to be done. The only issue I am facing is in my terminal, when the code has run for hours and hours, I wake up and see this mess (over and over again!):
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'))
But, even after all these warning/error messages, the bot is running fine. There is no issue there. The only problem is the network error messages which after a lot of googling, I am convinced, I can't get rid of. All I want is to hide these messages so that I can have a cleaner terminal for relevant information which I need.
Here is the things I have tried but none has worked.
Try 1:
import logging
logging.getLogger("telegram").setLevel(logging.CRITICAL)
logger = logging.getLogger(__name__)
Try 2:
import logging
logging.getLogger("telegram.vendor,ptb_urllib3.urllib3").setLevel(
logging.CRITICAL)
logger = logging.getLogger(__name__)
Nothing seems to be working and I am a little disappointed. Please show me what I am missing! This should not be this frustrating. It is a simple bot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对此有一个简单的解决方案:
主要功能将保持不变:
There is a simple solution to this:
The main function will remain the same: