ceki        2004/12/30 12:44:50

  Added:       src/java/org/apache/ugli LoggerFactory.java ULogger.java
                        LoggerFactoryAdapter.java
               src/java/org/apache/ugli/impl JDK14LoggerFA.java
                        JDK14Logger.java NOPLoggerFA.java NOPLogger.java
  Removed:     src/java/org/apache/ugli NullLoggerRepository.java
                        LoggerRepository.java Logger.java NullLogger.java
  Log:
  Starting to make UGLI useful. Work in progress
  
  Revision  Changes    Path
  1.1                  logging-log4j/src/java/org/apache/ugli/LoggerFactory.java
  
  Index: LoggerFactory.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.ugli;
  
  import java.io.InputStream;
  import java.util.Properties;
  
  import org.apache.ugli.impl.NOPLoggerFA;
  
  
  
  /**
   * The <code>LoggerFactory</code> can produce Loggers for various logging 
APIs, 
   * most notably for log4j and JDK 1.4 logging.
   * 
   * @author Ceki G&uuml;lc&uuml;
   */
  public class LoggerFactory {
  
    static final String UGLI_PROPERTIES ="ugli.properties";
    static final String UGLI_FACTORY_ADAPTER_CLASS ="ugli.factoryAdapterClass";
    
    static LoggerFactoryAdapter adapter;
  
    static {
      // the following code assumes that the jar file containing LoggerFactory
      // has properly packaged a ugli.propertis file specifiying the adapter 
      // factory class to use
  
      ClassLoader cl = LoggerFactoryAdapter.class.getClassLoader();
      InputStream is = cl.getResourceAsStream(UGLI_PROPERTIES);
      if(is == null) {
        System.err.println("Could not retreive ugli.properties");
      } else {
        Properties props = null;
        try {
          props = new Properties();
          props.load(is);
          is.close();
        } catch(java.io.IOException ie) {
        }
      
        String adapterClassStr = props.getProperty(UGLI_FACTORY_ADAPTER_CLASS); 
        try {
          Class adapterClass = cl.loadClass(adapterClassStr);
          adapter = (LoggerFactoryAdapter) adapterClass.newInstance();
        } catch(ClassNotFoundException cnfe) {
          System.err.println("Could not find class ["+adapterClassStr+"]");
          cnfe.printStackTrace();
        } catch(Exception e) {
          System.err.println("Could not instantiate instance of class 
["+adapterClassStr+"]");
          e.printStackTrace();
        }
      }
      if(adapter == null) {
        // TODO consider falling back on something more meaningful
        adapter = new NOPLoggerFA();
      }
    }
    
    static public ULogger getLogger(String name) {
      return adapter.getLogger(name);
    }
    
    static public ULogger getLogger(String domainName, String subDomainName) {
      return adapter.getLogger(domainName, subDomainName);
    }
    
    static public ULogger getLogger(Class clazz) {
      return adapter.getLogger(clazz.getName());
    }
    static public ULogger getLogger(Class clazz, String subDomainName) {
      return adapter.getLogger(clazz.getName(), subDomainName);
    }
    
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/ugli/ULogger.java
  
  Index: ULogger.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.ugli;
  
  
  /**
   * 
   * The main user inteface to logging. It is expected that logging takes places
   * through concerete implemetations of the ULogger interface.
   *  
   * 
   * @author Ceki G&uuml;lc&uuml;
   */
  public interface ULogger {
  
  
    /**
     * Is the logger instance enabled for the DEBUG level?
     * @return 
     */
    public boolean isDebugEnabled();
  //
    
    /**
     * Log a message object with the DEBUG level. 
     * @param msg - the message object to be logged
     */
    public void debug(Object msg);
    
    
    /**
     * Log a parameterized message object at the DEBUG level. 
     * 
     * <p>This form is useful in avoiding the superflous object creation
     * problem when invoking this method while it is disabled.
     * </p>
     * @param parameterizedMsg - the parameterized message object
     * @param param1 - the parameter 
     */
    public void debug(Object parameterizedMsg, Object param1);
    
    /**
     * Log a parameterized message object at the DEBUG level. 
     * 
     * <p>This form is useful in avoiding the superflous object creation
     * problem when invoking this method while it is disabled.
     * </p>
     * @param parameterizedMsg - the parameterized message object
     * @param param1 - the first parameter 
     * @param param2 - the second parameter 
     */
    public void debug(Object parameterizedMsg, Object param1, Object param2);
    public void debug(Object msg, Throwable t);
  
  
    public boolean isInfoEnabled();
    public void info(Object msg);
    public void info(Object parameterizedMsg, Object param1);
    public void info(Object parameterizedMsg, Object param1, Object param2);
    public void info(Object msg, Throwable t);
  
  
    public boolean isWarnEnabled();
    public void warn(Object msg);
    public void warn(Object parameterizedMsg, Object param1);
    public void warn(Object parameterizedMsg, Object param1, Object param2);
    public void warn(Object msg, Throwable t);
  
  
    public boolean isErrorEnabled();
    public void error(Object msg);
    public void error(Object parameterizedMsg, Object param1);
    public void error(Object parameterizedMsg, Object param1, Object param2);
    public void error(Object msg, Throwable t);
  
  }
  
  
  
  1.1                  
logging-log4j/src/java/org/apache/ugli/LoggerFactoryAdapter.java
  
  Index: LoggerFactoryAdapter.java
  ===================================================================
  /*
   * Created on Dec 30, 2004
   *
   * To change the template for this generated file go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  package org.apache.ugli;
  
  
  
  /**
   * LoggerFactoryAdapter interface is used internally by [EMAIL PROTECTED] 
LoggerFactory}.
   * 
   * <p>Only developers wishing to write new UGLI adapters need to worry about
   * this interface.
   * 
   * @author Ceki G&uuml;lc&uuml;
   *
   */
  public interface LoggerFactoryAdapter {
    
    public ULogger getLogger(String name);
    public ULogger getLogger(String domainName, String subDomainName);  
  }
  
  
  
  1.1                  
logging-log4j/src/java/org/apache/ugli/impl/JDK14LoggerFA.java
  
  Index: JDK14LoggerFA.java
  ===================================================================
  /*
   * Created on Dec 30, 2004
   *
   * To change the template for this generated file go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  package org.apache.ugli.impl;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.WeakHashMap;
  import java.util.logging.Logger;
  
  import org.apache.ugli.LoggerFactoryAdapter;
  import org.apache.ugli.ULogger;
  
  
  /**
   * @author ceki
   *
   * To change the template for this generated type comment go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  public class JDK14LoggerFA implements LoggerFactoryAdapter {
  
    Map map;
    
    JDK14LoggerFA() {
      map = new HashMap();
    }
  
    /* (non-Javadoc)
     * @see org.apache.ugli.LoggerFactoryAdapter#getLogger(java.lang.String)
     */
    public ULogger getLogger(String name) {
      ULogger ulogger = (ULogger) map.get(name);
      if(ulogger == null) {
        Logger logger = Logger.getLogger(name);
        ulogger = new JDK14Logger(logger);
        map.put(name, ulogger);
      }
      return ulogger;
    }
  
    /* (non-Javadoc)
     * @see org.apache.ugli.LoggerFactoryAdapter#getLogger(java.lang.String, 
java.lang.String)
     */
    public ULogger getLogger(String domainName, String subDomainName) {
      return getLogger(domainName);
    }
    
    
  }
  
  
  
  1.1                  
logging-log4j/src/java/org/apache/ugli/impl/JDK14Logger.java
  
  Index: JDK14Logger.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  /*
   * Created on Dec 30, 2004
   *
   * To change the template for this generated file go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  package org.apache.ugli.impl;
  
  import org.apache.log4j.helpers.MessageFormatter;
  
  import org.apache.ugli.ULogger;
  
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  
  /**
   * @author ceki
   *
   * To change the template for this generated type comment go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  public class JDK14Logger implements ULogger {
    final Logger logger;
  
    JDK14Logger(Logger logger) {
      this.logger = logger;
    }
  
    /**
     * Is the logger instance enabled for the DEBUG level?
     * @return
     */
    public boolean isDebugEnabled() {
      return logger.isLoggable(Level.FINE);
    }
  
    //
  
    /**
     * Log a message object with the DEBUG level.
     * @param msg - the message object to be logged
     */
    public void debug(Object msg) {
      logger.fine(String.valueOf(msg));
    }
  
    /**
     * Log a parameterized message object at the DEBUG level.
     *
     * <p>This form is useful in avoiding the superflous object creation
     * problem when invoking this method while it is disabled.
     * </p>
     * @param parameterizedMsg - the parameterized message object
     * @param param1 - the parameter
     */
    public void debug(Object parameterizedMsg, Object param1) {
      if (logger.isLoggable(Level.FINE)) {
        if (parameterizedMsg instanceof String) {
          String msgStr = (String) parameterizedMsg;
          msgStr = MessageFormatter.format(msgStr, param1);
          logger.fine(msgStr);
        } else {
          // To be failsafe, we handle the case where 'messagePattern' is not
          // a String. Unless the user makes a mistake, this should not happen.
          logger.fine(parameterizedMsg.toString());
        }
      }
    }
  
    /**
     * Log a parameterized message object at the DEBUG level.
     *
     * <p>This form is useful in avoiding the superflous object creation
     * problem when invoking this method while it is disabled.
     * </p>
     * @param parameterizedMsg - the parameterized message object
     * @param param1 - the first parameter
     * @param param2 - the second parameter
     */
    public void debug(Object parameterizedMsg, Object param1, Object param2) {
      if (logger.isLoggable(Level.FINE)) {
        if (parameterizedMsg instanceof String) {
          String msgStr = (String) parameterizedMsg;
          msgStr = MessageFormatter.format(msgStr, param1, param2);
          logger.fine(msgStr);
        } else {
          // To be failsafe, we handle the case where 'messagePattern' is not
          // a String. Unless the user makes a mistake, this should not happen.
          logger.fine(parameterizedMsg.toString());
        }
      }
    }
  
    public void debug(Object msg, Throwable t) {
      logger.log(Level.FINE, msg.toString(), t);
    }
  
    public boolean isInfoEnabled() {
      return logger.isLoggable(Level.INFO);
    }
  
    public void info(Object msg) {
      logger.info(msg.toString());
    }
  
    public void info(Object parameterizedMsg, Object param1) {
      if (logger.isLoggable(Level.INFO)) {
        if (parameterizedMsg instanceof String) {
          String msgStr = (String) parameterizedMsg;
          msgStr = MessageFormatter.format(msgStr, param1);
          logger.info(msgStr);
        } else {
          // To be failsafe, we handle the case where 'messagePattern' is not
          // a String. Unless the user makes a mistake, this should not happen.
          logger.info(parameterizedMsg.toString());
        }
      }
    }
  
    public void info(Object parameterizedMsg, Object param1, Object param2) {
      if (logger.isLoggable(Level.INFO)) {
        if (parameterizedMsg instanceof String) {
          String msgStr = (String) parameterizedMsg;
          msgStr = MessageFormatter.format(msgStr, param1, param2);
          logger.info(msgStr);
        } else {
          // To be failsafe, we handle the case where 'messagePattern' is not
          // a String. Unless the user makes a mistake, this should not happen.
          logger.info(parameterizedMsg.toString());
        }
      }
    }
  
    public void info(Object msg, Throwable t) {
      logger.log(Level.INFO, msg.toString(), t);
    }
  
    public boolean isWarnEnabled() {
      return logger.isLoggable(Level.WARNING);
    }
  
    public void warn(Object msg) {
      logger.warning(msg.toString());
    }
  
    public void warn(Object parameterizedMsg, Object param1) {
      if (logger.isLoggable(Level.WARNING)) {
        if (parameterizedMsg instanceof String) {
          String msgStr = (String) parameterizedMsg;
          msgStr = MessageFormatter.format(msgStr, param1);
          logger.warning(msgStr);
        } else {
          // To be failsafe, we handle the case where 'messagePattern' is not
          // a String. Unless the user makes a mistake, this should not happen.
          logger.warning(parameterizedMsg.toString());
        }
      }
    }
  
    public void warn(Object parameterizedMsg, Object param1, Object param2) {
      if (logger.isLoggable(Level.WARNING)) {
        if (parameterizedMsg instanceof String) {
          String msgStr = (String) parameterizedMsg;
          msgStr = MessageFormatter.format(msgStr, param1, param2);
          logger.warning(msgStr);
        } else {
          // To be failsafe, we handle the case where 'messagePattern' is not
          // a String. Unless the user makes a mistake, this should not happen.
          logger.warning(parameterizedMsg.toString());
        }
      }
    }
  
    public void warn(Object msg, Throwable t) {
      logger.log(Level.WARNING, msg.toString(), t);
    }
  
    public boolean isErrorEnabled() {
      return logger.isLoggable(Level.SEVERE);
    }
  
    public void error(Object msg) {
      logger.severe(msg.toString());
    }
  
    public void error(Object parameterizedMsg, Object param1) {
      if (logger.isLoggable(Level.WARNING)) {
        if (parameterizedMsg instanceof String) {
          String msgStr = (String) parameterizedMsg;
          msgStr = MessageFormatter.format(msgStr, param1);
          logger.severe(msgStr);
        } else {
          // To be failsafe, we handle the case where 'messagePattern' is not
          // a String. Unless the user makes a mistake, this should not happen.
          logger.severe(parameterizedMsg.toString());
        }
      }
    }
    
    public void error(Object parameterizedMsg, Object param1, Object param2) {
      if (logger.isLoggable(Level.WARNING)) {
        if (parameterizedMsg instanceof String) {
          String msgStr = (String) parameterizedMsg;
          msgStr = MessageFormatter.format(msgStr, param1, param2);
          logger.severe(msgStr);
        } else {
          // To be failsafe, we handle the case where 'messagePattern' is not
          // a String. Unless the user makes a mistake, this should not happen.
          logger.severe(parameterizedMsg.toString());
        }
      }
    }
    public void error(Object msg, Throwable t) {
      logger.log(Level.SEVERE, msg.toString(), t);
    }
  }
  
  
  
  1.1                  
