如何快速定位到报错日志中的关键信息,一招学会,赶快GET吧

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
注册配置 MSE Nacos/ZooKeeper,118元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 如何快速定位到报错日志中的关键信息,一招学会,赶快GET吧

一般的服务器日志一个可能大的有几十上百m,小的也得几百k,里面内容是比较多的,如拿到日志没思路去看的话,下面一些办法可以让你快速定位到日志中的异常错误信息


文章目录


步骤1:定位到错误信息再那个日志中(grep)

步骤2:查看日志上下文信息

查看上下多少x行信息(cat指令)

查看关键字下多少x行信息(cat指令)

直接进入日志用关键字定位(less指令)

直接进入日志用关键字定位(vim指令-但是不太推荐)

特殊情况(日志被打成tar包了。。。)


下面的这块日志可以作为练习参考

g_2.11-3.9.0.jar:/export/server/kafka/bin/../libs/scala-reflect-2.11.12.jar:/export/server/kafka/bin/../libs/slf4j-api-1.7.25.jar:/export/server/kafka/bin/../libs/slf4j-log4j12-1.7.25.jar:/export/server/kafka/bin/../libs/snappy-java-1.1.7.2.jar:/export/server/kafka/bin/../libs/validation-api-1.1.0.Final.jar:/export/server/kafka/bin/../libs/zkclient-0.10.jar:/export/server/kafka/bin/../libs/zookeeper-3.4.13.jar:/export/server/kafka/bin/../libs/zstd-jni-1.3.5-4.jar (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,836] INFO Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,836] INFO Client environment:java.io.tmpdir=/tmp (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,836] INFO Client environment:java.compiler=<NA> (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,836] INFO Client environment:os.name=Linux (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,836] INFO Client environment:os.arch=amd64 (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,836] INFO Client environment:os.version=3.10.0-1160.76.1.el7.x86_64 (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,837] INFO Client environment:user.name=root (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,837] INFO Client environment:user.home=/root (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,837] INFO Client environment:user.dir=/root (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,839] INFO Initiating client connection, connectString=master:2181,slave1:2181,slave2:2181 sessionTimeout=6000 watcher=kafka.zookeeper.ZooKeeperClient$ZooKeeperClientWatcher$@77fbd92c (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:32,946] INFO Opening socket connection to server slave2/192.168.241.23:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:32,949] INFO [ZooKeeperClient] Waiting until connected. (kafka.zookeeper.ZooKeeperClient)
[2022-11-15 20:46:32,970] INFO Socket error occurred: slave2/192.168.241.23:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:33,073] INFO Opening socket connection to server slave1/192.168.241.22:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:33,074] INFO Socket error occurred: slave1/192.168.241.22:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:33,174] INFO Opening socket connection to server master/192.168.241.21:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:33,175] INFO Socket connection established to master/192.168.241.21:2181, initiating session (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:33,182] INFO Unable to read additional data from server sessionid 0x0, likely server has closed socket, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:34,747] INFO Opening socket connection to server slave2/192.168.241.23:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:34,748] INFO Socket error occurred: slave2/192.168.241.23:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:35,820] INFO Opening socket connection to server slave1/192.168.241.22:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:35,821] INFO Socket error occurred: slave1/192.168.241.22:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:36,209] INFO Opening socket connection to server master/192.168.241.21:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:36,209] INFO Socket connection established to master/192.168.241.21:2181, initiating session (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:36,210] INFO Unable to read additional data from server sessionid 0x0, likely server has closed socket, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:37,505] INFO Opening socket connection to server slave2/192.168.241.23:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:37,510] INFO Socket error occurred: slave2/192.168.241.23:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:38,515] INFO Opening socket connection to server slave1/192.168.241.22:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:38,515] INFO Socket error occurred: slave1/192.168.241.22:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:38,951] INFO [ZooKeeperClient] Closing. (kafka.zookeeper.ZooKeeperClient)
[2022-11-15 20:46:39,623] INFO Session: 0x0 closed (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:39,624] INFO EventThread shut down for session: 0x0 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:39,627] INFO [ZooKeeperClient] Closed. (kafka.zookeeper.ZooKeeperClient)
[2022-11-15 20:46:39,629] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply$mcV$sp(ZooKeeperClient.scala:268)
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply(ZooKeeperClient.scala:264)
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply(ZooKeeperClient.scala:264)
        at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:251)
        at kafka.zookeeper.ZooKeeperClient.kafka$zookeeper$ZooKeeperClient$$waitUntilConnected(ZooKeeperClient.scala:264)
        at kafka.zookeeper.ZooKeeperClient.<init>(ZooKeeperClient.scala:97)
        at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1693)
        at kafka.server.KafkaServer.kafka$server$KafkaServer$$createZkClient$1(KafkaServer.scala:348)
        at kafka.server.KafkaServer.initZkClient(KafkaServer.scala:372)
        at kafka.server.KafkaServer.startup(KafkaServer.scala:202)
        at kafka.server.KafkaServerStartable.startup(KafkaServerStartable.scala:38)
        at kafka.Kafka$.main(Kafka.scala:75)
        at kafka.Kafka.main(Kafka.scala)
