Skip to content

Commit f6e85a5

Browse files
Add colored logging output
Added ColoredFormatter to make log levels easier to spot in terminal output. Uses the ANSI color codes from issue #206 different from ccos-scripts
1 parent 82bcef6 commit f6e85a5

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

scripts/shared.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,37 @@ def paths_update(logger, paths, old_quarter, new_quarter):
114114
return paths
115115

116116

117+
class ColoredFormatter(logging.Formatter):
118+
"""Custom formatter that adds ANSI color codes based on log level."""
119+
120+
# Color codes from issue #206
121+
COLORS = {
122+
logging.DEBUG: "\033[90m", # Bright Black
123+
logging.INFO: "\033[37m", # White
124+
logging.WARNING: "\033[103m", # Bright Yellow
125+
logging.ERROR: "\033[33m", # Yellow
126+
logging.CRITICAL: "\033[31m", # Red
127+
}
128+
RESET_COLOR = "\033[0m"
129+
130+
def format(self, record):
131+
# Get the formatted message first
132+
message = super().format(record)
133+
134+
# Add color based on log level
135+
color_code = self.COLORS.get(record.levelno, "")
136+
if color_code:
137+
return f"{color_code}{message}{self.RESET_COLOR}"
138+
return message
139+
140+
117141
def setup(current_file):
118142
# Set up logging
119143
root = logging.getLogger()
120144
root.handlers.clear()
121145
root.setLevel(logging.INFO)
122146

123-
formatter = logging.Formatter(
147+
formatter = ColoredFormatter(
124148
"%(asctime)s - %(levelname)s - %(module)s - %(message)s"
125149
)
126150

0 commit comments

Comments
 (0)