From a0c9ce72ce524819734d01fbbc28dba14bac87c8 Mon Sep 17 00:00:00 2001 From: "Josef M. Gallmetzer" Date: Sun, 5 May 2024 21:37:56 +0200 Subject: [PATCH] feat: Add custom_exception attribute to log records in CustomLogger class --- PQAnalysis/utils/custom_logging.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/PQAnalysis/utils/custom_logging.py b/PQAnalysis/utils/custom_logging.py index a106ab8b..c913dd4b 100644 --- a/PQAnalysis/utils/custom_logging.py +++ b/PQAnalysis/utils/custom_logging.py @@ -107,6 +107,8 @@ def _log(self, # pylint: disable=arguments-differ if the level is logging.ERROR or logging.CRITICAL and the logger is not enabled for logging.DEBUG. """ + self.custom_exception = exception + self._original_log( level, msg, @@ -164,6 +166,19 @@ def _original_log(self, super()._log(level, msg, args, **kwargs) + def makeRecord(self, name, level, fn, lno, msg, args, exc_info, + func=None, extra=None, sinfo=None): + """ + This method creates a log record. + """ + + record = super().makeRecord(name, level, fn, lno, msg, args, exc_info, + func=func, extra=extra, sinfo=sinfo) + if hasattr(self, 'custom_exception'): + setattr(record, 'custom_exception', self.custom_exception) + + return record + def error(self, msg: Any, *args, @@ -321,7 +336,11 @@ def format(self, record: logging.LogRecord) -> str: longest_level_key = max(level_keys, key=len) name = record.name - header = f'{formatted_level} {name}\n\n' + + if hasattr(record, 'custom_exception'): + header = f'{formatted_level} {name} - {record.custom_exception}\n\n' + else: + header = f'{formatted_level} {name}\n\n' messages = message.split('\n') wrapper = textwrap.TextWrapper(