Background any program running will inevitably generate a lot of logs, of which error logs need to be the most concerned. In some cases, the error log and the normal log are separated, but our system does not. What's more troublesome is that one log file is stored every hour. Therefore, every time a log is generated to find whether the background of the day is running, it is inevitable to generate many logs. the error log requires the most concern. In some cases, the error log and the normal log are separated, but our system does not. What's more troublesome is that one log file is stored every hour, so N files need to be opened every time to find out whether there is an error message on the day, and grep cannot be used because the entire stack needs to be captured.
SHELL is a beginner and has written a script. The main logic is to determine the start record of the line at the log level of the ERROR until it encounters the next log level line of INFO or DEBUG.
[Html]View plaincopy
- #! /Bin/bash
-
- IsInErrorBlock = false
-
- StrArray = ()
- Count = 1
-
-
- While read line
-
- Do
-
- # Echo "Line $ count :"
-
- ArrayIdx = $ count % 10
- StrArray [$ count] = "$ line"
-
-
- Idx = $ (expr match "$ {strArray [$ count]}" ". * ERROR ")
-
- If [$ idx-le 0];
- Then
- Idx = $ (expr match "$ {strArray [$ count]}" ". * Exception *")
- Fi
-
-
- Idx2 = $ (expr match "$ {strArray [$ count]}" ". * DEBUG ")
- Idx3 = $ (expr match "$ {strArray [$ count]}" ". * INFO ")
-
-
- If [$ idx-gt 0];
- Then
- If! $ IsInErrorBlock;
- Then
- Echo 'Print out previous lines'
- IsInErrorBlock = true
- Echo $ {strArray [$ count]}
-
- Fi
- Else
- If [$ idx2-gt 0] | [$ idx3-gt 0];
-
- Then
- IsInErrorBlock = false
- Else
- If $ isInErrorBlock;
- Then
- Echo $ {strArray [$ count]}
- Fi
-
- Fi
- Fi
- Count = $ [$ count + 1]
- Done
-
- # Echo "finish \ n"
-
- Exit 0
The results basically meet the requirements. for each directory cat * log | sh find_error.sh, the entire stack can be output to a single file separately. But the efficiency is very slow. I don't know how to improve the SHELL running efficiency. More 0