diff sat/core/log.py @ 3988:760f563b1243

core (log): `log.exception` is now usable
author Goffi <goffi@goffi.org>
date Thu, 24 Nov 2022 10:08:47 +0100
parents 8289ac1b34f4
children 1b7c6ee080b9
line wrap: on
line diff
--- a/sat/core/log.py	Tue Nov 22 12:03:36 2022 +0100
+++ b/sat/core/log.py	Thu Nov 24 10:08:47 2022 +0100
@@ -22,7 +22,6 @@
 # TODO: change formatting from "%s" style to "{}" when moved to Python 3
 
 from typing import TYPE_CHECKING, Any, Optional
-from typing_extensions import TypedDict
 
 if TYPE_CHECKING:
     from logging import _ExcInfoType
@@ -45,10 +44,6 @@
     pass
 
 
-class KWArgs(TypedDict):
-    exc_info: _ExcInfoType
-
-
 class Logger:
     """High level logging class"""
     fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER)
@@ -68,25 +63,39 @@
         self.post_treat = other.post_treat
         self._name = other._name
 
-    def addTraceback(self, message):
+    def add_traceback(self, message):
         tb = traceback.format_exc()
         return message + "\n==== traceback ====\n" + tb
 
-    def out(self, message: object, level: Optional[str] = None, **kwargs: KWArgs) -> None:
+    def out(
+        self,
+        message: object,
+        level: Optional[str] = None,
+        exc_info: _ExcInfoType = False,
+        **kwargs
+    ) -> None:
         """Actually log the message
 
         @param message: formatted message
         """
-        if kwargs.get('exc_info', False):
-            message = self.addTraceback(message)
+        if exc_info:
+            message = self.add_traceback(message)
         print(message)
 
-    def log(self, level: str, message: object, **kwargs: KWArgs) -> None:
+    def log(
+        self,
+        level: str,
+        message: object,
+        exc_info: _ExcInfoType = False,
+        **kwargs
+    ) -> None:
         """Print message
 
         @param level: one of C.LOG_LEVELS
         @param message: message to format and print
         """
+        if exc_info:
+            message = self.add_traceback(message)
         try:
             formatted = self.format(level, message)
             if self.post_treat is None:
@@ -129,21 +138,24 @@
             else:
                 raise e
 
-    def debug(self, msg: object, **kwargs: KWArgs) -> None:
+    def debug(self, msg: object, **kwargs) -> None:
         self.log(C.LOG_LVL_DEBUG, msg, **kwargs)
 
-    def info(self, msg: object, **kwargs: KWArgs) -> None:
+    def info(self, msg: object, **kwargs) -> None:
         self.log(C.LOG_LVL_INFO, msg, **kwargs)
 
-    def warning(self, msg: object, **kwargs: KWArgs) -> None:
+    def warning(self, msg: object, **kwargs) -> None:
         self.log(C.LOG_LVL_WARNING, msg, **kwargs)
 
-    def error(self, msg: object, **kwargs: KWArgs) -> None:
+    def error(self, msg: object, **kwargs) -> None:
         self.log(C.LOG_LVL_ERROR, msg, **kwargs)
 
-    def critical(self, msg: object, **kwargs: KWArgs) -> None:
+    def critical(self, msg: object, **kwargs) -> None:
         self.log(C.LOG_LVL_CRITICAL, msg, **kwargs)
 
+    def exception(self, msg: object, exc_info=True, **kwargs) -> None:
+        self.log(C.LOG_LVL_ERROR, msg, exc_info=exc_info, **kwargs)
+
 
 class FilterName(object):
     """Filter on logger name according to a regex"""