This is the third day of my participation in the August More Text Challenge

The world’s martial arts, only fast can not break. When we encounter different problem scenarios in development projects, we can have corresponding routines, so that our work can get twice the result with half the effort.

Scenario 1: Filter out logs near keywords

Since we usually get very few logs with grep, we need to look at nearby logs.

Routine: View logs by line number and filter out logs near the keyword

First, get the line number of the critical log,

cat -n test.log | grep "error"  
Copy the code

For example, the line number of the “error” keyword is 102. At this point, if I want to view the log for the first 10 and last 10 lines of this keyword:

cat -n test.log |tail -n +92|head -n 20
Copy the code
  • Tail -n +92 Queries logs generated after row 92
  • Head -n 20: Searches the first 20 records in the previous query result

Display line numbers, page view, page turn with Spaces (use more/less)

cat -n test.log | grep "error" | more
Copy the code

Scenario 2: View logs generated within a specified period

Because there are so many application logs, we usually only want to view logs for a certain period of time.

Format:

sed -n '/2020-02-20 17:00:/,/2020-02-20 17:10:/p' app.log
sed -n '/2020-02-20 17:00:00/,/2020-02-20 17:10:00/p' app.log
Copy the code

Note that 17:00:00 or 17:00 must have occurred. If no log exists, run the following command to check whether the log exists.

grep "2020-02-20 17:10:00" app.log
Copy the code

Scenario 3: View the number of matched characters in logs

We sometimes want to see how many times a certain error occurs.

Format:

Count the number of errors in the entire file.

grep "error" test.log | wc -l
Copy the code

Counts the number of errors in the log file within a specified period of time.

sed -n '/2020-02-21 00:00:/,/2020-02-21 17:10:/p' app.log  | grep 'error' | wc -l
Copy the code

Scenario 4: Viewing logs in real time

When we do upstream and downstream syndication, we need to see the logs in real time. Whether the interface was called, whether the message was received, and so on.

Format:

tail -f test.log
Copy the code

By default, you can view only 10 rows of data. To view more, say 100 rows, you can run the following command.

tail -100f test.log

tailf -100 app.log
Copy the code

I personally like this command very much and often use it when coordinating with upstream and downstream systems.

Scenario 5: Query the last 100 lines and look for the keyword “error”

When the server deploys an application startup error, the application is interrupted directly because of some error. The following command may be used to view only the latest error log information.

Format:

tail -n 100 test.log | grep 'error'
Copy the code

Keyword text marked red, you deserve it.

tail -n 100 test.log | grep 'error' --color
Copy the code

Query the last 100 lines, and look for the keyword “error”, the text is red, up and down two lines

Tail - n 20 test. The log | grep 'results' - color - C2Copy the code

Similar parameters:

  • -a: Displays the matched lines and the first three lines. For example, -a3 indicates that the matched lines and the first three lines are displayed
  • -b: displays the number of matched lines and the next three lines. For example, -b3 indicates that the matched lines and the next three lines are displayed
  • -c: displays the number of rows before and after the matched row. For example, -c3 indicates the number of rows before and after the matched row

Scenario 6: Use VIM to search for oversized log files

Sometimes the file content is too much, through filtering is not good to locate the specific error information, we might as well use Vim to view.

Format:

Start with the vim command to open the file

vim test.log 
Copy the code

Then do the corresponding operations, such as the following general usage methods.

  • Skip to the last line of text: Press ‘G’, i.e. ‘Shift + G’
  • Skip to the first character of the first line of text: press g twice,
  • Jump to the first character of the current line: Press 0 on the current line.
  • Search for a keyword: Search for the keyword in the colon + question mark + keyword format. Search for the keyword from the bottom up, N up. Such as:? com.common.exception

If you’ve learned the six tips above, you should be able to troubleshoot most application logging problems.