[2022-11-15 20:46:39,633] INFO shutting down (kafka.server.KafkaServer)
[2022-11-15 20:46:39,636] INFO shut down completed (kafka.server.KafkaServer)
[2022-11-15 20:46:39,638] ERROR Exiting Kafka. (kafka.server.KafkaServerStartable)
[2022-11-15 20:46:39,647] INFO shutting down (kafka.server.KafkaServer)
(END)

步骤1:定位到错误信息再那个日志中(grep)


当天日志可能比较多,被切分成很多个小日志

如下图

1.png

如果出现这种情况,我们可以使用这种办法来定位错误信息再那个日志中

比如我们有相关的关键字可以直接用,用关键字匹配日志

grep -ri "关键字" 所以查询的日志(可以使用*进行匹配)

[root@master logs]# grep -ri "ERROR Fatal" server.log.2022-11-14-21*
server.log.2022-11-14-21:[2022-11-15 20:46:39,629] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
server.log.2022-11-14-21-1:[2022-11-15 20:46:39,629] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)

从上面我们可以直接定位到错误信息在server.log.2022-11-14-21和server.log.2022-11-14-21-1都有


步骤2:查看日志上下文信息

查看上下多少x行信息(cat指令)

查看指定文件中关键词上下5行的信息cat xxx |grep -C 5 "关键信息"

[root@master logs]# cat server.log.2022-11-14-21 |grep -C 5 "ERROR Fatal"
[2022-11-15 20:46:38,515] INFO Socket error occurred: slave1/192.168.241.22:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:38,951] INFO [ZooKeeperClient] Closing. (kafka.zookeeper.ZooKeeperClient)
[2022-11-15 20:46:39,623] INFO Session: 0x0 closed (org.apache.zookeeper.ZooKeeper)
[2022-11-15 20:46:39,624] INFO EventThread shut down for session: 0x0 (org.apache.zookeeper.ClientCnxn)
[2022-11-15 20:46:39,627] INFO [ZooKeeperClient] Closed. (kafka.zookeeper.ZooKeeperClient)
[2022-11-15 20:46:39,629] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply$mcV$sp(ZooKeeperClient.scala:268)
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply(ZooKeeperClient.scala:264)
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply(ZooKeeperClient.scala:264)
        at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:251)
[root@master logs]#

如图示效果

1.png

查看关键字下多少x行信息(cat指令)

查看指定文件中关键词往下10行的信息cat xxx |grep -A 10 "关键信息"

[root@master logs]# cat server.log.2022-11-14-21 |grep -A 10  "ERROR Fatal"
[2022-11-15 20:46:39,629] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply$mcV$sp(ZooKeeperClient.scala:268)
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply(ZooKeeperClient.scala:264)
        at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply(ZooKeeperClient.scala:264)
        at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:251)
        at kafka.zookeeper.ZooKeeperClient.kafka$zookeeper$ZooKeeperClient$$waitUntilConnected(ZooKeeperClient.scala:264)
        at kafka.zookeeper.ZooKeeperClient.<init>(ZooKeeperClient.scala:97)
        at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1693)
        at kafka.server.KafkaServer.kafka$server$KafkaServer$$createZkClient$1(KafkaServer.scala:348)
        at kafka.server.KafkaServer.initZkClient(KafkaServer.scala:372)
[root@master logs]#

