Extending log coloring to message text. Enable by default.
Signed-off-by: colramos-amd <colramos@amd.com>
Этот коммит содержится в:
1 родитель
78c48eaed5
Коммит
b1d0b3905c
1 file changed
+10
-20
+10
-20
@@ -30,13 +30,13 @@ from utils.utils import trace_logger
|
|||||||
# Define the colors
|
# Define the colors
|
||||||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
||||||
RESET_SEQ = "\033[0m"
|
RESET_SEQ = "\033[0m"
|
||||||
COLOR_SEQ = "\033[1;%dm"
|
COLOR_SEQ = "\033[%dm"
|
||||||
|
|
||||||
COLORS = {
|
COLORS = {
|
||||||
"WARNING": YELLOW,
|
"WARNING": YELLOW,
|
||||||
"INFO": GREEN,
|
"INFO": WHITE,
|
||||||
"DEBUG": BLUE,
|
"DEBUG": BLUE,
|
||||||
"CRITICAL": YELLOW,
|
"CRITICAL": RED,
|
||||||
"ERROR": RED,
|
"ERROR": RED,
|
||||||
"TRACE": MAGENTA,
|
"TRACE": MAGENTA,
|
||||||
}
|
}
|
||||||
@@ -47,9 +47,12 @@ class ColoredFormatter(logging.Formatter):
|
|||||||
def format(self, record):
|
def format(self, record):
|
||||||
levelname = record.levelname
|
levelname = record.levelname
|
||||||
if levelname in COLORS:
|
if levelname in COLORS:
|
||||||
levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
|
if levelname == "WARNING" or levelname == "ERROR" or levelname == "DEBUG":
|
||||||
record.levelname = levelname_color
|
log_fmt = f"{COLOR_SEQ % (30 + COLORS[levelname])}%(levelname)s: %(message)s{RESET_SEQ}"
|
||||||
return logging.Formatter.format(self, record)
|
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):
|
class PlainFormatter(logging.Formatter):
|
||||||
@@ -70,20 +73,7 @@ def setup_console_handler():
|
|||||||
setattr(logging, "TRACE", logging.TRACE)
|
setattr(logging, "TRACE", logging.TRACE)
|
||||||
setattr(logging, "trace", trace_logger)
|
setattr(logging, "trace", trace_logger)
|
||||||
|
|
||||||
# setup log formatting
|
formatter = ColoredFormatter()
|
||||||
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()
|
|
||||||
|
|
||||||
console_handler = logging.StreamHandler(sys.stdout)
|
console_handler = logging.StreamHandler(sys.stdout)
|
||||||
console_handler.setFormatter(formatter)
|
console_handler.setFormatter(formatter)
|
||||||
|
|||||||
Reference in new issue
Block a user