Hi.

I'm using Log4j in an enterprise logging solution and interestingly, the
_only_ extension that we need to make is to allow applications to log their
hostname, preferably in PatternLayout. While I could extend PatternParser
to do it, I would then have to extend PatternLayout so it would use the new
PatternParser. And then I'd have to extend JDBCAppender so it would use the
new PatternLayout. And I find that extra maintenance to be stupid when all
I want is a specifier for the hostname, which I think should have been
there in the first place.

Thus, I patched the latest CVS tree to do exactly that. Now, %h in
PatternLayout will print the hostname. Hopefully, it'll make its way into
the next point release and then I can just use stock Log4j without needing
to extend things.

Thanks

Dan


diff -ru orig/src/java/org/apache/log4j/helpers/PatternParser.java 
src/java/org/apache/log4j/helpers/PatternParser.java
--- orig/src/java/org/apache/log4j/helpers/PatternParser.java     2002-10-09 
18:50:03.000000000 -0400
+++ src/java/org/apache/log4j/helpers/PatternParser.java    2002-12-05 
+12:19:13.000000000 -0500
@@ -56,6 +56,7 @@
   static final int LEVEL_CONVERTER = 2002;
   static final int NDC_CONVERTER = 2003;
   static final int MESSAGE_CONVERTER = 2004;
+  static final int HOSTNAME_CONVERTER = 2005;

   int state;
   protected StringBuffer currentLiteral = new StringBuffer(32);
@@ -277,6 +278,12 @@
       //formattingInfo.dump();
       currentLiteral.setLength(0);
       break;
+    case 'h':
+      pc = new BasicPatternConverter(formattingInfo, HOSTNAME_CONVERTER);
+      //LogLog.debug("Hostname converter.");
+      //formattingInfo.dump();
+      currentLiteral.setLength(0);
+      break;
     case 'l':
       pc = new LocationPatternConverter(formattingInfo,
                              FULL_LOCATION_CONVERTER);
@@ -391,6 +398,8 @@
      return event.getLevel().toString();
       case NDC_CONVERTER:
      return event.getNDC();
+      case HOSTNAME_CONVERTER:
+        return event.getHostname();
       case MESSAGE_CONVERTER: {
      return event.getRenderedMessage();
       }
diff -ru orig/src/java/org/apache/log4j/spi/LoggingEvent.java 
src/java/org/apache/log4j/spi/LoggingEvent.java
--- orig/src/java/org/apache/log4j/spi/LoggingEvent.java    2002-10-21 
19:20:34.000000000 -0400
+++ src/java/org/apache/log4j/spi/LoggingEvent.java   2002-12-06 11:03:44.000000000 
+-0500
@@ -102,6 +102,9 @@
   /** The name of thread in which this logging event was generated. */
   private String threadName;

+  /** The name of the host on which this logging event was generated. */
+  private String hostName;
+
   /** This
       variable contains information about this event's throwable
   */
@@ -142,6 +145,13 @@
     this.categoryName = logger.getName();
     this.level = priority;
     this.message = message;
+
+    try {
+      this.hostName = java.net.InetAddress.getLocalHost().getHostName();
+    } catch (java.net.UnknownHostException uhe) {
+      this.hostName = "*HOSTNAME UNKNOWN*";
+    }
+
     if (throwable != null) {
       this.throwableInfo = new ThrowableInformation(throwable);
     }
@@ -171,6 +181,13 @@
     this.categoryName = logger.getName();
     this.level = priority;
     this.message = message;
+
+    try {
+      this.hostName = java.net.InetAddress.getLocalHost().getHostName();
+    } catch (java.net.UnknownHostException uhe) {
+      this.hostName = "*HOSTNAME UNKNOWN*";
+    }
+
     if (throwable != null) {
       this.throwableInfo = new ThrowableInformation(throwable);
     }
@@ -190,6 +207,13 @@
   }

   /**
+   * Return the hostname of this event. Use this form instead of directly
+   * accessing the <code>hostName</code> field. */
+  public String getHostname() {
+    return hostName;
+  }
+
+  /**
    * Return the level of this event. Use this form instead of directly
    * accessing the <code>level</code> field.  */
   public Level getLevel() {





-------------------------------------------------------------------------------
This message and any included attachments are from Siemens Medical Solutions 
Health Services Corporation and are intended only for the addressee(s).  
The information contained herein may include trade secrets or privileged or 
otherwise confidential information.  Unauthorized review, forwarding, printing, 
copying, distributing, or using such information is strictly prohibited and may 
be unlawful.  If you received this message in error, or have reason to believe 
you are not authorized to receive it, please promptly delete this message and 
notify the sender by e-mail with a copy to [EMAIL PROTECTED]  Thank you

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

Reply via email to