Skip to content

Commit 4c3c115

Browse files
authored
highlight warnings and errors (#278)
Follow up of eth-cscs/alps-uenv#276 (comment), covering just the "colored" part for highlighting messages.
1 parent 05f0bb6 commit 4c3c115

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

stackinator/main.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,31 @@ def generate_logfile_name(name=""):
3333
def configure_logging(logfile):
3434
root_logger.setLevel(logging.DEBUG)
3535

36+
class ColoredFormatter(logging.Formatter):
37+
# ANSI escape codes (CSI Control Sequence Introducer)
38+
COLORS = {
39+
logging.WARNING: "\033[1;33m",
40+
logging.ERROR: "\033[1;31m",
41+
logging.CRITICAL: "\033[1;41m",
42+
}
43+
RESET = "\033[0m"
44+
45+
def format(self, record):
46+
message = super().format(record)
47+
48+
# Prepend level name for warnings and above
49+
if record.levelno >= logging.WARNING:
50+
message = f"{record.levelname}: {message}"
51+
52+
# Apply color based on log level
53+
color = self.COLORS.get(record.levelno, self.RESET)
54+
55+
return f"{color}{message}{self.RESET}"
56+
3657
# create stdout handler and set level to info
3758
ch = logging.StreamHandler(stream=sys.stdout)
3859
ch.setLevel(logging.INFO)
39-
ch.setFormatter(logging.Formatter("%(message)s"))
60+
ch.setFormatter(ColoredFormatter())
4061
root_logger.addHandler(ch)
4162

4263
# create log file handler and set level to debug

0 commit comments

Comments
 (0)