thamizhinian February 2016

how to grep the error line and error caused by line

I need to grep the error.log file to get the *ERROR* and Caused by: for the same error tag in next-next lines

For example: i have the error.log like the following

08.02.2016 00:11:21.541 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp
org.apache.sling.api.scripting.ScriptEvaluationException:
        at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388)
        at org.apache.sling.scripting.jsp.taglib.IncludeTagHandler.dispatch(IncludeTagHandler.java:59)
        at org.apache.sling.scripting.jsp.taglib.AbstractDispatcherTagHandler.doEndTag(AbstractDispatcherTagHandler.java:129)
        at org.apache.jsp.libs.foundation.components.parsys.parsys_jsp._jspService(parsys_jsp.java:318)
        at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502)
        ... 180 common frames omitted
Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available
08.02.2016 05:28:56.748 *ERROR* [158.151.84.34 [1454927336516] GET /content/user/home/people-search.html HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException
org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
        at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502)
        ... 20 common frames omitted
Caused by: java.lang.Exception: user person search is not available
08.02.2016 06:11:24.725 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp
org.apache.sling.api.scripting.ScriptEvaluationExcepti        

Answers


Robert Bräutigam February 2016

How about:

grep -e "\*ERROR\*" -e "Caused by:" error.log


Svperstar February 2016

egrep '\*ERROR\*|Caused by:' error.log


Ed Morton February 2016

Can you have a "Caused by:" line that wasn't preceded by an "ERROR" line? For example could you get a "WARNING" (or similar) followed by a "Caused by"? How about an "ERROR" that isn't followed by a "Caused by:"? How about either text in some other part of the line than where you are targeting (3rd field for ERROR, start of line for Caused by)? If any of that can happen then edit your question to include those cases in your sample input/output.

This is what you need to be safe from all of the above issues:

$ cat tst.awk        
/^[0-9]/ {
    if (inErr) {
        print "No cause found"
    }
    if ($3 == "*ERROR*") {
        print
        inErr = 1
    }
}
inErr && /^Caused by:/ {
    print
    inErr = 0
}
END {
    if (inErr) {
        print "No cause found"
    }
}

$ awk -f tst.awk file
08.02.2016 00:11:21.541 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp
Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available
08.02.2016 05:28:56.748 *ERROR* [158.151.84.34 [1454927336516] GET /content/user/home/people-search.html HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException
Caused by: java.lang.Exception: user person search is not available
08.02.2016 06:11:24.725 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp
Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available

Post Status

Asked in February 2016
Viewed 2,003 times
Voted 6
Answered 3 times

Search




Leave an answer