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.
This commit is contained in:
Lonami Exo 2021-02-02 20:47:02 +01:00
parent 9a0e030db8
commit 0997e3fa9f
2 changed files with 6 additions and 16 deletions

View File

@ -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):

View File

@ -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