GlassFish Log and Regular Expressions

I was tired to filter out many information not directly useful to my debugging process while reading a GlassFish log file.

A log entry  like this one

thread-pool-1; w: 7;|BATCH-MSG-M0992: Another Work item already
checking for files... |#]

contains many useful information, like the timestamp, the message severity level, the component that issued the message, the thread name and pool etc … But when I do look at a GlassFish log file, nine times out of ten, I do not need all these extra information, I just need the last message. 

Visually filtering out this is boring, error prone and, in general, slow down your analysis process.

When looking at the server log file from NetBeans, it does filter out all that information to propose you only the "relevant" part.  But, when working from command line, switching to NetBeans "just" to read your uncluttered log file is tedious as well.

I decided to implement from the command line, the same filtering as the one proposed by NetBeans, using sed and regular expressions. The first expression I crafted is


It matches any line starting (^) with a bracket and containing anything (.*) until it matches the sequence ;|.  ([ and | must be escaped because they have special meanings in regular expression language).

Combining this with sed‘s substitute function (s/…/…), allows me to eliminate the first part of each line in the log entry.  Applying this to the previous log line gives :

tail server.log  | sed -e 's/^\[.*;\|//'
BATCH-MSG-M0992: Another Work item already checking for files... |#]

This is already more readable.  Then, just for the fun of it, we can get rid of the last three characters as well, by applying a second sed command :

sed -e 's/\|#\]$//'

where the regular expression \|#\]$ means "any line ending with ($) the charaters |#]" (once again, | and ] are escaped with a \).

To summarize, the complete tail command looks like this

tail server.log  | sed -e 's/^\[.*;\|//' | sed -e 's/\|#\]$//'

Of course, I scripted all this to avoid entering this each time I am looking at a GlassFish log file :-).  I let you create your own script as a follow-up exercise.

If you happen to know how to improve this by using only one sed command, I will welcome your input !

Enjoy !

  1. #1 by Claudio Miranda on 25/08/2008 - 11:15

    Hi Sebastien, you can use -e sed parameter to pass many RE as needed

    tail server.log | sed -e ‘s/^\[.*;\|//’ -e ‘s/\|#\]$//’

  2. #2 by Claudio Miranda on 25/08/2008 - 11:19

    At my linux (ubuntu) machine, some escapes are not needed, see the revided version

    tail server.log | sed -e ‘s/^\[.*;|//’ -e ‘s/|#]$//’

  3. #3 by Eric Lerognon on 29/08/2008 - 04:30

    For a bit more power I routinely use the following Perl script. In the example it extracts messages relevant to eInsight 5.1.

(will not be published)