logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Raif Naffah" <RaifNaf...@forge.com.au>
Subject Re: ServerSocket that processes xml configuration data (long)
Date Mon, 29 Apr 2002 22:56:33 GMT

hello Log4J developers,

firstly thanks for making, and continue to keeping, this great product
available to the Java developers community.

secondly, i need a functionality similar to
org.apache.log4j.net.SocketServer
but that can understand and process .xml configuration files (i.e.
configure
with a DOMConfigurator).

i've amended the SocketServer class to do just that and would appreciate
it if these changes get incorporated in the standard release of Log4J.

here are the unified diffs:


----- diff -u build.xml.orig build.xml

--- build.xml.orig      Wed Apr 17 05:39:48 2002
+++ build.xml   Tue Apr 23 15:43:58 2002
@@ -9,9 +9,9 @@
 <!-- ================================================================= -->
 <project name="log4j" default="usage" basedir="." >

-
+
   <!-- The build.properties file defines the parth to local jar files -->
-  <property file="build.properties"/>
+  <property file="build.properties"/>

   <!-- Read the system environment variables and stores them in
properties, -->
   <!-- prefixed with "env". -->
@@ -87,11 +87,11 @@
     These are the targets supported by this ANT build scpript:

     build   - compile all project files, if a certain library is missing,
-             then the compilation of its dependents are skipped.
+              then the compilation of its dependents are skipped.

-    javadoc - build project javadoc files
+    javadoc - build project javadoc files.

-    jar     - build log4j-core and log4j jar files
+    jar     - build log4j-core and log4j jar files.

     dist    - will create a complete distribution in dist/
               Setting the env variable NO_JAVADOC will build the
distribution
@@ -170,7 +170,8 @@
   </target>

   <target name="build" depends="init, build.core, build.examples,
build.xml,
-                                build.unitTests, build.javamail,
build.jms, build.jmx"/>
+                                build.unitTests, build.javamail,
build.jms, build.jmx"
+          description="Compile all project files, if a certain library is
missing, then the compilation of its dependents are skipp
ed"/>

   <target name="build.core" depends="init">
     <mkdir dir="${javac.dest}" />
@@ -191,7 +192,7 @@
            debug="on">
       <classpath refid="compile.classpath"/>
     </javac>
-
+
   </target>

   <target name="build.examples" depends="build.core">
@@ -297,7 +298,8 @@
   <!-- Remove the temporary manifest file, actual work is done in the
-->
   <!-- dependencies.
-->
   <!-- =================================================================
-->
-  <target name="jar" depends="log4j.jar">
+  <target name="jar" depends="log4j.jar"
+          description="Build log4j-core and log4j jar files">
      <delete file="${jar.dest}/manifest.mf"/>
   </target>

@@ -342,7 +344,8 @@
   <!-- =================================================================
-->
   <!-- This target builds the javadoc files.
-->
   <!-- =================================================================
-->
-  <target name="javadoc" depends="init" unless="env.NO_JAVADOC">
+  <target name="javadoc" depends="init" unless="env.NO_JAVADOC"
+          description="Build project javadoc files">

     <mkdir dir="${javadoc.dest}" />

@@ -383,7 +386,8 @@
  <!-- =================================================================
-->
  <!-- Build a complete distribution. Results go to ${dist.images}
-->
  <!-- =================================================================
-->
-  <target name="dist" depends="init, clean, javadoc, jar">
+  <target name="dist" depends="init, clean, javadoc, jar"
+          description="Create a complete distribution in dist/. Setting
the 'env variable' NO_JAVADOC will build the distribution w
ithout running the 'javadoc' target">

     <delete verbose="true">
       <fileset dir=".">
@@ -440,15 +444,19 @@
                          **/*.bak, **/goEnv.bat,
                          **/Makefile, **/goEnv.bat,
                          docs/pub-support/*,
-                         src/java/org/apache/log4j/test/**/*,
+                         src/java/org/apache/log4j/test/**/*,
                          **/.#*"/>
     </copy>

-
+
+    <!--
     <fixcrlf srcdir="${dist.tmp}/jakarta-log4j-${version}"
              includes="build.sh" cr="remove"/>
     <fixcrlf srcdir="${dist.tmp}/jakarta-log4j-${version}"
-             includes="build.bat" cr="add"/>
+             includes="build.bat" cr="add"/>
+    -->
+    <fixcrlf srcdir="${dist.tmp}/jakarta-log4j-${version}"
+             includes="build.sh" eol="crlf"/>
     <chmod   dir="${dist.tmp}/jakarta-log4j-${version}"
              includes="build.sh" perm="+x"/>


----- diff -u SocketServer.java.orig SocketServer.java

--- SocketServer.java.orig      Wed Apr 17 05:39:48 2002
+++ SocketServer.java   Tue Apr 23 15:24:21 2002
@@ -6,18 +6,22 @@
  * distribution in the LICENSE.txt file.  */

 package org.apache.log4j.net;
-
-import java.net.Socket;
-import java.net.ServerSocket;
+
 import java.net.InetAddress;
-import java.net.MalformedURLException;
-import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
 import java.io.File;
 import java.util.Hashtable;

-import org.apache.log4j.*;
-import org.apache.log4j.spi.*;
-
+import org.apache.log4j.Category;
+import org.apache.log4j.Hierarchy;
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Priority;
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.log4j.xml.DOMConfigurator;
+import org.apache.log4j.spi.LoggerRepository;
+import org.apache.log4j.spi.RootCategory;

 /**
    A {@link SocketNode} based server that uses a different hierarchy
@@ -56,7 +60,7 @@

      <p>Having different client hosts log using different hierarchies
      ensures the total independence of the clients with respect to
-     their logging settings.
+     their logging settings.

      <p>Currently, the hierarchy that will be used for a given request
      depends on the IP address of the client host. For example, two
@@ -67,17 +71,21 @@
      as an example to be enhanced in order to implement more elaborate
      policies.

-
+
     @author  Ceki G&uuml;lc&uuml;
-
+
     @since 1.0 */

 public class SocketServer  {
+
+  // Constants and variables
+  //
-------------------------------------------------------------------------

   static String GENERIC = "generic";
-  static String CONFIG_FILE_EXT = ".lcf";
+  static String CONFIG_FILE_EXT = ".lcf";
+  static String XML_CONFIG_FILE_EXT = ".xml";

-  static Category cat = Category.getInstance(SocketServer.class);
+  static Category cat = Category.getInstance(SocketServer.class);
   static SocketServer server;
   static int port;

@@ -85,15 +93,27 @@
   Hashtable hierarchyMap;
   LoggerRepository genericHierarchy;
   File dir;
+
+  // Constructor(s)
+  //
-------------------------------------------------------------------------
+
+  public
+  SocketServer(File directory) {
+    this.dir = directory;
+    hierarchyMap = new Hashtable(11);
+  }
+
+  // Class methods
+  //
-------------------------------------------------------------------------

-  public
-  static
+  public
+  static
   void main(String argv[]) {
-    if(argv.length == 3)
+    if(argv.length == 3)
       init(argv[0], argv[1], argv[2]);
-    else
-      usage("Wrong number of arguments.");
-
+    else
+      usage("Wrong number of arguments.");
+
     try {
       cat.info("Listening on port " + port);
       ServerSocket serverSocket = new ServerSocket(port);
@@ -106,9 +126,9 @@
        LoggerRepository h = (LoggerRepository)
server.hierarchyMap.get(inetAddress);
        if(h == null) {
          h = server.configureHierarchy(inetAddress);
-       }
+       }

-       cat.info("Starting new socket node.");
+       cat.info("Starting new socket node.");
        new Thread(new SocketNode(socket, h)).start();
       }
     }
@@ -117,7 +137,6 @@
     }
   }

-
   static
   void  usage(String msg) {
     System.err.println(msg);
@@ -125,38 +144,38 @@
       "Usage: java " +SocketServer.class.getName() + " port configFile
directory");
     System.exit(1);
   }
-
+
   static
   void init(String portStr, String configFile, String dirStr) {
     try {
-      port = Integer.parseInt(portStr);
+      port = Integer.parseInt(portStr);
     }
     catch(java.lang.NumberFormatException e) {
       e.printStackTrace();
       usage("Could not interpret port number ["+ portStr +"].");
     }
-
-    PropertyConfigurator.configure(configFile);
-
-    File dir = new File(dirStr);
+
+//    PropertyConfigurator.configure(configFile);
+    if (configFile.endsWith(XML_CONFIG_FILE_EXT))
+      DOMConfigurator.configure(configFile);
+    else
+      PropertyConfigurator.configure(configFile);
+
+    File dir = new File(dirStr);
     if(!dir.isDirectory()) {
       usage("["+dirStr+"] is not a directory.");
     }
     server = new SocketServer(dir);
   }
