logger: improve error handling

This commit is contained in:
pedro 2024-11-27 00:38:03 +01:00
parent 2793344684
commit e25b8e5994

View file

@ -18,13 +18,13 @@ class CustomFormatter(logging.Formatter):
color = PURPLE
else:
color = RESET
record.levelname = f"{color}{record.levelname}{RESET}"
if record.args:
record.msg = self.highlight_args(record.msg, record.args, color)
record.args = ()
# provide trace when DEBUG config
if settings.DEBUG:
import traceback
@ -33,5 +33,8 @@ class CustomFormatter(logging.Formatter):
return super().format(record)
def highlight_args(self, message, args, color):
highlighted_args = tuple(f"{color}{arg}{RESET}" for arg in args)
return message % highlighted_args
try:
highlighted_args = tuple(f"{color}{arg}{RESET}" for arg in args)
return message % highlighted_args
except (TypeError, ValueError):
return f"{color}{message}{RESET}"