akash
akash

Reputation: 1801

What are the best ways to search in logs?

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.

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.

I want to print for the 100 lines above the log so to print backtrace also in both the cases(especially for second).

Upvotes: 1

Views: 13841

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 99001

You can use sed , i.e.:

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

sed 100q

Displays the first 100 lines (emulates tail -n 100)

Upvotes: 1

griffon vulture
griffon vulture

Reputation: 6774

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:

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'

Upvotes: 7

Related Questions