Skip to content

Commit af4d986

Browse files
authored
Merge pull request eternnoir#1090 from Badiboy/master
Custom logging level for infinity_polling
2 parents f01412a + 8790f26 commit af4d986

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

telebot/__init__.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,28 @@ def __notify_update(self, new_messages):
455455
for listener in self.update_listener:
456456
self._exec_task(listener, new_messages)
457457

458-
def infinity_polling(self, timeout=20, long_polling_timeout=20, *args, **kwargs):
458+
def infinity_polling(self, timeout=20, long_polling_timeout=20, logger_level=logging.ERROR, *args, **kwargs):
459+
"""
460+
Wrap polling with infinite loop and exception handling to avoid bot stops polling.
461+
462+
:param timeout: Request connection timeout
463+
:param long_polling_timeout: Timeout in seconds for long polling (see API docs)
464+
:param logger_level: Custom logging level for infinity_polling logging. None/NOTSET = no error logging
465+
"""
459466
while not self.__stop_polling.is_set():
460467
try:
461468
self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout, *args, **kwargs)
462469
except Exception as e:
463-
logger.error("Infinity polling exception: %s", str(e))
464-
logger.debug("Exception traceback:\n%s", traceback.format_exc())
470+
if logger_level and logger_level >= logging.ERROR:
471+
logger.error("Infinity polling exception: %s", str(e))
472+
if logger_level and logger_level >= logging.DEBUG:
473+
logger.error("Exception traceback:\n%s", traceback.format_exc())
465474
time.sleep(3)
466475
continue
467-
logger.info("Infinity polling: polling exited")
468-
logger.info("Break infinity polling")
476+
if logger_level and logger_level >= logging.INFO:
477+
logger.error("Infinity polling: polling exited")
478+
if logger_level and logger_level >= logging.INFO:
479+
logger.error("Break infinity polling")
469480

470481
def polling(self, none_stop=False, interval=0, timeout=20, long_polling_timeout=20):
471482
"""
@@ -475,10 +486,10 @@ def polling(self, none_stop=False, interval=0, timeout=20, long_polling_timeout=
475486
Warning: Do not call this function more than once!
476487
477488
Always get updates.
478-
:param interval:
489+
:param interval: Delay between two update retrivals
479490
:param none_stop: Do not stop polling when an ApiException occurs.
480-
:param timeout: Integer. Request connection timeout
481-
:param long_polling_timeout. Timeout in seconds for long polling.
491+
:param timeout: Request connection timeout
492+
:param long_polling_timeout: Timeout in seconds for long polling (see API docs)
482493
:return:
483494
"""
484495
if self.threaded:

0 commit comments

Comments
 (0)