Use shell commands to view analysis logs

Abstract: Now websites have background logs, which are mainly used to record abnormal information and key parameters when exceptions occur, and are used to troubleshoot bugs. Sometimes when the log file is large, using shell commands can do more with less.

Scenario : java web

Goal : See the location of the most recent null pointer exception , and

The first 5 lines of code and the last five lines of code when the null pointer exception occurs.

Analysis :

Null pointer exceptions are the most frequently occurring exceptions and are generally code bugs, so they are of practical significance.

The first five lines of code are obtained to view the request parameters,

The last 5 lines of code are to confirm the line number of the code where the exception occurred.

Let's take a look at the example first, which will give a perceptual understanding of our purpose:



 Solution :

1, the first step, we want to get the abnormal line number in the log file, we can use grep -n,

Where -n is to display the line number;

2, the second step: I want to intercept the content of the log file, you can use head and tail together

 

Solution :

Order:

 

grep -rnw "java.lang.NullPointerException" house_error.log |cut  -d ':' -f 1 |xargs -i expr  {} + 10 |xargs -i head -n {} house_error.log|tail -n 20

 Disadvantage: Only the last abnormal

  command execution result can be seen :

 



 Command Explanation :

grep -rnw "java.lang.NullPointerException" house_error.log |cut  -d ':' -f 1 |xargs -i expr  {} + 10 |xargs -i head -n {} house_error.log|tail -n 20



 Note: In expr arithmetic operations, there must be a space on both sides of the operator

refer to:

https://blog.csdn.net/cy_cai/article/details/41908921

https://my.oschina.net/huangweiindex/blog/1798546

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325335266&siteId=291194637