假如我们的生产环境遇到了
日志异常增多
的场景,这时候需要排查日志中具体哪块儿的请求打印的日志比较多,这里我们就需要用到 Linux 命令来排查问题。
cat springboot-demo.jar.log | awk '{print $3}' | uniq -cd | sort -rn | head -n 10
cat
遍历当前日志文件所有内容;awk '{print $3}'
筛选文件的第 3 列,这里我的第 3 列是 traceId;uniq -cd
分组统计当前 traceId 重复出现的次数,并仅展示重复出现的行;sort -rn
这里是 r 和 n,表示将内容以数字的形式进行降序排序;head -n 10
展示出现次数排行前十的 traceId。springboot-demo.jar.log 内容如下:
2022-09-14 09:31:51.387 [69e7bad21f354071bc3e846c8dd2cd61,] INFO com.test.demo Test1...
2022-09-14 09:31:51.387 [69e7bad21f354071bc3e846c8dd2cd61,] INFO com.test.demo Test1...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd61,] INFO com.test.demo Test1...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd63,] INFO com.test.demo Test3...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd63,] INFO com.test.demo Test3...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd63,] INFO com.test.demo Test3...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd63,] INFO com.test.demo Test3...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd64,] INFO com.test.demo Test4...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd64,] INFO com.test.demo Test4...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
整理完毕,完结撒花~