Posted 5 months ago. Visible to the public.

Bash: How to use colors in your tail output

Sometimes it's nice to have some coloring in your logs for better readability. You can output your logs via tail and pipe this through sed to add ANSI color annotations (which your console then interprets).

To print a log (e.g. rails log) and color all lines containing "FATAL" in red and all lines with "INFO" in green:

Copy
tail -f /path/to/log | sed --unbuffered -e 's/\(.*INFO.*\)/\o033[32m\1\o033[39m/' -e 's/\(.*FATAL.*\)/\o033[31m\1\o033[39m/'

Here are the most common color codes:

| Color | Font | Background |
| Black | \033[30m | \033[40m |
| Red | \033[31m | \033[41m |
| Green | \033[32m | \033[42m |
| Orange | \033[33m | \033[43m |
| Blue | \033[34m | \033[44m |
| Magenta | \033[35m | \033[45m |
| Cyan | \033[36m | \033[46m |
| Light gray | \033[37m | \033[47m |
| Use default | \033[39m | \033[49m |

Remember to restore the default color after the content you want colored.

As typing this is kind of cumbersome, you may want to add a bash alias.

Side note: if you have colored output and want to remove the color annotations, see this card

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Judith Roth
Last edit:
5 months ago
by Judith Roth
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Judith Roth to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more