Extending log coloring to message text. Enable by default.
Signed-off-by: colramos-amd <colramos@amd.com>
This commit is contained in:
+10
-20
@@ -30,13 +30,13 @@ from utils.utils import trace_logger
|
||||
# Define the colors
|
||||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
||||
RESET_SEQ = "\033[0m"
|
||||
COLOR_SEQ = "\033[1;%dm"
|
||||
COLOR_SEQ = "\033[%dm"
|
||||
|
||||
COLORS = {
|
||||
"WARNING": YELLOW,
|
||||
"INFO": GREEN,
|
||||
"INFO": WHITE,
|
||||
"DEBUG": BLUE,
|
||||
"CRITICAL": YELLOW,
|
||||
"CRITICAL": RED,
|
||||
"ERROR": RED,
|
||||
"TRACE": MAGENTA,
|
||||
}
|
||||
@@ -47,9 +47,12 @@ class ColoredFormatter(logging.Formatter):
|
||||
def format(self, record):
|
||||
levelname = record.levelname
|
||||
if levelname in COLORS:
|
||||
levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
|
||||
record.levelname = levelname_color
|
||||
return logging.Formatter.format(self, record)
|
||||
if levelname == "WARNING" or levelname == "ERROR" or levelname == "DEBUG":
|
||||
log_fmt = f"{COLOR_SEQ % (30 + COLORS[levelname])}%(levelname)s: %(message)s{RESET_SEQ}"
|
||||
else:
|
||||
log_fmt = f"{COLOR_SEQ % (30 + COLORS[levelname])}%(message)s{RESET_SEQ}"
|
||||
formatter = logging.Formatter(log_fmt)
|
||||
return formatter.format(record)
|
||||
|
||||
|
||||
class PlainFormatter(logging.Formatter):
|
||||
@@ -70,20 +73,7 @@ def setup_console_handler():
|
||||
setattr(logging, "TRACE", logging.TRACE)
|
||||
setattr(logging, "trace", trace_logger)
|
||||
|
||||
# setup log formatting
|
||||
color_setting = 0
|
||||
if "OMNIPERF_COLOR" in os.environ.keys():
|
||||
color_setting = int(os.environ["OMNIPERF_COLOR"])
|
||||
|
||||
if color_setting == 1:
|
||||
# colored levelname
|
||||
formatter = ColoredFormatter("%(levelname)16s %(message)s")
|
||||
elif color_setting == 2:
|
||||
# non-colored levelname
|
||||
formatter = logging.Formatter("%(levelname)5s %(message)s")
|
||||
else:
|
||||
# non-colored
|
||||
formatter = PlainFormatter()
|
||||
formatter = ColoredFormatter()
|
||||
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_handler.setFormatter(formatter)
|
||||
|
||||
Reference in New Issue
Block a user