效果如图所示

1.png

直接进入日志用关键字定位(less指令)

less xxx.log

[root@master logs]# less server.log.2022-11-14-21

进入日志后敲一下/ + 关键字,之后按一下回车

具体看图操作

1.png

回车之后就定位到关键词的地方了,如果还有其他相同的关键字日志继续/ + 方向键↑就能快速把刚才的关键字打出来了,完了在敲回车

1.png

直接进入日志用关键字定位(vim指令-但是不太推荐)

同less指令的后续操作

直接vim进去或者vi进去,不太建议使用是有时候shell连接在你正在vi或者vim的时候断掉之后会产生一部分快照文件,如果下一次连到服务器的时候需要把快照文件删除掉才能继续编辑


特殊情况(日志被打成tar包了。。。)

如果日志比较大了,而且是前n天的,比如说是10天前,有可能会被压缩tar包(具体看你们公司相关配置),这个时候使用可以解压文件之后再查看,但是如果权限不够,不允许解压,就需要另求他法了

查看tar包中的日志内容:zcat xxx.tar.gz | egrep -a "关键字"

[root@master logs]# zcat test.tar.gz |egrep -a "ERROR Fatal"
[2022-11-15 20:46:39,629] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
1月前
|
存储 数据采集 分布式计算
Hadoop-17 Flume 介绍与环境配置 实机云服务器测试 分布式日志信息收集 海量数据 实时采集引擎 Source Channel Sink 串行复制负载均衡
Hadoop-17 Flume 介绍与环境配置 实机云服务器测试 分布式日志信息收集 海量数据 实时采集引擎 Source Channel Sink 串行复制负载均衡
44 1
|
2月前
|
Docker Python 容器
1. 日志输出报错
1. 日志输出报错
|
1月前
|
缓存 Linux 编译器
【C++】CentOS环境搭建-安装log4cplus日志组件包及报错解决方案
通过上述步骤,您应该能够在CentOS环境中成功安装并使用log4cplus日志组件。面对任何安装或使用过程中出现的问题,仔细检查错误信息,对照提供的解决方案进行调整,通常都能找到合适的解决之道。log4cplus的强大功能将为您的项目提供灵活、高效的日志管理方案,助力软件开发与维护。
54 0
|
2月前
|
Java
日志框架log4j打印异常堆栈信息携带traceId,方便接口异常排查
日常项目运行日志,异常栈打印是不带traceId,导致排查问题查找异常栈很麻烦。
|
1月前
|
存储 缓存 网络协议
搭建dns服务常见报错--查看/etc/named.conf没有错误日志信息却显示出错(/etc/named.conf:49: missing ‘;‘ before ‘include‘)及dns介绍
搭建dns服务常见报错--查看/etc/named.conf没有错误日志信息却显示出错(/etc/named.conf:49: missing ‘;‘ before ‘include‘)及dns介绍
110 0
|
3月前
|
Kubernetes 数据安全/隐私保护 容器
【Azure APIM】APIM Self-Hosted网关中,添加网关日志以记录请求头信息(Request Header / Response Header)
【Azure APIM】APIM Self-Hosted网关中,添加网关日志以记录请求头信息(Request Header / Response Header)
|
3月前
|
Go 开发者
【应用服务 App Service】App Service发生错误请求时,如何查看IIS Freb日志,从中得知错误所发生的模块,请求中所携带的Header信息
【应用服务 App Service】App Service发生错误请求时,如何查看IIS Freb日志,从中得知错误所发生的模块,请求中所携带的Header信息
|
3月前
|
监控 安全 Linux
在Linux中,某个账号登陆linux后,系统会在哪些日志文件中记录相关信息?
在Linux中,某个账号登陆linux后,系统会在哪些日志文件中记录相关信息?
|
3月前
|
数据挖掘 语音技术
3D-Speaker说话人任务的开源项目问题之语义说话人信息模块在说话人日志系统中的问题如何解决
3D-Speaker说话人任务的开源项目问题之语义说话人信息模块在说话人日志系统中的问题如何解决
|
3月前
|
API
【Azure 服务总线】查看Service Bus中消息多次发送的日志信息,消息是否被重复消费
【Azure 服务总线】查看Service Bus中消息多次发送的日志信息,消息是否被重复消费