查看日志常用命令
在我们实际开发中都会把项目部署到测试环境上去,就导致我们看不到后台日志,只能通过登陆测试环境Linux虚拟机来查看相关日志。
下面是我总结出的经常用到的查看日志的相关命令
1、tail
(1) 实时监控100行日志/所有日志
tail -100f test.logtail -f test.log
(2)查询日志最后100行的日志记录
tail -n 100 test.log
(3)查询日志第100行之后的所有日志记录
tail -n +100 test.log
2、head
head 和 tail 正好相反,tail 是查询后多少行记录,而head 是查询前多少行记录
(1)查询日志的前100行记录
head -n 100 test.log
(2)查询日志文件中除了最后100行所有的记录
head -n +100 test.log
3、cat
tac 是倒序查看,cat反写
(1)查看带有关键字的日志(可以得到关键字附近的行号)
cat -n test.log | grep '关键字'
(2)选择关键字的行号100,然后查看它后20行的日志记录
cat -n test.log |tail -n +100 |head -n 20解释:tail -n +100 表示查看100行之后的日志记录head -n 20 表示再查看100行之后日志记录中的前20行日志记录
4、当日志内容比较多,打印在屏幕上不方便时
(1)使用more、less命令
cat -n test.log |grep '关键字' |morecat -n test.log |grep '关键字' |less
(2)使用 XXX.txt 将其保存到文件中
cat -n test.log |grep '关键字' >test.txt