You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-cvs@jakarta.apache.org by ce...@apache.org on 2001/01/08 02:37:56 UTC

cvs commit: jakarta-log4j/org/apache/log4j/test ShortSocketServer.java

ceki        01/01/07 17:37:56

  Modified:    .        FAQ.html
               org/apache/log4j Category.java PropertyConfigurator.java
               org/apache/log4j/net Makefile SocketNode.java
                        SocketServer.java
               org/apache/log4j/net/test socketServer.lcf
               org/apache/log4j/test ShortSocketServer.java
  Added:       org/apache/log4j/net SimpleSocketServer.java
  Log:
  - Enhanced SocketServer to use a different hiearchy for each client.
  - The old SocketServer is available in SimpleSocketServer.
  - SocketNode constructor now takes a Hiearchy parameter.
  
  Revision  Changes    Path
  1.4       +9 -20     jakarta-log4j/FAQ.html
  
  Index: FAQ.html
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/FAQ.html,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FAQ.html	2000/12/27 23:27:03	1.3
  +++ FAQ.html	2001/01/08 01:37:55	1.4
  @@ -45,11 +45,13 @@
   <li><p><a href=#tax>Why should I donate my log4j extensions back to the
   project?</a>
   <li><p><a href=#help>What should I keep in mind when contributing code?</a>
  -<li><p><a href=#numberingPolicy>What is the version numbering policy for
  -log4j?</a><br>
  +
   <li><p><a href=#bugCorrection>How fast do bugs in log4j get fixed?</a>
  +
   <li><p><a href=#history>What is the history of log4j?</a>
  +
   <li><p><a href=#bugs>How do I report bugs?</a>
  +
   <li><p><a href=#download>Where can I find the latest distribution of log4j?</a>
   
   </ul>
  @@ -528,11 +530,10 @@
   <a name=tax><h4>Why should I donate my extensions to log4j back to the
   project?</h4>
   
  -Contrary to the GNU Public License (GPL) our license does not have a
  -clause forcing you to donate your own log4j extensions back to the
  -log4j project. <em>You are free to do whatever you wish with your
  -proprietary log4j extensions.</em> In particlular, you may choose to
  -never release your extensions to the wider public.
  +Contrary to the GNU Public License (GPL) our license does not make any
  +claims to your extensions. <em>You are free to do whatever you wish
  +with your proprietary log4j extensions.</em> In particlular, you may
  +choose to never release your extensions to the wider public.
   
   <p>We are very careful not to change the log4j client API so that
   newer log4j releases are backward compatible with previous
  @@ -581,7 +582,7 @@
          <p>It's all about the application not about logging.
   
       <p><li>Identify yourself as the contributor at the top of the
  -	relevant file. Add a copyright statement if appropriate.
  +	relevant file. 
   
       <p><li>Take responsibility for your code.
   	
  @@ -589,18 +590,6 @@
   	years to raise a child. 
   	
   </ol>
  -
  -<a name=numberingPolicy><h4>What is the version numbering policy for
  -log4j?</h4>
  -
  -<p>As the package us still undergoing rapid development, log4j version
  -numbers start with zero. Increments in the second digit mean that
  -there has been a significant enhacement or even an interface
  -change. The third digit is incremented in case of smaller changes.
  -
  -<p>Releases consisting of only bug fixes, do not get a completely new
  -version number but a letter is attached to the third number,
  -e.g. 0.8.3a, 0.8.3b, and so on.
   
   <a name=bugCorrection><h4>How fast do bugs in log4j get fixed?</h4>
   
  
  
  
  1.7       +2 -1      jakarta-log4j/org/apache/log4j/Category.java
  
  Index: Category.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/org/apache/log4j/Category.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Category.java	2001/01/07 21:10:56	1.6
  +++ Category.java	2001/01/08 01:37:55	1.7
  @@ -69,7 +69,8 @@
     // an undescore.
     static 
     public 
  -  final Hierarchy defaultHierarchy = new Hierarchy(new RootCategory(Priority.DEBUG));
  +  final Hierarchy defaultHierarchy = new Hierarchy(new 
  +						   RootCategory(Priority.DEBUG));
   
     
     protected ResourceBundle resourceBundle;
  
  
  
  1.6       +3 -2      jakarta-log4j/org/apache/log4j/PropertyConfigurator.java
  
  Index: PropertyConfigurator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/org/apache/log4j/PropertyConfigurator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PropertyConfigurator.java	2001/01/07 21:10:56	1.5
  +++ PropertyConfigurator.java	2001/01/08 01:37:55	1.6
  @@ -580,8 +580,9 @@
         LogLog.debug("Parsed \"" + appenderName +"\" options.");
         if(appender.requiresLayout()) {
   	Layout layout = (Layout) OptionConverter.instantiateByKey(props, 
  -						layoutPrefix,
  -						org.apache.log4j.Layout.class, null);
  +								  layoutPrefix,
  +								  Layout.class, 
  +								  null);
   	if(layout != null) {
   	  appender.setLayout(layout);
   	  LogLog.debug("Parsing layout options for \"" + appenderName +"\".");
  
  
  
  1.5       +1 -0      jakarta-log4j/org/apache/log4j/net/Makefile
  
  Index: Makefile
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/org/apache/log4j/net/Makefile,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Makefile	2000/12/21 17:59:50	1.4
  +++ Makefile	2001/01/08 01:37:56	1.5
  @@ -5,6 +5,7 @@
   	JMSAppender.java\
   	JMSSink.java\
   	SocketServer.java\
  +	SimpleSocketServer.java\
   	SocketAppender.java\
   	SMTPAppender.java\
   	SocketNode.java\
  
  
  
  1.4       +5 -2      jakarta-log4j/org/apache/log4j/net/SocketNode.java
  
  Index: SocketNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/org/apache/log4j/net/SocketNode.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SocketNode.java	2001/01/05 21:08:57	1.3
  +++ SocketNode.java	2001/01/08 01:37:56	1.4
  @@ -17,6 +17,7 @@
   
   
   import org.apache.log4j.Category;
  +import org.apache.log4j.Hierarchy;
   import org.apache.log4j.spi.LoggingEvent;
   import org.apache.log4j.Priority;
   import org.apache.log4j.NDC;
  @@ -38,13 +39,15 @@
   public class SocketNode implements Runnable {
   
     Socket socket;
  +  Hierarchy hierarchy;
     ObjectInputStream ois;
   
     static Category cat = Category.getInstance(SocketNode.class.getName());
   
     public 
  -  SocketNode(Socket socket) {
  +  SocketNode(Socket socket, Hierarchy hierarchy) {
       this.socket = socket;
  +    this.hierarchy = hierarchy;
       try {
         ois = new ObjectInputStream(socket.getInputStream());
       }
  @@ -66,7 +69,7 @@
       try {
         while(true) {	
   	event = (LoggingEvent) ois.readObject();	
  -	remoteCategory = Category.getInstance(event.categoryName);
  +	remoteCategory = hierarchy.getInstance(event.categoryName);
   	if(event.priority.isGreaterOrEqual(remoteCategory.getChainedPriority())) {
   	  remoteCategory.callAppenders(event);	
   	}
  
  
  
  1.4       +98 -10    jakarta-log4j/org/apache/log4j/net/SocketServer.java
  
  Index: SocketServer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/org/apache/log4j/net/SocketServer.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SocketServer.java	2000/12/14 21:07:56	1.3
  +++ SocketServer.java	2001/01/08 01:37:56	1.4
  @@ -9,10 +9,17 @@
   
   import java.net.Socket;
   import java.net.ServerSocket;
  +import java.net.InetAddress;
  +import java.net.MalformedURLException;
   import java.io.IOException;
  +import java.io.File;
  +import java.util.Hashtable;
   
   import org.apache.log4j.Category;
  +import org.apache.log4j.Hierarchy;
  +import org.apache.log4j.Priority;
   import org.apache.log4j.PropertyConfigurator;
  +import org.apache.log4j.spi.RootCategory;
   
   
   /**
  @@ -32,17 +39,28 @@
   
   public class SocketServer  {
   
  -  static Category cat = Category.getInstance(SocketServer.class.getName());
  +  static String DEFAULT_PREFIX = "default";
  +  static String CONFIG_FILE_EXT = ".lcf";
   
  +  static Category cat = Category.getInstance(SocketServer.class.getName());  
  +  static SocketServer server;
     static int port;
  -  
  +
  +  // key=inetAddress, value=hierarchy
  +  static Hashtable hierarchyMap = new Hashtable(11);
  +  static Hierarchy defaultHierarchy;
  +  static File dir;
  +
  +
     public 
     static 
     void main(String argv[]) {
  -    if(argv.length == 2) 
  -      init(argv[0], argv[1]);
  +    if(argv.length == 3) 
  +      init(argv[0], argv[1], argv[2]);
       else 
         usage("Wrong number of arguments.");     
  +  
  +    
       
       try {
         cat.info("Listening on port " + port);
  @@ -50,9 +68,16 @@
         while(true) {
   	cat.info("Waiting to accept a new client.");
   	Socket socket = serverSocket.accept();
  -	cat.info("Connected to client at " + socket.getInetAddress());
  +	InetAddress inetAddress =  socket.getInetAddress();
  +	cat.info("Connected to client at " + inetAddress);
  +
  +	Hierarchy h = (Hierarchy) hierarchyMap.get(inetAddress);
  +	if(h == null) {
  +	  h = configureHierarchy(inetAddress);
  +	} 
  +
   	cat.info("Starting new socket node.");	
  -	new Thread(new SocketNode(socket)).start();
  +	new Thread(new SocketNode(socket, Category.getDefaultHierarchy())).start();
         }
       }
       catch(Exception e) {
  @@ -65,19 +90,82 @@
     void  usage(String msg) {
       System.err.println(msg);
       System.err.println(
  -      "Usage: java " +SocketServer.class.getName() + " port configFile");
  +      "Usage: java " +SocketServer.class.getName() + " port configFile directory");
       System.exit(1);
     }
       
     static
  -  void init(String portStr, String configFile) {
  +  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);
  +    
  +    
  +    dir = new File(dirStr);    
  +    if(!dir.isDirectory()) {
  +      usage("["+dirStr+"] is not a directory.");
  +    }
  +  }
  +
  +
  +
  +  // This method assumes that there is no hiearchy for inetAddress
  +  // yet. It will configure one and return it.
  +  static
  +  Hierarchy configureHierarchy(InetAddress inetAddress) {
  +    // We assume that the toSting 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("/");
  +    if(i == -1) {
  +      cat.warn("Could not parse the inetAddress ["+inetAddress+
  +	       "]. Using default hierarchy.");
  +      return defaultHierarchy();
  +    } else {
  +      String key = s.substring(0, i);
  +      System.out.println("key = "+key);
  +      
  +      File configFile = new File(dir, key+CONFIG_FILE_EXT);
  +      if(configFile.exists()) {
  +	Hierarchy h = new Hierarchy(new RootCategory(Priority.DEBUG));
  +	hierarchyMap.put(inetAddress, h);
  +	
  +	try {
  +	  new PropertyConfigurator().doConfigure(configFile.toURL(), h);
  +	} catch(MalformedURLException e) {
  +	  cat.error("Could not convert"+configFile+" to a URL.", e);
  +	}
  +	return h;	
  +      } else {
  +	cat.warn("Could not find config file ["+configFile+"].");
  +	return defaultHierarchy();
  +      }
  +    }
  +  }
  +
  +  static
  +  Hierarchy defaultHierarchy() {
  +    if(defaultHierarchy == null) {
  +      File f = new File(dir, DEFAULT_PREFIX+CONFIG_FILE_EXT);
  +      if(f.exists()) {
  +	defaultHierarchy = new Hierarchy(new RootCategory(Priority.DEBUG));
  +	try {
  +	  new PropertyConfigurator().doConfigure(f.toURL(),
  +					       defaultHierarchy);
  +	} catch(MalformedURLException e) {
  +	  cat.error("Could not convert"+f+" to a URL.", e);
  +	}
  +      } else {
  +	cat.warn("Could not find config file ["+f+"].");
  +	defaultHierarchy = Category.getDefaultHierarchy();
  +      }
       }
  -    PropertyConfigurator.configure(configFile);    
  +    return defaultHierarchy;
     }
   }
  
  
  
  1.1                  jakarta-log4j/org/apache/log4j/net/SimpleSocketServer.java
  
  Index: SimpleSocketServer.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software
   * License version 1.1, a copy of which has been included with this
   * distribution in the LICENSE.APL file.  */
  
  package org.apache.log4j.net;
  
  import java.net.Socket;
  import java.net.ServerSocket;
  import java.io.IOException;
  
  import org.apache.log4j.Category;
  import org.apache.log4j.PropertyConfigurator;
  
  
  /**
     A simple {@link SocketNode} based server.
  
     <pre>
       <b>Usage:</b> java org.apache.log4j.net.SimpleSocketServer port configFile
  
       where <em>port</em> is a part number where the server listens and
       <em>configFile</em> is a configuration file fed to the {@link
       PropertyConfigurator}.
     </pre>
  
      @author  Ceki G&uuml;lc&uuml;
   
      @since 0.8.4 */
  
  public class SimpleSocketServer  {
  
    static Category cat = Category.getInstance(SimpleSocketServer.class.getName());
  
    static int port;
    
    public 
    static 
    void main(String argv[]) {
      if(argv.length == 2) 
        init(argv[0], argv[1]);
      else 
        usage("Wrong number of arguments.");     
      
      try {
        cat.info("Listening on port " + port);
        ServerSocket serverSocket = new ServerSocket(port);
        while(true) {
  	cat.info("Waiting to accept a new client.");
  	Socket socket = serverSocket.accept();
  	cat.info("Connected to client at " + socket.getInetAddress());
  	cat.info("Starting new socket node.");	
  	new Thread(new SocketNode(socket, 
  				  Category.getDefaultHierarchy())).start();
        }
      }
      catch(Exception e) {
        e.printStackTrace();
      }
    }
  
    
    static
    void  usage(String msg) {
      System.err.println(msg);
      System.err.println(
        "Usage: java " +SimpleSocketServer.class.getName() + " port configFile");
      System.exit(1);
    }
      
    static
    void init(String portStr, String configFile) {
      try {
        port = Integer.parseInt(portStr);      
      }
      catch(java.lang.NumberFormatException e) {
        e.printStackTrace();
        usage("Could not interpret port number ["+ portStr +"].");
      }
      PropertyConfigurator.configure(configFile);    
    }
  }
  
  
  
  1.3       +2 -2      jakarta-log4j/org/apache/log4j/net/test/socketServer.lcf
  
  Index: socketServer.lcf
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/org/apache/log4j/net/test/socketServer.lcf,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- socketServer.lcf	2000/12/26 23:39:07	1.2
  +++ socketServer.lcf	2001/01/08 01:37:56	1.3
  @@ -1,9 +1,9 @@
   
  -log4j.rootCategory=, R
  +log4j.rootCategory=, A1
   log4j.appender.R=org.apache.log4j.performance.NullAppender
   
   
  -log4j.category.org.apache.log4j.net.SocketServer=, A1
  +#log4j.category.org.apache.log4j.net.SocketServer=
   log4j.appender.A1=org.apache.log4j.FileAppender
   log4j.appender.A1.File=System.out
   log4j.appender.A1.layout=org.apache.log4j.PatternLayout
  
  
  
  1.3       +1 -1      jakarta-log4j/org/apache/log4j/test/ShortSocketServer.java
  
  Index: ShortSocketServer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/org/apache/log4j/test/ShortSocketServer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ShortSocketServer.java	2000/12/14 21:08:13	1.2
  +++ ShortSocketServer.java	2001/01/08 01:37:56	1.3
  @@ -44,7 +44,7 @@
         Socket socket = serverSocket.accept();
         LogLog.debug("Connected to client at " + socket.getInetAddress());
         LogLog.debug("Starting new socket node.");	
  -      SocketNode sn = new SocketNode(socket);
  +      SocketNode sn = new SocketNode(socket, Category.getDefaultHierarchy());
         Thread t = new Thread(sn);
         t.start(); 
         t.join();