-
-
-  public
-  SocketServer(File directory) {
-    this.dir = directory;
-    hierarchyMap = new Hashtable(11);
-  }
+
+  // Instance methods
+  //
-------------------------------------------------------------------------

   // This method assumes that there is no hiearchy for inetAddress
   // yet. It will configure one and return it.
   LoggerRepository configureHierarchy(InetAddress inetAddress) {
     cat.info("Locating configuration file for "+inetAddress);
-    // We assume that the toSting method of InetAddress returns is in
+    // We assume that the toString method of InetAddress returns is in
     // the format hostname/d1.d2.d3.d4 e.g. torino/192.168.1.1
     String s = inetAddress.toString();
     int i = s.indexOf("/");
@@ -166,17 +185,29 @@
       return genericHierarchy();
     } else {
       String key = s.substring(0, i);
-
+
       File configFile = new File(dir, key+CONFIG_FILE_EXT);
       if(configFile.exists()) {
        Hierarchy h = new Hierarchy(new RootCategory((Level)
Priority.DEBUG));
        hierarchyMap.put(inetAddress, h);
-
+
        new PropertyConfigurator().doConfigure(configFile.getAbsolutePath
(), h);

-       return h;
+       return h;
       } else {
-       cat.warn("Could not find config file ["+configFile+"].");
+       cat.warn("Could not find config file ["+configFile+"].");
+
+        configFile = new File(dir, key + XML_CONFIG_FILE_EXT);
+        cat.warn("Will check if a configuration file named \""
+configFile+"\" exists.");
+        if (configFile.exists()) {
+          Hierarchy result =
+              new Hierarchy(new RootCategory((Level) Priority.DEBUG));
+          hierarchyMap.put(inetAddress, result);
+          new DOMConfigurator().doConfigure(configFile.getAbsolutePath(),
result);
+
+          return result;
+        }
+
        return genericHierarchy();
       }
     }
@@ -188,10 +219,19 @@
       if(f.exists()) {
        genericHierarchy = new Hierarchy(new RootCategory((Level)
Priority.DEBUG));
        new PropertyConfigurator().doConfigure(f.getAbsolutePath(),
genericHierarchy);
-      } else {
-       cat.warn("Could not find config file ["+f+
-                "]. Will use the default hierarchy.");
-       genericHierarchy = LogManager.getLoggerRepository();
+      } else {
+//     cat.warn("Could not find config file ["+f+"]. Will use the default
hierarchy.");
+       cat.warn("Could not find config file ["+f+"].");
+
+         f = new File(dir, GENERIC + XML_CONFIG_FILE_EXT);
+         cat.warn("Will check if a configuration file named \""+f+"\"
exists.");
+         if (f.exists()) {
+           genericHierarchy = new Hierarchy(new RootCategory((Level)
Priority.DEBUG));
+           new DOMConfigurator().doConfigure(f.getAbsolutePath(),
genericHierarchy);
+         } else {
+           cat.warn("Could not find config file \""+f+".\" Will use
default hierarchy.");
+       genericHierarchy = LogManager.getLoggerRepository();
+         }
       }
     }
     return genericHierarchy;


---- end diffs

thanks in anticipation + cheers;
rsn
-----------------------------------------------------------------------
Raif S. Naffah
Forge Research Pty. Limited < http://www.forge.com.au >
-----------------------------------------------------------------------
The message contains privileged and confidential information intended
only for the use of the addressee named above.  If you are not the
intended recipient of this message you must not disseminate, copy or
take any action in reliance on it.  If you have received this message
in error please notify the sender immediately.  Any views expressed in
this message are those of the individual sender, except where the
sender specifically states them to be the views of another (including
a Body Corporate).
-----------------------------------------------------------------------


--
To unsubscribe, e-mail:   <mailto:log4j-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:log4j-dev-help@jakarta.apache.org>


Mime
View raw message