LINUXSHELL capture error log script

Source: Internet
Author: User
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
  1. #! /Bin/bash
  2. IsInErrorBlock = false
  3. StrArray = ()
  4. Count = 1
  5. While read line
  6. Do
  7. # Echo "Line $ count :"
  8. ArrayIdx = $ count % 10
  9. StrArray [$ count] = "$ line"
  10. Idx = $ (expr match "$ {strArray [$ count]}" ". * ERROR ")
  11. If [$ idx-le 0];
  12. Then
  13. Idx = $ (expr match "$ {strArray [$ count]}" ". * Exception *")
  14. Fi
  15. Idx2 = $ (expr match "$ {strArray [$ count]}" ". * DEBUG ")
  16. Idx3 = $ (expr match "$ {strArray [$ count]}" ". * INFO ")
  17. If [$ idx-gt 0];
  18. Then
  19. If! $ IsInErrorBlock;
  20. Then
  21. Echo 'Print out previous lines'
  22. IsInErrorBlock = true
  23. Echo $ {strArray [$ count]}
  24. Fi
  25. Else
  26. If [$ idx2-gt 0] | [$ idx3-gt 0];
  27. Then
  28. IsInErrorBlock = false
  29. Else
  30. If $ isInErrorBlock;
  31. Then
  32. Echo $ {strArray [$ count]}
  33. Fi
  34. Fi
  35. Fi
  36. Count = $ [$ count + 1]
  37. Done
  38. # Echo "finish \ n"
  39. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.