aboutsummaryrefslogtreecommitdiffstats
path: root/botcore/utils/logging.py
diff options
context:
space:
mode:
authorGravatar Chris Lovering <[email protected]>2022-02-24 16:10:47 +0000
committerGravatar Chris Lovering <[email protected]>2022-02-24 17:32:48 +0000
commitf7dac414b098900b340b2c36b0e69fce6b6c69ba (patch)
tree206580307cb4c9084e19c80212a3b2f5c98d72bb /botcore/utils/logging.py
parentAlter docstrings to look better in autodocs (diff)
Rename loggers.py to logging.py to allow for more generic utils in future
Diffstat (limited to 'botcore/utils/logging.py')
-rw-r--r--botcore/utils/logging.py45
1 files changed, 45 insertions, 0 deletions
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))