在日志中搜索的最佳方法是什么?

时间:2015-05-05 05:52:13

标签: unix search logging grep tail

在任何类型的服务中,在以下情况下搜索日志的最佳方法是什么:

1 - 如果错误已经发生。
2 - 如果错误被复制,并且想要捕获异常/错误发生。

我知道但效率低下的一些方法是:

tail -f production.log => log flows and you have to check manually.
tail -n1000 production.log => log for last 1000 lines
tail -f production.log | grep '500 Internal Server Error' => shows the flow of log for only one particular line that says 500.

我想打印日志上方的100行,以便在两种情况下都打印回溯(特别是第二次)。

1 个答案:

答案 0 :(得分:4)

希望我完全理解你想要的东西。

将grep与-B选项一起使用(-B, - before-context = NUM​​打印NUM行前导上下文),以告知在搜索行之前要打印的行数:

在所有日志中找到错误:

grep  -B 100 '500 Internal Server Error' production.log

对于实时错误:

 tail -f production.log | grep  -B 100 '500 Internal Server Error'