Parsing Log4j Layouts from Log Files

Is there any open source tool that can read all Apache Log4j 1.2.x layout implementations into something meaningful (for example, Log4j own LogEvent objects)? So far I have not found an application (including a chainsaw) that can do this.

I am looking for something direct that will be converted from a text file (plain or XML) to a Java object, given the file name and template options (like the format template in PatternLayout). If it has a clear API that simplifies integration into the application, even better, but it is not necessary.

+5
source share
3 answers

Here is the Chainsaw code used to convert patternlayouts to something useful, using only the log4j API. In the case of the last Chainsaw developer snapshot, it is used to create the Chainsaw configuration directly from the log4j xml entries or fileappender file files.

Use the converters and fields filled with the PatternParser # template 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);
}

-, , PatternLayout, log4j LoggingEvents, . LogFilePatternReceiver. Chainsaw getLogFormatFromPatternLayout patternLayout LogFormat, .

, Chainsaw - , :

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

+9

OtrosLogViewer. . .

//Define log4j properties
Properties p = new Properties();
p.put("type", "log4j");
p.put("pattern", "TIMESTAMP LEVEL [THREAD]  MESSAGE");
p.put("dateFormat", "yyyy-MM-dd HH:mm:ss,SSS");
Log4jPatternMultilineLogParser logParser = new Log4jPatternMultilineLogParser();    
LogImporterUsingParser importerUsingParser = new LogImporterUsingParser(logParser);
importerUsingParser.init(p);

//
ParsingContext context = new ParsingContext();

//Create log collector, it capture all parsed log events
ProxyLogDataCollector dataCollector = new ProxyLogDataCollector();

//Create input stream from file
InputStream in = new FileInputStream("log4j/log4j.txt");

//parse log file
importerUsingParser.importLogs(in, dataCollector, context);
+9

, , -, Log4j (, %-5p [%t]: %m%n) , , .

Essentially, I'm looking for the flip side of the Layout.format(LoggingEvent)Layout base class method for Log4j. This method creates a string that is stored in a log file, so the reverse operation can parse this file and create LoggingEvent objects or something similar (something like a deserialization procedure).

Thank!

EDIT: Converting is simple just by doing

LogFilePatternLayoutBuilder.getLogFormatFromPatternLayout(conversionPattern);

through the Chainsaw class org.apache.log4j.chainsaw.LogFilePatternLayoutBuilder.

+1
source

All Articles