feat: Implement ANSI color formatting for JSON log output when writing to a terminal.

main
MrWaradana 4 weeks ago
parent 1e8633ac5d
commit c81bb21ef5

@ -11,6 +11,17 @@ from src.enums import OptimumOHEnum
LOG_FORMAT_DEBUG = "%(levelname)s:%(message)s:%(pathname)s:%(funcName)s:%(lineno)d" LOG_FORMAT_DEBUG = "%(levelname)s:%(message)s:%(pathname)s:%(funcName)s:%(lineno)d"
# ANSI Color Codes
RESET = "\033[0m"
COLORS = {
"DEBUG": "\033[36m", # Cyan
"INFO": "\033[32m", # Green
"WARNING": "\033[33m", # Yellow
"WARN": "\033[33m", # Yellow
"ERROR": "\033[31m", # Red
"CRITICAL": "\033[1;31m", # Bold Red
}
class LogLevels(OptimumOHEnum): class LogLevels(OptimumOHEnum):
info = "INFO" info = "INFO"
@ -66,8 +77,15 @@ class JSONFormatter(logging.Formatter):
for key, value in record.__dict__.items(): for key, value in record.__dict__.items():
if key not in standard_attrs: if key not in standard_attrs:
log_record[key] = value log_record[key] = value
return json.dumps(log_record) log_json = json.dumps(log_record)
# Apply color if the output is a terminal
if sys.stdout.isatty():
level_color = COLORS.get(record.levelname, "")
return f"{level_color}{log_json}{RESET}"
return log_json
def configure_logging(): def configure_logging():

Loading…
Cancel
Save