简体   繁体   English

如何为 docker 容器的日志着色

[英]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).我有一个容器,它有时会在日志中写下对我来说很重要的关键字,我想在我的终端中用颜色突出显示这个词,但同样重要的是仍然可以实时查看所有内容日志(--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?那么存在某种方式来做到这一点?

I use ccze .我使用ccze as @aimless said, grc is the great utility also.正如@aimless 所说, grc也是一个很好的工具。 It easy to install by sudo apt install ccze for debian/ubuntu-like OS sudo apt install ccze for debian/ubuntu-like OS 很sudo apt install ccze

But if you want to colorize stderr, you need to redirect stderr output to stdout.但是如果你想给 stderr 着色,你需要将 stderr 输出重定向到 stdout。 For example:例如:

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

arg -m ansi helps if you want to scroll output normally如果您想正常滚动输出,arg -m ansi会有所帮助

Try this.试试这个。

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

Note the "--color= always " argument.注意“--color= always ”参数。

Another option would be to use something like https://github.com/jlinoff/colorize .另一种选择是使用类似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'

try试试

docker service logs -f nameOfContainer | grcat conf.dockerinfo

All color profiles reside in /usr/share/grc所有颜色配置文件都位于/usr/share/grc

grc => https://github.com/garabik/grc grc => https://github.com/garabik/grc

try grc .试试grc Follow the instruction to install and just pipe the logs output:按照说明安装并通过管道输出日志:

docker logs -app | grc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM