Saturday, November 29, 2008

Inspecting log files with Log Factor 5

Almost every serious Java developer uses Log4j as part of their everyday tool set. Log4j is a very powerful, simple and flexible logging framework.

Most developers, myself included, uses a very small subset of Log4j features. Some nice framework capabilities remains hidden from almost everyone. For me the most useful of these hidden features is the Log Factor 5 AKA LF5. LF5 provides a special message formatter and a SWING based user interface for log files inspection. The user interface allows hierarchical category filtering and full text search. The LF5 formatter prepares log messages so they can be opened by LF5 GUI.

LF5 formatter configuration is quite simple. All you have to do is to add the following configuration to your Log4j configuration file:

log4j.rootCategory= INFO,A1

#Configure rolling file appender
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=log4j_lf5.log

#Configure pattern for LF5
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[slf5s.start]%d{DATE}[slf5s.DATE]%n\
%p[slf5s.PRIORITY]%n%x[slf5s.NDC]%n%t[slf5s.THREAD]%n\
%c[slf5s.CATEGORY]%n%l[slf5s.LOCATION]%n%m[slf5s.MESSAGE]%n%n

The previous configuration creates a file appender. The message format is compatible with LF5. After running any Java application (that sends messages to Log4j) using this logging configuration, the file named log4j_lf5.log will be created on the application root directory.

To inspect the log file, open the LF5 GUI using the following command (make sure the log4j jar is available in the classpath):

$ java org.apache.log4j.lf5.StartLogFactor5

You can easily run the LF5 GUI from Eclipse using a run configuration pointing to the org.apache.log4j.lf5.StartLogFactor5 class:

Assuming everything works fine the LF5 GUI will open. Now open the log file using the Menu File|Open... and point to the file log4j_lf5.log. You will see something like the following figure:

Using the tree on left side is possible to filter the categories. It is also possible to choose the visible columns and apply filters by log level.

Using LF5 you can easily change the log scope (level and categories) using the same file as input and this is a great bonus. I hope you enjoy LF5 as much as I do.

2 comments:

Unknown said...

Bacana esse LF5 (qual o motivo do nome?). Tão importante quanto ter logs é ser capaz de analizar os logs com facilidade. :)

Wilson Freitas said...

Matheus,

O motivo do nome LF5 eu não faço a menor idéia :)

Sim, realmente excesso de log pode ser pior que log nenhum. Este post do Coding Horror fala sobre isso.

O LF5 ajuda bastante e também é possível usá-lo com o SocketAppender para capturar logs via rede.