To apply color to the output of a tail command while following a logfile (e.g., using tail -f), you can use a combination of tools and techniques to highlight specific patterns, lines, or keywords. Below are several methods to achieve this, depending on your needs:
1. Using grep with Color Output
The simplest way to add color to tail output is to pipe the output to grep with the --color=always option. This highlights matching patterns in the logfile.
Example:
tail -f logfile.log | grep --color=always "ERROR"
- This will highlight the word "ERROR" in red (or your terminal's default color for
grep).
- Use
grep to match any pattern you want to highlight.
Notes:
2. Using awk for Custom Coloring
If you need more control over which parts of the output are colored, you can use awk to apply ANSI color codes based on conditions.
Example:
tail -f logfile.log | awk '
/ERROR/ {print "\033[31m" $0 "\033[0m"; next}
/WARNING/ {print "\033[33m" $0 "\033[0m"; next}
{print}'
\033[31m sets the text color to red (for ERROR).
\033[33m sets the text color to yellow (for WARNING).
\033[0m resets the color to default.
- Lines not matching any condition are printed without color.
Common ANSI Color Codes:
- Red:
\033[31m
- Green:
\033[32m
- Yellow:
\033[33m
- Blue:
\033[34m
- Reset:
\033[0m
Notes:
- This method is highly customizable and allows you to color entire lines or specific fields based on complex logic.
3. Using sed for Coloring
You can use sed to insert ANSI color codes into the output stream based on patterns.
Example:
tail -f logfile.log | sed -E \
-e 's/ERROR/\x1b[31mERROR\x1b[0m/g' \
-e 's/WARNING/\x1b[33mWARNING\x1b[0m/g'
- This highlights the words "ERROR" in red and "WARNING" in yellow, leaving the rest of the line uncolored.
Notes:
- Unlike
awk, sed is better for simple text replacements but less flexible for complex logic.
4. Using ccze for Log Coloring
ccze is a specialized tool for colorizing logfiles. It automatically recognizes common log formats and applies colors to different parts of the output.
Install ccze:
- On Debian/Ubuntu:
sudo apt install ccze
- On Red Hat/CentOS:
sudo yum install ccze
Example:
tail -f logfile.log | ccze -A
- The
-A flag ensures ANSI color codes are used, which is necessary for piping.
Notes:
ccze is great for general-purpose log coloring but may not be as customizable as awk or grep.
- It works well with common log formats (e.g., Apache, syslog).
5. Using multitail for Advanced Log Viewing
multitail is a powerful tool that extends tail with built-in colorization and the ability to follow multiple files simultaneously.
Install multitail:
- On Debian/Ubuntu:
sudo apt install multitail
- On Red Hat/CentOS:
sudo yum install multitail
Example:
multitail -cS apache logfile.log
- The
-cS apache option tells multitail to use a predefined color scheme for Apache logs. You can define custom color schemes in the multitail configuration file (/etc/multitail.conf or ~/.multitailrc).
Custom Color Scheme Example:
Add the following to ~/.multitailrc to highlight "ERROR" in red and "WARNING" in yellow:
colorscheme:mylog
cs_re:red:ERROR
cs_re:yellow:WARNING
Then use it with:
multitail -cS mylog logfile.log
Notes:
multitail is ideal for advanced use cases, such as monitoring multiple logfiles or applying complex color schemes.
6. Using highlight for General Syntax Highlighting
The highlight tool can apply syntax highlighting to arbitrary text, including logfiles.
Install highlight:
- On Debian/Ubuntu:
sudo apt install highlight
- On Red Hat/CentOS:
sudo yum install highlight
Example:
tail -f logfile.log | highlight --out-format=ansi --syntax=log
- The
--syntax=log option tells highlight to treat the input as a logfile, but you can customize it further with a custom syntax definition.
Notes:
highlight is useful for general-purpose highlighting but may not be as tailored to logs as ccze or multitail.
7. Combining Tools for Maximum Flexibility
You can combine tools to achieve more complex coloring. For example, use grep to filter lines and awk to apply custom colors:
Example:
tail -f logfile.log | grep -E "ERROR|WARNING" | awk '
/ERROR/ {print "\033[31m" $0 "\033[0m"; next}
/WARNING/ {print "\033[33m" $0 "\033[0m"; next}'
Recommendations
- For simple highlighting: Use
grep --color=always.
- For custom logic: Use
awk or sed.
- For general-purpose log coloring: Use
ccze.
- For advanced log monitoring: Use
multitail.
- For syntax highlighting: Use
highlight.
Choose the method that best fits your use case, and experiment with the tools to achieve the desired output.