logger: improve error handling
This commit is contained in:
parent
0e0ad400c2
commit
d7ff3c2798
|
@ -18,13 +18,13 @@ class CustomFormatter(logging.Formatter):
|
||||||
color = PURPLE
|
color = PURPLE
|
||||||
else:
|
else:
|
||||||
color = RESET
|
color = RESET
|
||||||
|
|
||||||
record.levelname = f"{color}{record.levelname}{RESET}"
|
record.levelname = f"{color}{record.levelname}{RESET}"
|
||||||
|
|
||||||
if record.args:
|
if record.args:
|
||||||
record.msg = self.highlight_args(record.msg, record.args, color)
|
record.msg = self.highlight_args(record.msg, record.args, color)
|
||||||
record.args = ()
|
record.args = ()
|
||||||
|
|
||||||
# provide trace when DEBUG config
|
# provide trace when DEBUG config
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
import traceback
|
import traceback
|
||||||
|
@ -33,5 +33,8 @@ class CustomFormatter(logging.Formatter):
|
||||||
return super().format(record)
|
return super().format(record)
|
||||||
|
|
||||||
def highlight_args(self, message, args, color):
|
def highlight_args(self, message, args, color):
|
||||||
highlighted_args = tuple(f"{color}{arg}{RESET}" for arg in args)
|
try:
|
||||||
return message % highlighted_args
|
highlighted_args = tuple(f"{color}{arg}{RESET}" for arg in args)
|
||||||
|
return message % highlighted_args
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return f"{color}{message}{RESET}"
|
||||||
|
|
Loading…
Reference in a new issue