In any kind of service what are the best ways to search in the logs for the following cases :
在任何类型的服务中,在以下情况下搜索日志的最佳方法是什么:
1 - If the bug has already occurred.
2 - If the bug is reproduced and one wants to catch the exception/error occurred.
1 - 如果错误已经发生。 2 - 如果错误被复制,并且想要捕获异常/错误发生。
Some of the ways that i know but inefficient are :
我知道但效率低下的一些方法是:
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.
tail -f production.log =>日志流,你必须手动检查。 tail -n1000 production.log =>记录最后1000行tail -f production.log | grep'500 Internal Server Error'=>显示只有一条特定行的日志流,表示500。
I want to print for the 100 lines above the log so to print backtrace also in both the cases(especially for second).
我想打印日志上方的100行,以便在两种情况下打印回溯(特别是第二次)。
2 个解决方案
#1
You can use sed
, i.e.:
你可以使用sed,即:
sed '/500 Internal Server Error/!d' sederror.log|sed 10q
Explanation:
sed '/500 Internal Server Error/!d'
Will print only lines matching 500 Internal Server Error
将仅打印与500内部服务器错误匹配的行
sed 100q
Displays the first 100 lines (emulates tail -n 100
)
显示前100行(模拟尾部-n 100)
#2
Hope I understand exactly what you want.
希望我完全理解你想要的东西。
use grep with -B option ( -B, --before-context=NUM print NUM lines of leading context) to tell how many lines to print before the line of search:
使用带-B选项的grep(-B, - before-context = NUM打印NUM行前导上下文)来告诉在搜索行之前要打印的行数:
For finding the error in all the log:
要在所有日志中找到错误:
grep -B 100 '500 Internal Server Error' production.log
For real time error:
对于实时错误:
tail -f production.log | grep -B 100 '500 Internal Server Error'
#1
You can use sed
, i.e.:
你可以使用sed,即:
sed '/500 Internal Server Error/!d' sederror.log|sed 10q
Explanation:
sed '/500 Internal Server Error/!d'
Will print only lines matching 500 Internal Server Error
将仅打印与500内部服务器错误匹配的行
sed 100q
Displays the first 100 lines (emulates tail -n 100
)
显示前100行(模拟尾部-n 100)
#2
Hope I understand exactly what you want.
希望我完全理解你想要的东西。
use grep with -B option ( -B, --before-context=NUM print NUM lines of leading context) to tell how many lines to print before the line of search:
使用带-B选项的grep(-B, - before-context = NUM打印NUM行前导上下文)来告诉在搜索行之前要打印的行数:
For finding the error in all the log:
要在所有日志中找到错误:
grep -B 100 '500 Internal Server Error' production.log
For real time error:
对于实时错误:
tail -f production.log | grep -B 100 '500 Internal Server Error'