aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_core/utils/logging.py
diff options
context:
space:
mode:
authorGravatar ChrisJL <[email protected]>2022-11-05 14:15:11 +0000
committerGravatar GitHub <[email protected]>2022-11-05 14:15:11 +0000
commit3f55e7149a3197b7fa41fcf7dc7df47a3a209cfd (patch)
tree11d77db3a48d8226bc1cd14cb18a21d2423b6352 /pydis_core/utils/logging.py
parentUse New Static Build Site API (#122) (diff)
parentAdd six as a dev dep (diff)
Merge pull request #157 from python-discord/prepare-for-pypi-releasev9.0.0
Prepare for pypi release
Diffstat (limited to 'pydis_core/utils/logging.py')
-rw-r--r--pydis_core/utils/logging.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/pydis_core/utils/logging.py b/pydis_core/utils/logging.py
new file mode 100644
index 00000000..7814f348
--- /dev/null
+++ b/pydis_core/utils/logging.py
@@ -0,0 +1,51 @@
+"""Common logging related functions."""
+
+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))
+
+
+# Setup trace level logging so that we can use it within pydis_core.
+logging.TRACE = TRACE_LEVEL
+logging.setLoggerClass(CustomLogger)
+logging.addLevelName(TRACE_LEVEL, "TRACE")