logging-log4j/src/java/org/apache/ugli/impl/NOPLoggerFA.java
  
  Index: NOPLoggerFA.java
  ===================================================================
  /*
   * Created on Dec 30, 2004
   *
   * To change the template for this generated file go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  package org.apache.ugli.impl;
  
  import org.apache.ugli.LoggerFactoryAdapter;
  import org.apache.ugli.ULogger;
  
  
  /**
   * NOPLoggerFA is am implementation of [EMAIL PROTECTED] LoggerFactoryAdapter}
   * which always returns the unique instance of NOPLogger.
   * 
   * @author Ceki Gulcu
   */
  public class NOPLoggerFA implements LoggerFactoryAdapter {
    
    public ULogger getLogger(String name) {
      return NOPLogger.NOP_LOGGER;
    }
    public ULogger getLogger(String domainName, String subDomainName) {
      return NOPLogger.NOP_LOGGER;  
    }  
  }
  
  
  
  1.1                  
logging-log4j/src/java/org/apache/ugli/impl/NOPLogger.java
  
  Index: NOPLogger.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ugli.impl;
  
  import org.apache.ugli.ULogger;
  
  
  /**
   * A no operation (NOP) implementation of [EMAIL PROTECTED] ULogger}.
   * 
   * @author Ceki G&uuml;lc&uuml;
   */
  public class NOPLogger implements ULogger {
  
    /**
     * The unique instance of NOPLogger.
     */
    public final static NOPLogger NOP_LOGGER = new NOPLogger();
    
    /**
     * There is no point in people creating multiple instances of NullLogger. 
     * Hence, the private access modifier. 
     */
    private NOPLogger() {
    }
    
    /* Always returns false.
     * 
     * @see org.apache.ugli.Logger#isDebugEnabled()
     */
    public boolean isDebugEnabled() {
      return false;
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#debug(java.lang.Object)
     */
    public void debug(Object msg) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#debug(java.lang.Object, java.lang.Object)
     */
    public void debug(Object parameterizedMsg, Object param1) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#debug(java.lang.Object, java.lang.Object, 
java.lang.Object)
     */
    public void debug(Object parameterizedMsg, Object param1, Object param2) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#debug(java.lang.Object, java.lang.Throwable)
     */
    public void debug(Object msg, Throwable t) {
      // NOP
    }
  
    /* Always returns false.
     * @see org.apache.ugli.Logger#isInfoEnabled()
     */
    public boolean isInfoEnabled() {
      // NOP
      return false;
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#info(java.lang.Object)
     */
    public void info(Object msg) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#info(java.lang.Object, java.lang.Object)
     */
    public void info(Object parameterizedMsg, Object param1) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#info(java.lang.Object, java.lang.Object, 
java.lang.Object)
     */
    public void info(Object parameterizedMsg, Object param1, Object param2) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#info(java.lang.Object, java.lang.Throwable)
     */
    public void info(Object msg, Throwable t) {
      // NOP
    }
  
    /* Always returns false.
     * @see org.apache.ugli.Logger#isWarnEnabled()
     */
    public boolean isWarnEnabled() {
      return false;
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#warn(java.lang.Object)
     */
    public void warn(Object msg) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#warn(java.lang.Object, java.lang.Object)
     */
    public void warn(Object parameterizedMsg, Object param1) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#warn(java.lang.Object, java.lang.Object, 
java.lang.Object)
     */
    public void warn(Object parameterizedMsg, Object param1, Object param2) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#warn(java.lang.Object, java.lang.Throwable)
     */
    public void warn(Object msg, Throwable t) {
      // NOP
    }
  
    /* Always returns false.
     * @see org.apache.ugli.Logger#isErrorEnabled()
     */
    public boolean isErrorEnabled() {
      return false;
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#error(java.lang.Object)
     */
    public void error(Object msg) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#error(java.lang.Object, java.lang.Object)
     */
    public void error(Object parameterizedMsg, Object param1) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#error(java.lang.Object, java.lang.Object, 
java.lang.Object)
     */
    public void error(Object parameterizedMsg, Object param1, Object param2) {
      // NOP
    }
  
    /* A NOP implementation.
     * @see org.apache.ugli.Logger#error(java.lang.Object, java.lang.Throwable)
     */
    public void error(Object msg, Throwable t) {
      // NOP
    }
  
  }
  
  
  

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

Reply via email to