Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of bash output for specific character sequence

Tags:

bash

colors

I'm working on a project in which I start some servers from terminal and it displays all of the server logs as different events happen (polling messages, API calls, etc). I'm in the middle of testing some new features and need to watch these logs as they go by (quickly) to ensure everything is working as expected. I'm wondering if it's possible to color code certain messages that come through so I can identify them more quickly.

For example, I want to identify where a specific API call was made. Something like "INFO:root:default: PUT /api/v0.1/something/something/mashedpotatoes". Is it possible to change the color of that message whenever it happens to come through?

like image 510
MJDonns Avatar asked Mar 15 '23 14:03

MJDonns


1 Answers

You can use ANSI escape codes to do this.

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

And use it like:

PURPLE='\033[0;35m'
NC='\033[0m' # No Color
echo "${PURPLE}INFO:root:default: PUT /api/v0.1/something/something/mashedpotatoes${NC}\n"

which prints it in purple.

like image 200
vivek85 Avatar answered Apr 09 '23 10:04

vivek85