From 0997e3fa9f8bb7d596edd868409c004cdfa73af6 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 2 Feb 2021 20:47:02 +0100 Subject: [PATCH] Remove _log_exc workaround and NullHandler It was added back in bfc408b probably due to a misunderstanding of https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library. The default behaviour of logging WARNING and above is good and desirable (hiding unhandled exceptions in update handlers by default was a big, accidental mistake). NullHandler is used to *prevent* this good default, so it shouldn't be used in the first place. --- telethon/client/telegrambaseclient.py | 5 ++--- telethon/client/updates.py | 17 ++++------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 03d201b0..bea10cbe 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -25,8 +25,7 @@ DEFAULT_PORT = 443 if typing.TYPE_CHECKING: from .telegramclient import TelegramClient -__default_log__ = logging.getLogger(__base_name__) -__default_log__.addHandler(logging.NullHandler()) +_base_log = logging.getLogger(__base_name__) # In seconds, how long to wait before disconnecting a exported sender. @@ -246,7 +245,7 @@ class TelegramBaseClient(abc.ABC): if isinstance(base_logger, str): base_logger = logging.getLogger(base_logger) elif not isinstance(base_logger, logging.Logger): - base_logger = __default_log__ + base_logger = _base_log class _Loggers(dict): def __missing__(self, key): diff --git a/telethon/client/updates.py b/telethon/client/updates.py index 7d009ffe..a9d6344e 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -284,16 +284,6 @@ class UpdateMethods: # region Private methods - def _log_exc(self: 'TelegramClient', msg, *args): - """ - Log an exception, using `stderr` if `logging` is not configured. - """ - if logging.root.hasHandlers(): - self._log[__name__].exception(msg, *args) - else: - print('[ERROR/telethon]:', msg % args, file=sys.stderr) - traceback.print_exc() - # It is important to not make _handle_update async because we rely on # the order that the updates arrive in to update the pts and date to # be always-increasing. There is also no need to make this async. @@ -477,7 +467,7 @@ class UpdateMethods: except Exception as e: if not isinstance(e, asyncio.CancelledError) or self.is_connected(): name = getattr(callback, '__name__', repr(callback)) - self._log_exc('Unhandled exception on %s', name) + self._log[__name__].exception('Unhandled exception on %s', name) async def _dispatch_event(self: 'TelegramClient', event): """ @@ -518,7 +508,7 @@ class UpdateMethods: except Exception as e: if not isinstance(e, asyncio.CancelledError) or self.is_connected(): name = getattr(callback, '__name__', repr(callback)) - self._log_exc('Unhandled exception on %s', name) + self._log[__name__].exception('Unhandled exception on %s', name) async def _get_difference(self: 'TelegramClient', update, channel_id, pts_date): """ @@ -621,7 +611,8 @@ class UpdateMethods: self._log[__name__].warning('Failed to get missed updates after ' 'reconnect: %r', e) except Exception: - self._log_exc('Unhandled exception while getting update difference after reconnect') + self._log[__name__].exception( + 'Unhandled exception while getting update difference after reconnect') # endregion