Hello,

Apologies in advance if this is not the correct list. 

I see no clear cut way to retrieve the remost hostname
of  my log4j clients using the JMSAppender at the
message consumer(s). 

I guess there are two quick ways of doing it, using
MDC or using a JMS object message's string property.
Latter might actually be a little more lightweight but
for now I have used the former. 

I'm not really familiar with the log4j internals but
seeing as the MDC is set per thread and I assume only
one instance of the JMSAppender is instantiated from a
static initializer somewhere, the JMSAppender
constructor would be the right place to do this. All
threads should hopefully inherit this. 

I still need to profile both approaches and see which
is most efficient, every bit of performance is
important to us in this situation. Incidentally, I've
found LoggingEvent.getThrowableStrRep() to be a huge
performance hit, nearly halved our throughput, it
seems that it's performed in a rather convoluted way
which I don't know the reasoning behind and could be
done a bit faster using a simple StringBuffer. But I
digress...

Attached is a basic patch.

Thanks
Peter
--- logging-log4j-1.2.12.orig/src/java/org/apache/log4j/net/JMSAppender.java	2005-08-29 22:28:12.000000000 -0700
+++ logging-log4j-1.2.12/src/java/org/apache/log4j/net/JMSAppender.java	2005-09-27 11:01:54.000000000 -0700
@@ -16,24 +16,29 @@
 
 package org.apache.log4j.net;
 
-import org.apache.log4j.AppenderSkeleton;
-import org.apache.log4j.spi.LoggingEvent;
-import org.apache.log4j.spi.ErrorCode;
-import org.apache.log4j.helpers.LogLog;
-
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Hashtable;
 import java.util.Properties;
+
+import javax.jms.ObjectMessage;
+import javax.jms.Session;
+import javax.jms.Topic;
 import javax.jms.TopicConnection;
 import javax.jms.TopicConnectionFactory;
-import javax.jms.Topic;
 import javax.jms.TopicPublisher;
 import javax.jms.TopicSession;
-import javax.jms.Session;
-import javax.jms.ObjectMessage;
-import javax.naming.InitialContext;
 import javax.naming.Context;
+import javax.naming.InitialContext;
 import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
 
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.MDC;
+import org.apache.log4j.helpers.LogLog;
+import org.apache.log4j.spi.ErrorCode;
+import org.apache.log4j.spi.LoggingEvent;
+
 /**
  * A simple appender that publishes events to a JMS Topic. The events
  * are serialized and transmitted as JMS message type [EMAIL PROTECTED]
@@ -98,6 +103,8 @@
 
    @author Ceki Gülcü */
 public class JMSAppender extends AppenderSkeleton {
+	
+	public static final String HOSTNAME_PROPERTY = "hostname";
 
   String securityPrincipalName;
   String securityCredentials;
@@ -109,6 +116,8 @@
   String userName;
   String password;
   boolean locationInfo;
+  
+  private String hostname;
 
   TopicConnection  topicConnection;
   TopicSession topicSession;
@@ -116,6 +125,15 @@
 
   public
   JMSAppender() {
+	try
+	{
+		hostname = InetAddress.getLocalHost().getHostName();
+		MDC.put(HOSTNAME_PROPERTY, hostname);
+	}
+	catch (UnknownHostException e)
+	{
+		hostname = "unknownhost";
+	}
   }
 
   /**

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to