Skip to content

Commit

Permalink
feat: Add custom_exception attribute to log records in CustomLogger c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
galjos committed May 5, 2024
1 parent cec6f78 commit a0c9ce7
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion PQAnalysis/utils/custom_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit a0c9ce7

Please sign in to comment.