Operations 4 min read

How to Use a Python Script to Highlight Log Keywords in the Terminal

This guide explains how to set up and use a Python script that highlights specified log keywords with colors in the terminal, including configuring the script, adding aliases in .zshrc or .bashrc, piping command output through the script, and tips for grep buffering and Python line reading.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
How to Use a Python Script to Highlight Log Keywords in the Terminal

When testing an SDK’s logging, you may want to highlight different log tags in the terminal so that the output is easier to read.

1. Copy the script to your local machine.

2. Edit the script’s config dictionary: the tag is the keyword to highlight and color defines the foreground and background colors (format: foreground;background ).

Example configuration:

config = {<br/>    'tag0': '37;42',<br/>    'tag1': '37;43',<br/>    'tag2': '37;44',<br/>}

3. Add an alias to your shell startup file ( .zshrc or .bashrc ).

alias logcolor='python -u /path/to/logcolor.py'<br/>alias grep='grep --color --line-buffered'

4. Pipe the standard output of any command into logcolor to see highlighted tags.

Example: > your_command | logcolor The full logcolor.py script:

#!/usr/bin/env python<br/># encoding: utf-8<br/><br/>"""<br/>Format: \033[display;foreground;backgroundm]<br/><br/>Foreground  Background  Color<br/>30          40          Black<br/>31          41          Red<br/>32          42          Green<br/>33          43          Yellow<br/>34          44          Blue<br/>35          45          Magenta<br/>36          46          Cyan<br/>37          47          White<br/><br/>Display    Meaning<br/>0          Default<br/>1          Bright<br/>4          Underline<br/>5          Blink<br/>7          Reverse<br/>8          Concealed<br/>"""<br/><br/>import sys<br/>import signal<br/><br/>config = {<br/>    'tag0': '37;42',<br/>    'tag1': '37;43',<br/>    'tag2': '37;44',<br/>}<br/><br/>def color_line(line):<br/>    for key, value in config.items():<br/>        line = line.replace(key, '\033[1;%sm%s\033[0m' % (value, key))<br/>    return line<br/><br/>def exit_gracefully(signal, frame):<br/>    sys.exit(0)<br/><br/>if __name__ == '__main__':<br/>    signal.signal(signal.SIGINT, exit_gracefully)<br/>    for line in iter(sys.stdin.readline, ''):<br/>        print(color_line(line.rstrip()))<br/>        sys.stdout.flush()<br/>

Tips

1. Use grep --line-buffered to avoid output buffering delays when piping through logcolor.

2. When the command’s output is redirected to stdout, it is line‑buffered by default; otherwise it may be block‑buffered (often 4096 bytes), which can cause noticeable latency.

3. In Python, avoid iterating directly over sys.stdin because it can introduce similar buffering delays; instead use for line in iter(sys.stdin.readline, '') as shown in the script.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonBashGrepterminallog highlighting
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.