changeset 3988:760f563b1243

core (log): `log.exception` is now usable
author Goffi <goffi@goffi.org>
date Thu, 24 Nov 2022 10:08:47 +0100
parents 9089cf6c9684
children f5ba7594cced
files sat/core/log.py sat/core/log_config.py
diffstat 2 files changed, 27 insertions(+), 17 deletions(-) [+]
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"""
--- a/sat/core/log_config.py	Tue Nov 22 12:03:36 2022 +0100
+++ b/sat/core/log_config.py	Thu Nov 24 10:08:47 2022 +0100
@@ -38,8 +38,6 @@
 
         @param message: formatted message
         """
-        if kwargs.pop('exc_info', False):
-            message = self.addTraceback(message)
         self.twisted_log.emit(
             level=self.level_map[level],
             format=message,