Mbded
Mbded

Reputation: 1916

How to colorize logs for docker container

I have container which in logs sometimes write key word which is for me important, and I want to highlight this word in color in my terminal, but also important is still see all content logs in real time (--follow). I just tried command

docker logs -f my_app --tail=100 | grep --color -E '^myWord'

but not working.

So exist some way to do this ?

Upvotes: 26

Views: 27144

Answers (3)

spiilmusic
spiilmusic

Reputation: 725

I use ccze. as @aimless said, grc is the great utility also. It easy to install by sudo apt install ccze for debian/ubuntu-like OS

But if you want to colorize stderr, you need to redirect stderr output to stdout. For example:

docker logs -f my-app 2>&1 | ccze -m ansi

arg -m ansi helps if you want to scroll output normally

UPD: ccze can be very slow. If you encounter this, try running ccze with the nolookups option: ccze -o nolookups. originally answered - https://unix.stackexchange.com/a/461390/83391

Upvotes: 27

aimless
aimless

Reputation: 635

try grc. Follow the instruction to install and just pipe the logs output:

docker logs -app | grc

Upvotes: 2

Joe Linoff
Joe Linoff

Reputation: 771

Try this.

docker logs -f my_app --tail=100 | grep --color=always -E '^myWord'

Note the "--color=always" argument.

Another option would be to use something like https://github.com/jlinoff/colorize. I wrote it to specifically address situations like this. For example it has the ability to specify different colors for each pattern (see the help for details).

Here is an example of how to use it for your case.

$ curl -L https://github.com/jlinoff/colorize/releases/download/v0.8.1/colorize-linux-amd64 --out colorize
$ chmod a+x colorize
$ ./colorize -h
$ docker logs -f my_app --tail=100 | ./colorize '^myWord'
$ # really make it standout.
$ docker logs -f my_app --tail=100 | ./colorize -c red+greenB+bold '^myWord'

Upvotes: 11

Related Questions