core (log): log.exception is now usable
This commit is contained in:
parent
cdd166a1b4
commit
c5d0966e34
2 changed files with 27 additions and 17 deletions
|
|
@ -22,7 +22,6 @@
|
||||||
# TODO: change formatting from "%s" style to "{}" when moved to Python 3
|
# TODO: change formatting from "%s" style to "{}" when moved to Python 3
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, Any, Optional
|
from typing import TYPE_CHECKING, Any, Optional
|
||||||
from typing_extensions import TypedDict
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from logging import _ExcInfoType
|
from logging import _ExcInfoType
|
||||||
|
|
@ -45,10 +44,6 @@ class Filtered(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class KWArgs(TypedDict):
|
|
||||||
exc_info: _ExcInfoType
|
|
||||||
|
|
||||||
|
|
||||||
class Logger:
|
class Logger:
|
||||||
"""High level logging class"""
|
"""High level logging class"""
|
||||||
fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER)
|
fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER)
|
||||||
|
|
@ -68,25 +63,39 @@ class Logger:
|
||||||
self.post_treat = other.post_treat
|
self.post_treat = other.post_treat
|
||||||
self._name = other._name
|
self._name = other._name
|
||||||
|
|
||||||
def addTraceback(self, message):
|
def add_traceback(self, message):
|
||||||
tb = traceback.format_exc()
|
tb = traceback.format_exc()
|
||||||
return message + "\n==== traceback ====\n" + tb
|
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
|
"""Actually log the message
|
||||||
|
|
||||||
@param message: formatted message
|
@param message: formatted message
|
||||||
"""
|
"""
|
||||||
if kwargs.get('exc_info', False):
|
if exc_info:
|
||||||
message = self.addTraceback(message)
|
message = self.add_traceback(message)
|
||||||
print(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
|
"""Print message
|
||||||
|
|
||||||
@param level: one of C.LOG_LEVELS
|
@param level: one of C.LOG_LEVELS
|
||||||
@param message: message to format and print
|
@param message: message to format and print
|
||||||
"""
|
"""
|
||||||
|
if exc_info:
|
||||||
|
message = self.add_traceback(message)
|
||||||
try:
|
try:
|
||||||
formatted = self.format(level, message)
|
formatted = self.format(level, message)
|
||||||
if self.post_treat is None:
|
if self.post_treat is None:
|
||||||
|
|
@ -129,21 +138,24 @@ class Logger:
|
||||||
else:
|
else:
|
||||||
raise e
|
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)
|
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)
|
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)
|
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)
|
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)
|
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):
|
class FilterName(object):
|
||||||
"""Filter on logger name according to a regex"""
|
"""Filter on logger name according to a regex"""
|
||||||
|
|
|
||||||
|
|
@ -38,8 +38,6 @@ class TwistedLogger(log.Logger):
|
||||||
|
|
||||||
@param message: formatted message
|
@param message: formatted message
|
||||||
"""
|
"""
|
||||||
if kwargs.pop('exc_info', False):
|
|
||||||
message = self.addTraceback(message)
|
|
||||||
self.twisted_log.emit(
|
self.twisted_log.emit(
|
||||||
level=self.level_map[level],
|
level=self.level_map[level],
|
||||||
format=message,
|
format=message,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue