2024-10-31 09:14:02 +00:00
|
|
|
import logging
|
2024-11-05 02:43:18 +00:00
|
|
|
from django.conf import settings
|
2024-10-31 09:14:02 +00:00
|
|
|
|
|
|
|
# Colors
|
|
|
|
RED = "\033[91m"
|
|
|
|
PURPLE = "\033[95m"
|
|
|
|
YELLOW = "\033[93m"
|
|
|
|
RESET = "\033[0m"
|
|
|
|
|
|
|
|
|
|
|
|
class CustomFormatter(logging.Formatter):
|
|
|
|
def format(self, record):
|
|
|
|
if record.levelname == "ERROR":
|
|
|
|
color = RED
|
|
|
|
elif record.levelname == "WARNING":
|
|
|
|
color = YELLOW
|
2024-11-10 02:51:15 +00:00
|
|
|
elif record.levelname in ["INFO", "DEBUG"]:
|
|
|
|
color = PURPLE
|
2024-10-31 09:14:02 +00:00
|
|
|
else:
|
|
|
|
color = RESET
|
2024-11-26 23:38:03 +00:00
|
|
|
|
2024-10-31 09:14:02 +00:00
|
|
|
record.levelname = f"{color}{record.levelname}{RESET}"
|
|
|
|
|
|
|
|
if record.args:
|
2024-11-26 23:52:20 +00:00
|
|
|
try:
|
|
|
|
record.msg = record.msg % record.args
|
|
|
|
record.args = ()
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
record.msg = f"{color}{record.msg}{RESET}"
|
|
|
|
|
|
|
|
# Highlight the final formatted message
|
|
|
|
record.msg = self.highlight_message(record.msg, color)
|
2024-11-26 23:38:03 +00:00
|
|
|
|
2024-11-27 23:00:09 +00:00
|
|
|
# pedro says: I discovered that trace is provided anyway with
|
|
|
|
# this commented (reason: strange None msgs)
|
|
|
|
# is this needed?
|
|
|
|
### provide trace when DEBUG config
|
|
|
|
#if settings.DEBUG:
|
|
|
|
# import traceback
|
|
|
|
# print(traceback.format_exc())
|
2024-11-05 02:43:18 +00:00
|
|
|
|
2024-10-31 09:14:02 +00:00
|
|
|
return super().format(record)
|
|
|
|
|
2024-11-27 00:21:52 +00:00
|
|
|
def highlight_message(self, message, color):
|
2024-11-26 23:52:20 +00:00
|
|
|
return f"{color}{message}{RESET}"
|