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 = { 'tag0': '37;42', 'tag1': '37;43', 'tag2': '37;44', }

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

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

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.

pythonoperationsbashgrepterminallog 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

login 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.