ggrep让多行日志-无处遁形!

时间:2024-01-26 09:37:49

相信大家都很喜欢用grep指令,查一下项目中有没有出错的,然后通过logid搜索相关出错的日志和一些关键参数,但是在多行日志的情况下就很难处理了,比如okhttp拦截器中分别打印了url,param和response,然后你想统计一下有多少成功的,有多少失败的,发现就比较难了。

解决这个问题的方法,大多数选择在打印日志的时候做处理,把所需要的日志打印到一个行上,但是可能body中的json有换行,或合并后的日志过长,对阅读性增加了更多挑战。能解决但不优雅!今天给大家推荐一个新的方法,ggrep指令!

ggrep是group grep的缩写,意思是将数据分组匹配。然后可以通过多个表达式匹配指定的日志数据。

ggrep指令安装:
gitee:

/bin/bash -c "$(curl -fsSL https://gitee.com/bobby96333/ggrep/raw/main/install.sh)"

or github

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/lingdor/ggrep/HEAD/install.sh)"

or 源码安装

git clone https://github.com/lingdor/ggrep.git
go build ./
sudo cp ggrep /usr/local/bin/

CASE1: 对okhttp的多行数据url,param和response过滤输出:

ggrep --smart-logid --grep 'OkHttpUtil print: --> POST' --grep 'OkHttpUtil print: {' --grep 'OkHttpUtil print: {"return_code' --orderly-match --color always *.info |head

CASE2 对多行日志进行并行,然后可以通过awk,grep,wc等脚本进行二次处理

ggrep --smart-logid --grep 'OkHttpUtil print: --> POST' --grep 'OkHttpUtil print: {' --grep 'OkHttpUtil print: {"return_code' --orderly-match --merge-lines --color always *.info |head

CASE3 将整组数据打印:

ggrep --smart-logid -grep 'OkHttpUtil print: --> POST' --print-group *.info |head -n 100

CASE4 并行处理

./ggrep --smart-logid --grep 'LOG1' --grep 'LOG2' --full-match --parallel-count 0 --color always ./demo/demo.log

CASE5 多条件and匹配

ggrep 
./ggrep --smart-logid --grep 'LOG1' --grep 'LOG2' --full-match --color always ./demo/demo.log

输入:

20240119 LogId[111] LOG1
20240119 LogId[111] LOG2
20240119 LogId[111] LOG3
20240119 LogId[111] LOG2
50240119 LogId[222] LOG1

输出:

20240119 LogId[111] LOG1
20240119 LogId[111] LOG2