A log is composed of several log messages.
A log message contains :
Jalore extract messages using a description of the first line. When parsing a log, lines not matching the description are considered to be extra lines of the previous message.
Log messages are represented by interface LogMessage.
LogReader interface is at the heart of Jalore. You can use it to get messages from a log as shown in the following sample :
LogReader logreader; ... while(logreader.hasMoreMessages()) { LogMessage logMessage = logreader.nextMessage(); ... }
You can read a log using a log4j pattern thank to the class Log4JReader. The pattern must only describe the first line of messages or the beginning the first line. Thus, the pattern can end with %n but must not contain %n before end.
Pattern elements like %d, %c, etc are used to fill log messages' properties. See Log4JProperties javadoc to have a list of available property names.
Here is a sample on how to read a log4j file :
FileReader reader = new FileReader("test.log"); Log4JReader logReader = new Log4JReader(reader, "%d [%10X{myMdc}] %-5p %c{1} - %m%n"); while(logReader.hasMoreMessages()) { LogMessage message = logReader.nextMessage(); // %d property String timestamp = message.getProperty(Log4JProperties.TIMESTAMP); // MDC myMdc (%X{myMdc}) String myMdc = message.getProperty(Log4JProperties.PREFIX_MDC + "myMdc"); // %p String level = message.getProperty(Log4JProperties.LEVEL); // %c String category = message.getProperty(Log4JProperties.CATEGORY); // %m String rawMessage = message.getProperty(Log4JProperties.MESSAGE); }
You can read a log using a regular expression thank to the class RegexpLogReader. The pattern must only describe the first line of messages or a part of the first line. Thus, the pattern can end with $ but must not contain $ before end.
You can tell RegexpLogReader to convert regular expression groups into properties using a Map. Here is a sample :
FileReader reader = new FileReader("test.log"); String pattern = "^(([0-9]{1,3}\\.){4}) .*"; Map<String, Integer> propertyToGroup = new HashMap<String, Integer>(); // Message property "ipaddress" will be filled with first group value propertyToGroup.put("ipaddress", 1); RegexpLogReader logReader = new RegexpLogReader(reader, pattern, propertyToGroup); while(logReader.hasMoreMessages()) { LogMessage message = logReader.nextMessage(); String ipaddress = message.getProperty("ipaddress"); }