Skip to content

Commit

Permalink
Colored terminal output for warnings and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodsf committed Jun 20, 2024
1 parent 077b49c commit 0b644f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Copyright (c) 2021-2024 Claudio Satriano <[email protected]>
- avoid duplicated column guessing
- ensure that prefectly matching column field names are correctly guessed
- warn if an invalid time format is found
- Colored terminal output for warnings and errors

## v0.6 - 2024-05-04

Expand Down
30 changes: 30 additions & 0 deletions requake/config/rq_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ def _make_outdir(outdir):
''')


def _color_handler_emit(fn):
"""
Add color-coding to the logging handler emitter.
Source: https://stackoverflow.com/a/20707569/2021880
"""
def new(*args):
levelno = args[0].levelno
if levelno >= logging.CRITICAL:
color = '\x1b[31;1m' # red
elif levelno >= logging.ERROR:
color = '\x1b[31;1m' # red
elif levelno >= logging.WARNING:
color = '\x1b[33;1m' # yellow
elif levelno >= logging.INFO:
# color = '\x1b[32;1m' # green
color = '\x1b[0m' # no color
elif levelno >= logging.DEBUG:
color = '\x1b[35;1m' # purple
else:
color = '\x1b[0m' # no color
# Color-code the message
args[0].msg = f'{color}{args[0].msg}\x1b[0m'
return fn(*args)
return new


def _setup_logging(progname, action_name):
"""Set up the logging infrastructure."""
global logger
Expand Down Expand Up @@ -106,6 +133,9 @@ def emit(self, record):
self.handleError(record)
console = TqdmLoggingHandler()
console.setLevel(logging.INFO)
# Add logger color coding on all platforms but win32
if sys.platform != 'win32' and sys.stdout.isatty():
console.emit = _color_handler_emit(console.emit)
logger_root.addHandler(console)

logger = logging.getLogger(progname)
Expand Down

0 comments on commit 0b644f4

Please sign in to comment.