如何隐藏telegram.dendor.ptb错误和警告消息?

发布于 2025-01-18 11:03:37 字数 3417 浏览 1 评论 0原文

我正在使用下面的代码运行一个电报机器人。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜雨飘雪 2025-01-25 11:03:37

对此有一个简单的解决方案:

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()

There is a simple solution to this:

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__)

The main function will remain the same:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文