Skip to content
Advertisement

Unix grep for Java logs

I am new to Unix grep and i would like some help to come up with a grep command.

Currently, i have the following types of error in my application logs:

2012-02-31 10:21:41,333 ERROR [65] (GeneralAppliancesBean.java:135) – Exception java.net.ConnectException: Connection timed out

2012-01-31 10:24:41,843 ERROR [41] (FlightStatusBean.java:135) – Exception java.net.SocketTimeoutException: Connection timed out

2011-01-31 10:22:41,841 ERROR [1] (FlightCancellation.java:135) – Exception java.rmi.ConnectException: Connection timed out

2011-01-31 10:22:41,841 ERROR [1] (OrderBean.java:132) – Exception java.rmi.ConnectException: Connection timed out

I would like to know the timestamp (e.g. 2012-02-31 10:21:41,333), the Java class and lineresponsible (e.g. GeneralAppliancesBean.java:132) for this exception.

How can i build this grep command?

Advertisement

Answer

shadyabhi@archlinux ~ $ echo '2012-02-31 10:21:41,333 ERROR [65] (GeneralAppliancesBean.java:135) - Exception java.net.ConnectException: Connection timed oot' | cut -d "," -f 1
2012-02-31 10:21:41
shadyabhi@archlinux ~ $ echo '2012-02-31 10:21:41,333 ERROR [65] (GeneralAppliancesBean.java:135) - Exception java.net.ConnectException: Connection timed oot' | cut -d " " -f 5
(GeneralAppliancesBean.java:135)
shadyabhi@archlinux ~ 

cut is better suited for this.

If your logs are in file named logs, use this.

while read myline 
    do
        echo "Date: "$(echo $myline | cut -d "," -f 1)
        echo "Error: "$(echo $myline | cut -d " " -f 5)
    done < logs

If the errors are multiline (which I am skeptical about):

use grep "ERROR" logs | cut -d "," -f 1 for date & grep "ERROR" logs | cut -d " " -f 4 for class and line number.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement