赞
踩
通常在进行服务器项目bug排查时,我们会去到Linux上查看日志,像我们一般使用的是 tail -100f node.log ,那么查看日志的方式有哪些呢,怎么定制化的查看呢?
Linux查看日志的方法主要有以下4种,分别为:tail、head、cat、sed。
切换到某个路径的日志目标下,基础命令: # cd 、 # ll
tail -100f test.log
实时监控100行日志
-n 是显示行号;相当于nl命令;例子如下:
tail -n 10 test.log
查询日志尾部最后10行的日志;
tail -n +10 test.log
查询10行之后的所有日志;
参数-q表示不显示处理信息。
tail -q test.log
.参数-v显示详细的处理信息,命令:
tail -v test.log
参数-c显示字节数,命令:
tail -c test.log
循环实时查看最后100条数据?
命令:tail -fn 100 catalina.out
跟tail是相反的,tail是看后多少行日志;例子如下:
head -n 10 test.log
查询日志文件中的头10行日志;
head -n -10 test.log
查询日志文件除了最后10行的其他所有日志;
tac是倒序查看,是cat单词反写;例子如下:
cat -n test.log |grep "debug"
查询关键字的日志
查看所有文件信息的命令:
cat kitty.txt
使用键盘创建一个文件命令:
cat > kitty.test
清除日志文件命令:
cat :> filename
查找日志文件中指定的一段内容或者根据时间范围查询.
如何按行号查找?
操作命令:#sed -n '2,200p' kitty.log
如何按时间段查找?
操作命令:#sed -n /'/2022-04-17 10:07:10/,/2019-05-18 16:54:01/p'
cat -n test.log |grep "debug"
得到关键日志的行号
cat -n test.log |tail -n +92|head -n 20
选择关键字所在的中间一行. 然后查看这个关键字前10行和后10行的日志:
tail -n +92
表示查询92行之后的日志
head -n 20
则表示在前面的查询结果里再查前20条记录
sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p' test.log
特别说明:上面的两个日期必须是日志中打印出来的日志,否则无效;
先grep '2014-12-17 16:17:20' test.log
来确定日志中是否有该 时间点
(1)使用more和less命令,
如: cat -n test.log |grep "debug" |more
这样就分页打印了,通过点击空格键翻页
(2)使用 >xxx.txt 将其保存到文件中,到时可以拉下这个文件分析
如:cat -n test.log |grep "debug" >debug.txt
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。