如何使用grep从日志文件捕获Java异常(包括堆栈跟踪)? [英] How can I capture Java exceptions, including the stack trace, from a log file using grep?

查看:289
本文介绍了如何使用grep从日志文件捕获Java异常(包括堆栈跟踪)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结



我试图使用grep命令从日志文件中提取日志。然而,我可以匹配时间戳,但没有得到我需要的完整堆栈跟踪。

日志文件样本



  [1/10/16 23:55:33:018 PST] 00000057 ServerObj E SECJ0373E:异常消息
at com.own.ws.wim.util.UniqueNameHelper。 formatUniqueName(UniqueNameHelper.java:102)
at com.own.ws.wim.ProfileManager.getImpl(ProfileManager.java:1569)



我所试过的是什么



我可以提取日志条目,但我也想要堆栈跟踪。我试过了:

  $ grep -i'^ [[:space:]] * at'--before-context = 2 SystemOut.log | 
grep1/13/16 7:[1-60]
[1/10/16 23:55:33:018 PST] 00000057 ServerObj E SECJ0373E:异常消息

任何想法如何实现?

解决方案

(从我的答案这里: https://stackoverflow.com/a/16064081/430128



这里有一个快速和肮脏的grep表达式...如果您使用log4j之类的记录器,则异常的第一行通常包含 WARN ERROR ,下一行将包含异常名称和可选消息,然后将开始后续堆栈跟踪与以下之一:


  1. \ tat(tab + at )

  2. 原因:

  3. \ t ...< some number> more(这些是指示堆栈中的帧数未显示在引起的异常中的行)

  4. 堆栈前的一个异常名称(也许是消息)

我们希望得到以上所有的行, grep表达式是:

$ b $ grep -P(WARN | ERROR | ^ \ tat | Exception | ^引起: | \t ... \d + more)



它假定一个Exception类总是包含单词 Exception 这可能是也可能不是真的,但这毕竟是快速和肮脏的。



根据需要进行调整您的具体情况。


Summary

I am trying to fetch logs from a log file using the grep command. However, I can match time stamps, but am not getting the full stack trace I need.

Log File Sample

[1/10/16 23:55:33:018 PST] 00000057 ServerObj E   SECJ0373E: Exception message
at com.own.ws.wim.util.UniqueNameHelper.formatUniqueName(UniqueNameHelper.java:102)
at com.own.ws.wim.ProfileManager.getImpl(ProfileManager.java:1569)

What I've Tried

I am able to fetch log entries, but I want the stack trace as well. I've tried:

$ grep -i '^[[:space:]]*at' --before-context=2 SystemOut.log |
    grep "1/13/16 7:[1-60]" 
[1/10/16 23:55:33:018 PST] 00000057 ServerObj E   SECJ0373E: Exception message

Any idea how this can be achieved?

解决方案

(From my answer here: https://stackoverflow.com/a/16064081/430128)

Here is a quick-and-dirty grep expression... if you are using a logger such as log4j than the first line of the exception will generally contain WARN or ERROR, the next line will contain the Exception name, and optionally a message, and then the subsequent stack trace will begin with one of the following:

  1. "\tat" (tab + at)
  2. "Caused by: "
  3. "\t... <some number> more" (these are the lines that indicate the number of frames in the stack not shown in a "Caused by" exception)
  4. An Exception name (and perhaps message) before the stack

We want to get all of the above lines, so the grep expression is:

grep -P "(WARN|ERROR|^\tat |Exception|^Caused by: |\t... \d+ more)"

It assumes an Exception class always contains the word Exception which may or may not be true, but this is quick-and-dirty after all.

Adjust as necessary for your specific case.

这篇关于如何使用grep从日志文件捕获Java异常(包括堆栈跟踪)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