Parsing Log4j Layouts from Log Files

PNS picture PNS · Jul 8, 2011 · Viewed 9.8k times · Source

Is there any open source tool out there that can read all Layout implementations of Apache Log4j 1.2.x into something meaningful (e.g., Log4j's own LogEvent objects)? So far I haven't found an application (Chainsaw included) that can do that.

I am looking for something straightforward, that converts from a text file (plain or XML), into a Java object, given the file name and the pattern parameters (e.g., the format pattern in PatternLayout). If it has a clear API that makes it easy to integrate into an application, even better, but this is not necessary.

Answer

Scott picture Scott · Jul 8, 2011

Here is the code Chainsaw uses to convert patternlayouts into something useful, using only log4j APIs. In the case of the latest developer snapshot of Chainsaw, it is used to build a Chainsaw config directly from log4j xml or properties file fileappender entries.

Use the converters and fields populated by PatternParser#parse to do what you want. The PatternParser class is included in the log4j 'extras' companion.

From http://svn.apache.org/repos/asf/logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/LogFilePatternLayoutBuilder.java

public static String getLogFormatFromPatternLayout(String patternLayout) {
    String input = OptionConverter.convertSpecialChars(patternLayout);
    List converters = new ArrayList();
    List fields = new ArrayList();
    Map converterRegistry = null;

    PatternParser.parse(input, converters, fields, converterRegistry, PatternParser.getPatternLayoutRules());
    return getFormatFromConverters(converters);
}

If you actually want something that will read a log file conforming to a specific PatternLayout and generate log4j LoggingEvents, see LogFilePatternReceiver. Chainsaw uses the getLogFormatFromPatternLayout method to convert the patternLayout into the LogFormat required by the receiver.

By the way, the developer snapshot of Chainsaw contains a lot of additional new features - if you want to try it out, you can get it here:

http://people.apache.org/~sdeboy