Skip to content

Commit

Permalink
ENH: Change logging level colours
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSWang committed Nov 6, 2024
1 parent e6e00ee commit 4f92fbd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
36 changes: 30 additions & 6 deletions src/triumvirate/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@
""" # numpydoc ignore=SS01
import logging
import os
import sys
import time
import warnings
from copy import copy


class _ElapsedLogFormatter(logging.Formatter):
"""Elapsed-time logging formatter.
class _ElapsedColourLogFormatter(logging.Formatter):
"""Elapsed-time colourised logging formatter.
"""
# Log level colours
_IN_BOLD = 1 # default to: 0 for non-bold, 1 for bold
_COLOURS = {
'NOTSET': "\033[0m", # reset
'DEBUG': f"\033[{_IN_BOLD};36m", # cyan
'INFO': f"\033[{_IN_BOLD};32m", # green (blue)
'WARNING': f"\033[{_IN_BOLD};33m", # yellow
'ERROR': f"\033[{_IN_BOLD};31m", # red
'CRITICAL': f"\033[{_IN_BOLD};37;41m", # white on red
}

_start_time = time.time()

Expand All @@ -36,13 +48,25 @@ def format(self, record):
Modified log record with elapsed time.
"""
fmt_record = copy(record)

elapsed_time = record.created - self._start_time
h, remainder_time = divmod(elapsed_time, 3600)
m, s = divmod(remainder_time, 60)

record.elapsed = f"(+{int(h):02d}:{int(m):02d}:{int(s):02d})"
fmt_record.elapsed = f"(+{int(h):02d}:{int(m):02d}:{int(s):02d})"

reset = self._COLOURS.get('NOTSET', "\033[0m")
colour = self._COLOURS.get(record.levelname, reset)

fmt_record.levelname = f"{record.levelname:.4s}"\
.replace('DEBU', 'DBUG')\
.replace('CRIT', 'FATAL')
if 'color' in os.getenv('TERM', '') \
and os.getenv('TRV_INTERACTIVE') is not None:
fmt_record.levelname = colour + fmt_record.levelname + reset

return logging.Formatter.format(self, record)
return logging.Formatter.format(self, fmt_record)


class _CppLogAdapter(logging.LoggerAdapter):
Expand Down Expand Up @@ -138,8 +162,8 @@ def setup_logger(log_level=logging.INFO):
"""
# Set formatter.
formatter = _ElapsedLogFormatter(
fmt='[%(asctime)s %(elapsed)s %(levelname).4s] %(message)s',
formatter = _ElapsedColourLogFormatter(
fmt='[%(asctime)s %(elapsed)s %(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)

Expand Down
6 changes: 3 additions & 3 deletions src/triumvirate/src/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ std::string show_timestamp() {

void exit_fatal(const std::string& msg) {
if (is_colourable()) {
std::cout << "\n\033[1;31mFATAL:\033[0m " << msg << std::endl;
std::cout << "\n\033[1;37;41mFATAL:\033[0m " << msg << std::endl;
} else {
std::cout << "\nFATAL: " << msg << std::endl;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ void Logger::log(int level_entry, const char* fmt_string, ...) {
log_type = "";
break;
case LogLevel::DBUG:
log_type = "\033[0;35mDBUG\033[0m"; // magenta
log_type = "\033[0;36mDBUG\033[0m"; // cyan
break;
case LogLevel::STAT:
log_type = "\033[0;34mSTAT\033[0m"; // blue
Expand Down Expand Up @@ -267,7 +267,7 @@ void Logger::debug(const char* fmt_string, ...) {
std::va_list args;
va_start(args, fmt_string);
if (is_colourable()) {
Logger::emit("\033[0;35mDBUG\033[0m", fmt_string, args);
Logger::emit("\033[0;36mDBUG\033[0m", fmt_string, args);
} else {
Logger::emit("DBUG", fmt_string, args);
}
Expand Down
10 changes: 8 additions & 2 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@

# Define the RegEx expression for logging format.
_LOG_FORMAT = (
r"\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\(\+\d+:\d{2}:\d{2}\)\s\w{4}\]"
r"\s.*\(in C\+\+\)"
r"\[" # [
r"\d{4}-\d{2}-\d{2}\s" # date
r"\d{2}:\d{2}:\d{2}\s" # timestamp
r"\(\+\d+:\d{2}:\d{2}\)\s" # elapsed time
r"(?:\033\[\d+(?:;\d+)*m)?\w{4}(?:\033\[\d+(?:;\d+)*m)?" # log level
r"\]\s" # ]
r".*" # log message
r"\(in C\+\+\)" # C++ indication
)


Expand Down

0 comments on commit 4f92fbd

Please sign in to comment.