logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r528026 - /logging/sandbox/log4j/receivers/src/main/java/org/apache/log4j/net/SocketNode.java
Date Thu, 12 Apr 2007 16:51:08 GMT
Author: carnold
Date: Thu Apr 12 09:51:07 2007
New Revision: 528026

URL: http://svn.apache.org/viewvc?view=rev&rev=528026
Log:
Bug 42102: Change to reflective invocation of LoggingEvent.setProperty

Modified:
    logging/sandbox/log4j/receivers/src/main/java/org/apache/log4j/net/SocketNode.java

Modified: logging/sandbox/log4j/receivers/src/main/java/org/apache/log4j/net/SocketNode.java
URL: http://svn.apache.org/viewvc/logging/sandbox/log4j/receivers/src/main/java/org/apache/log4j/net/SocketNode.java?view=diff&rev=528026&r1=528025&r2=528026
==============================================================================
--- logging/sandbox/log4j/receivers/src/main/java/org/apache/log4j/net/SocketNode.java (original)
+++ logging/sandbox/log4j/receivers/src/main/java/org/apache/log4j/net/SocketNode.java Thu
Apr 12 09:51:07 2007
@@ -17,23 +17,23 @@
 
 package org.apache.log4j.net;
 
+import org.apache.log4j.Logger;
+import org.apache.log4j.plugins.Pauseable;
+import org.apache.log4j.plugins.Receiver;
+import org.apache.log4j.spi.ComponentBase;
+import org.apache.log4j.spi.LoggerRepository;
+import org.apache.log4j.spi.LoggingEvent;
+
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
+import java.lang.reflect.Method;
 import java.net.Socket;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.log4j.Logger;
-import org.apache.log4j.plugins.Pauseable;
-import org.apache.log4j.plugins.Receiver;
-import org.apache.log4j.spi.ComponentBase;
-import org.apache.log4j.spi.LoggerRepository;
-import org.apache.log4j.spi.LoggingEvent;
-
-
 // Contributors:  Moses Hohman <mmhohman@rainbow.uchicago.edu>
 
 /**
@@ -68,6 +68,29 @@
      */
   private List listenerList = Collections.synchronizedList(new ArrayList());
 
+    /**
+     * Method descriptor for LoggingEvent.setProperty
+     *   which does not exist in log4j 1.2.14.
+     */
+  private static final Method LOGGING_EVENT_SET_PROPERTY =
+          getLoggingEventSetProperty();
+
+    /**
+     * Get method descriptor for LoggingEvent.setProperty
+     *   which does not exist in log4j 1.2.14.
+     * @return method descriptor or null if not supported.
+     */
+  private static Method getLoggingEventSetProperty() {
+      Method m = null;
+      try {
+          m = LoggingEvent.class.getMethod("setProperty",
+                          new Class[] { String.class, String.class });
+      } catch (NoSuchMethodException e) {
+          return null;
+      }
+      return m;
+  }
+
   /**
     Constructor for socket and logger repository.
    @param s socket
@@ -127,6 +150,29 @@
   }
 
     /**
+     * Set property in event.
+     * @param event event, may not be null.
+     * @param propName property name
+     * @param propValue property value
+     * @return true if property was set
+     */
+  private static boolean setEventProperty(
+          final LoggingEvent event,
+          final String propName,
+          final String propValue) {
+      if (LOGGING_EVENT_SET_PROPERTY != null) {
+          try {
+              LOGGING_EVENT_SET_PROPERTY.invoke(event,
+                      new Object[] { propName, propValue });
+              return true;
+          } catch (Exception e) {
+              return false;
+          }
+      }
+      return false;
+  }
+
+    /**
      * Deserialize events from socket until interrupted.
      */
   public void run() {
@@ -161,7 +207,7 @@
           event = (LoggingEvent) ois.readObject();
 
           // store the known remote info in an event property
-          event.setProperty("log4j.remoteSourceInfo", remoteInfo);
+          setEventProperty(event, "log4j.remoteSourceInfo", remoteInfo);
 
           // if configured with a receiver, tell it to post the event
           if (!isPaused()) {



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message