Tuesday, 13 January 2009

Grep WebLogic Logs

The WebLogic logs do not obey a nice one line per entry, for example a log entry containing a stack trace will often occupy many lines. This makes reading the stack trace in the log quite easy but does not help if you try to use grep to search the logs.

For example:

####<13-jan-2009> <[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'> <> <> <1231840002265>
at weblogic.management.mbeanservers.edit.internal.ConfigurationManagerMBeanImpl.save(ConfigurationManagerMBeanImpl.java:243)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at weblogic.management.jmx.modelmbean.WLSModelMBean.invoke(WLSModelMBean.java:443)
at com.sun.jmx.mbeanserver.DynamicMetaDataImpl.invoke(DynamicMetaDataImpl.java:213)
at com.sun.jmx.mbeanserver.MetaDataImpl.invoke(MetaDataImpl.java:220)

The following is a script that I use that makes use of gawk (available on most Linux installations or via Cygwin) that uses the WebLogic log record separator (####) to emulate grep for an entire log entry.

#!/bin/bash
if [ $# -lt 2 ]; then
echo "$0 pattern file(s)"
exit 1
fi
pattern=$1
shift
gawk "BEGIN{RS=\"####\"}/$pattern/{print \$0}" $*

For example if this script is saved as loggrep.sh then you can search for entries containing a pattern with:

loggrep.sh MyException /domain1/servers/ms1/logs/*.log*