From f7dac414b098900b340b2c36b0e69fce6b6c69ba Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 24 Feb 2022 16:10:47 +0000 Subject: Rename loggers.py to logging.py to allow for more generic utils in future --- botcore/utils/__init__.py | 4 ++-- botcore/utils/channel.py | 4 ++-- botcore/utils/loggers.py | 45 --------------------------------------------- botcore/utils/logging.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ botcore/utils/members.py | 4 ++-- botcore/utils/scheduling.py | 6 +++--- 6 files changed, 54 insertions(+), 54 deletions(-) delete mode 100644 botcore/utils/loggers.py create mode 100644 botcore/utils/logging.py diff --git a/botcore/utils/__init__.py b/botcore/utils/__init__.py index 554e8ad1..71354334 100644 --- a/botcore/utils/__init__.py +++ b/botcore/utils/__init__.py @@ -1,12 +1,12 @@ """Useful utilities and tools for discord bot development.""" -from botcore.utils import (caching, channel, extensions, loggers, members, regex, scheduling) +from botcore.utils import (caching, channel, extensions, logging, members, regex, scheduling) __all__ = [ caching, channel, extensions, - loggers, + logging, members, regex, scheduling, diff --git a/botcore/utils/channel.py b/botcore/utils/channel.py index 6a630c94..db155911 100644 --- a/botcore/utils/channel.py +++ b/botcore/utils/channel.py @@ -3,9 +3,9 @@ import discord from discord.ext.commands import Bot -from botcore.utils import loggers +from botcore.utils import logging -log = loggers.get_logger(__name__) +log = logging.get_logger(__name__) def is_in_category(channel: discord.TextChannel, category_id: int) -> bool: diff --git a/botcore/utils/loggers.py b/botcore/utils/loggers.py deleted file mode 100644 index 740c20d4..00000000 --- a/botcore/utils/loggers.py +++ /dev/null @@ -1,45 +0,0 @@ -"""Custom :obj:`logging.Logger` class that implements a new ``"TRACE"`` level.""" - -import logging -import typing - -if typing.TYPE_CHECKING: - LoggerClass = logging.Logger -else: - LoggerClass = logging.getLoggerClass() - -TRACE_LEVEL = 5 - - -class CustomLogger(LoggerClass): - """Custom implementation of the :obj:`logging.Logger` class with an added :obj:`trace` method.""" - - def trace(self, msg: str, *args, **kwargs) -> None: - """ - Log the given message with the severity ``"TRACE"``. - - To pass exception information, use the keyword argument exc_info with a true value: - - .. code-block:: py - - logger.trace("Houston, we have an %s", "interesting problem", exc_info=1) - - Args: - msg: The message to be logged. - args, kwargs: Passed to the base log function as is. - """ - if self.isEnabledFor(TRACE_LEVEL): - self.log(TRACE_LEVEL, msg, *args, **kwargs) - - -def get_logger(name: typing.Optional[str] = None) -> CustomLogger: - """ - Utility to make mypy recognise that logger is of type :obj:`CustomLogger`. - - Args: - name: The name given to the logger. - - Returns: - An instance of the :obj:`CustomLogger` class. - """ - return typing.cast(CustomLogger, logging.getLogger(name)) diff --git a/botcore/utils/logging.py b/botcore/utils/logging.py new file mode 100644 index 00000000..740c20d4 --- /dev/null +++ b/botcore/utils/logging.py @@ -0,0 +1,45 @@ +"""Custom :obj:`logging.Logger` class that implements a new ``"TRACE"`` level.""" + +import logging +import typing + +if typing.TYPE_CHECKING: + LoggerClass = logging.Logger +else: + LoggerClass = logging.getLoggerClass() + +TRACE_LEVEL = 5 + + +class CustomLogger(LoggerClass): + """Custom implementation of the :obj:`logging.Logger` class with an added :obj:`trace` method.""" + + def trace(self, msg: str, *args, **kwargs) -> None: + """ + Log the given message with the severity ``"TRACE"``. + + To pass exception information, use the keyword argument exc_info with a true value: + + .. code-block:: py + + logger.trace("Houston, we have an %s", "interesting problem", exc_info=1) + + Args: + msg: The message to be logged. + args, kwargs: Passed to the base log function as is. + """ + if self.isEnabledFor(TRACE_LEVEL): + self.log(TRACE_LEVEL, msg, *args, **kwargs) + + +def get_logger(name: typing.Optional[str] = None) -> CustomLogger: + """ + Utility to make mypy recognise that logger is of type :obj:`CustomLogger`. + + Args: + name: The name given to the logger. + + Returns: + An instance of the :obj:`CustomLogger` class. + """ + return typing.cast(CustomLogger, logging.getLogger(name)) diff --git a/botcore/utils/members.py b/botcore/utils/members.py index e7b31342..e89b4618 100644 --- a/botcore/utils/members.py +++ b/botcore/utils/members.py @@ -4,9 +4,9 @@ import typing import discord -from botcore.utils import loggers +from botcore.utils import logging -log = loggers.get_logger(__name__) +log = logging.get_logger(__name__) async def get_or_fetch_member(guild: discord.Guild, member_id: int) -> typing.Optional[discord.Member]: diff --git a/botcore/utils/scheduling.py b/botcore/utils/scheduling.py index d6969302..e2952e6c 100644 --- a/botcore/utils/scheduling.py +++ b/botcore/utils/scheduling.py @@ -7,7 +7,7 @@ import typing from datetime import datetime from functools import partial -from botcore.utils import loggers +from botcore.utils import logging class Scheduler: @@ -36,7 +36,7 @@ class Scheduler: """ self.name = name - self._log = loggers.get_logger(f"{__name__}.{name}") + self._log = logging.get_logger(f"{__name__}.{name}") self._scheduled_tasks: typing.Dict[typing.Hashable, asyncio.Task] = {} def __contains__(self, task_id: typing.Hashable) -> bool: @@ -242,5 +242,5 @@ def _log_task_exception(task: asyncio.Task, *, suppressed_exceptions: typing.Tup exception = task.exception() # Log the exception if one exists. if exception and not isinstance(exception, suppressed_exceptions): - log = loggers.get_logger(__name__) + log = logging.get_logger(__name__) log.error(f"Error in task {task.get_name()} {id(task)}!", exc_info=exception) -- cgit v1.2.3