sdeboy 2004/03/16 20:19:37
Modified: src/java/org/apache/log4j/varia
LogFilePatternReceiverBeanInfo.java
LogFilePatternReceiver.java
Log:
- Added 'looping' param which allows the receiver to continuously check the file for
new events and post them to the framework
(tail functionality)
- Added checks to prevent and log parsing exceptions
Revision Changes Path
1.4 +1 -0
logging-log4j/src/java/org/apache/log4j/varia/LogFilePatternReceiverBeanInfo.java
Index: LogFilePatternReceiverBeanInfo.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/varia/LogFilePatternReceiverBeanInfo.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- LogFilePatternReceiverBeanInfo.java 2 Mar 2004 07:38:20 -0000 1.3
+++ LogFilePatternReceiverBeanInfo.java 17 Mar 2004 04:19:37 -0000 1.4
@@ -38,6 +38,7 @@
new PropertyDescriptor("timestampFormat",
LogFilePatternReceiver.class),
new PropertyDescriptor("logFormat", LogFilePatternReceiver.class),
new PropertyDescriptor("name", LogFilePatternReceiver.class),
+ new PropertyDescriptor("looping", LogFilePatternReceiver.class),
};
} catch (Exception e) {
}
1.4 +120 -62
logging-log4j/src/java/org/apache/log4j/varia/LogFilePatternReceiver.java
Index: LogFilePatternReceiver.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/varia/LogFilePatternReceiver.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- LogFilePatternReceiver.java 2 Mar 2004 07:38:20 -0000 1.3
+++ LogFilePatternReceiver.java 17 Mar 2004 04:19:37 -0000 1.4
@@ -19,6 +19,7 @@
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.Constants;
+import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.plugins.Receiver;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
@@ -135,6 +136,7 @@
private String logFormat;
private String fileName;
private String shortFileName;
+ private boolean looping;
/**
* Creates a new LogFilePatternReceiver object.
@@ -230,6 +232,24 @@
/**
* Accessor
*
+ * @return looping
+ */
+ public boolean isLooping() {
+ return looping;
+ }
+
+ /**
+ * Mutator
+ *
+ * @param looping
+ */
+ public void setLooping(boolean looping) {
+ this.looping = looping;
+ }
+
+ /**
+ * Accessor
+ *
* @return log format
*/
public String getLogFormat() {
@@ -253,7 +273,7 @@
public void setTimestampFormat(String timestampFormat) {
this.timestampFormat = timestampFormat;
}
-
+
/**
* Accessor
*
@@ -275,85 +295,102 @@
LinkedList list = new LinkedList();
String line = null;
- while ((line = reader.readLine()) != null) {
- //ignore blank lines in file
- if (line.length() == 0) {
- continue;
- }
+ do {
+ while ((line = reader.readLine()) != null) {
+ //ignore blank lines in file
+ if (line.length() == 0) {
+ continue;
+ }
+
+ //System.out.println("Added: " + line);
+ list.addLast(line);
- //System.out.println("Added: " + line);
- list.addLast(line);
+ if (list.size() > 2) {
+ //System.out.println("size > 2 - processing");
+ if (line.startsWith(TAB)) {
+ String firstLine = (String) list.remove(0);
+
+ //System.out.println("exception line1: " + firstLine);
+ while ((line != null) && line.startsWith(TAB)) {
+ line = reader.readLine();
+
+ if ((line != null) && (line.length() != 0)) {
+ list.addLast(line);
+ }
+ }
- if (list.size() > 2) {
- //System.out.println("size > 2 - processing");
- if (line.startsWith(TAB)) {
- String firstLine = (String) list.remove(0);
-
- //System.out.println("exception line1: " + firstLine);
- while ((line != null) && line.startsWith(TAB)) {
- line = reader.readLine();
+ //hold a reference to the last entry added - while loop adds one too
many
+ String nextLine = (String) list.getLast();
- if ((line != null) && (line.length() != 0)) {
- list.addLast(line);
+ if ((nextLine != null) && !(nextLine.startsWith(TAB))) {
+ list.removeLast();
}
- }
- //hold a reference to the last entry added - while loop adds one too many
- String nextLine = (String) list.getLast();
+ String[] exception = new String[list.size()];
- if ((nextLine != null) && !(nextLine.startsWith(TAB))) {
- list.removeLast();
- }
+ for (int i = 0, j = list.size(); i < j; i++) {
+ exception[i] = (String) list.remove(0);
- String[] exception = new String[list.size()];
+ //System.out.println("exception " + i + ".." + exception[i]);
+ }
- for (int i = 0, j = list.size(); i < j; i++) {
- exception[i] = (String) list.remove(0);
+ //now that exceptions have been taken from list, re-add last entry if
not an
+ //exception line
+ if ((nextLine != null) && !(nextLine.startsWith(TAB))) {
+ list.addLast(nextLine);
+ }
- //System.out.println("exception " + i + ".." + exception[i]);
- }
+ //GENERATE EXCEPTION EVENT
+ LoggingEvent event = convertToEvent(firstLine, exception);
+
+ //System.out.println(
+ // "created event with exception " + event.getLoggerName() + ".."
+ // + event.getMessage());
+ if (event != null) {
+ doPost(event);
+ }
+ } else {
+ //GENERATE NON-EXCEPTION EVENT
+ LoggingEvent event = convertToEvent((String) list.remove(0));
+
+ if (event != null) {
+ doPost(event);
+ }
- //now that exceptions have been taken from list, re-add last entry if not
an
- //exception line
- if ((nextLine != null) && !(nextLine.startsWith(TAB))) {
- list.addLast(nextLine);
+ //System.out.println(
+ // "Created event " + event.getLoggerName() + ".."
+ // + event.getMessage());
}
+ }
+ }
- //GENERATE EXCEPTION EVENT
- LoggingEvent event = convertToEvent(firstLine, exception);
+ //System.out.println(
+ // "outside loop - processing remaining entries: " + list.size());
+ //clean up remaining - should not be an exception
+ for (int k = 0, l = list.size(); k < l; k++) {
+ String s = (String) list.remove(0);
- //System.out.println(
- // "created event with exception " + event.getLoggerName() + ".."
- // + event.getMessage());
- doPost(event);
- } else {
+ if ((s != null) && (s.length() > 0)) {
//GENERATE NON-EXCEPTION EVENT
- LoggingEvent event = convertToEvent((String) list.remove(0));
+ LoggingEvent event = convertToEvent(s);
+
+ if (event != null) {
+ doPost(event);
+ }
//System.out.println(
- // "Created event " + event.getLoggerName() + ".."
- // + event.getMessage());
- doPost(event);
+ // "cleanup - Created non-exception event " + event.getLoggerName()
+ // + ".." + event.getMessage());
}
}
- }
-
- //System.out.println(
- // "outside loop - processing remaining entries: " + list.size());
- //clean up remaining - should not be an exception
- for (int k = 0, l = list.size(); k < l; k++) {
- String s = (String) list.remove(0);
-
- if ((s != null) && (s.length() > 0)) {
- //GENERATE NON-EXCEPTION EVENT
- LoggingEvent event = convertToEvent(s);
- //System.out.println(
- // "cleanup - Created non-exception event " + event.getLoggerName()
- // + ".." + event.getMessage());
- doPost(event);
+ try {
+ synchronized (this) {
+ wait(2000);
+ }
+ } catch (InterruptedException ie) {
}
- }
+ } while (looping);
}
/**
@@ -374,10 +411,23 @@
//ignore wildcard entries - skip to next field
if (thisField.equals(WILDCARD)) {
String nextField = (String) logFormatFields.get(i + 1);
+
+ if (logEntry.indexOf(nextField) == -1) {
+ LogLog.info("Couldn't process line, ignoring: " + logEntry);
+
+ return null;
+ }
+
logEntry = logEntry.substring(logEntry.indexOf(nextField));
} else if (thisField.equals(TIMESTAMP)) {
String nextField = (String) logFormatFields.get(i + 1);
+ if (logEntry.indexOf(nextField) == -1) {
+ LogLog.info("Couldn't process line, ignoring: " + logEntry);
+
+ return null;
+ }
+
//uses nextfield and length of format to guess at timestamp field
//luckily, SimpleDateFormat is very flexible and forgiving of trailing text
int firstLength =
@@ -401,8 +451,12 @@
//expects no two keywords to be butted up directly against eachother
String nextField = (String) logFormatFields.get(i + 1);
- //System.out.println(
- // thisField + " = " + logEntry.substring(0,
logEntry.indexOf(nextField)).trim());
+ if (logEntry.indexOf(nextField) == -1) {
+ LogLog.info("Couldn't process line, ignoring: " + logEntry);
+
+ return null;
+ }
+
fieldMap.put(
thisField,
logEntry.substring(0, logEntry.indexOf(nextField)).trim());
@@ -459,6 +513,10 @@
* @return logging event
*/
private LoggingEvent convertToEvent(Map fieldMap, String[] exception) {
+ if (fieldMap == null) {
+ return null;
+ }
+
Logger logger = null;
long timeStamp = 0L;
String level = null;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]