Search code examples
unixsearchlogginggreptail

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).


Solution

  • 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)