/***************************************
 *                                     *
 *  JBoss: The OpenSource J2EE WebOS   *
 *                                     *
 *  Distributable under LGPL license.  *
 *  See terms of license at gnu.org.   *
 *                                     *
 ***************************************/

package org.jboss.util;

import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.logger.Log4JLogger;
import org.jacorb.util.LoggerFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * JacORB logger factory that creates named Avalon loggers with log4j
 * as the underlying log mechanism.
 * <p>
 * JacORB log priorities for new loggers are set implicitly to either 
 * the value of this factory's <code>defaultPriority</code> field, or 
 * via JacORB configuration properties that have the same name as the 
 * requested logger, plus a suffix of <code>.log.verbosity</code>.
 *
 *  @author <a href="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
 *  @version $Revision: 1.1.2.1 $
 */
public class Log4jLoggerFactory
   implements LoggerFactory
{
   /** logging back-end mechanism used by all Log4jLoggerFactory instances */
   private final static String name = "log4j";
   
   /** default priority for loggers created with this factory */
   private int defaultPriority = 0;
   
   /** cache of created loggers */
   private final Map namedLoggers = new HashMap();
   
    /**
     * Constructs a <code>Log4jLoggerFactory<code> with 
     * <code>defaultPriority</code> specified by the property
     * <code>jacorb.log.default.verbosity</code>.
     */
   public Log4jLoggerFactory()
   {
      String defaultPriorityString = 
         org.jacorb.util.Environment.getProperty("jacorb.log.default.verbosity");
      if (defaultPriorityString != null)
      {
         try
         {
            defaultPriority = Integer.parseInt(defaultPriorityString);
         }
         catch (NumberFormatException nfe)
         {
            defaultPriority = -1;
         }
      }
   }

    /**
     * Gets a log4j logger by name.
     *
     * @param name the name of the logger
     * @return an <code>org.apache.log4j.Logger</code> instance
     */
   private static org.apache.log4j.Logger getLog4jLogger(String name)
   {
      org.jboss.logging.Logger l = org.jboss.logging.Logger.getLogger(name);
      org.jboss.logging.LoggerPlugin lp = l.getLoggerPlugin();
      if (lp instanceof org.jboss.logging.Log4jLoggerPlugin)
         return ((org.jboss.logging.Log4jLoggerPlugin)lp).getLogger();
      else
         return null;
   }

    /**
     * Returns the priority level for a given integer.
     *
     * @param priority an <code>int</code> value
     * @return an <code>org.apache.log4j.Level</code> value
     */
   private static org.apache.log4j.Level intToLevel(int priority)
   {
      switch (priority)
      {
      case 4:
         return org.apache.log4j.Level.DEBUG;
      case 3:
         return org.apache.log4j.Level.INFO;
      case 2:
         return org.apache.log4j.Level.WARN;
      case 1:
         return org.apache.log4j.Level.ERROR;
      case 0:
      default:
         return org.apache.log4j.Level.FATAL;
      }
   }

   // Implementation of org.jacorb.util.LoggerFactory ---------------

   /**
    * Gets the name of the logging back-end mechanism.
    *
    * @return the string <code>"log4j"</code> 
    */
   public final String getLoggingBackendName()
   {
      return name;
   }
   
   /**
    * Gets an Avalon logger by name.
    *
    * @param name the name of the logger
    * @return an <code>org.apache.avalon.framework.logger.Logger</code> 
    *         instance
    */
   public Logger getNamedLogger(String name)
   {
      Object o = namedLoggers.get(name);

      if (o != null)
         return (Logger)o;

      org.apache.log4j.Logger log4jLogger = getLog4jLogger(name);

      String priorityString = 
          org.jacorb.util.Environment.getProperty( name + ".log.verbosity");

      int priority = defaultPriority;
      
      if (priorityString != null)
          priority = Integer.parseInt(priorityString);
      log4jLogger.setLevel(intToLevel(priority));

      Logger logger = new Log4JLogger(log4jLogger);

      namedLoggers.put(name, logger);
      return logger;
   }
   
   /**
    * Gets an Avalon root logger by name.
    *
    * @param name the name of the logger
    * @return an <code>org.apache.avalon.framework.logger.Logger</code> 
    *         instance
    */
   public Logger getNamedRootLogger(String name)
   {
      return getNamedLogger(name);
   }
   
   /**
    * Creates a named Avalon logger with given <code>logFileName</code>
    * and <code>maxLogSize</code> parameters. This is a dummy implementation
    * that always return null.
    *
    * @param name the name of the logger
    * @param logFileName the name of the file to log to
    * @param maxLogSize maximum size of the log file. 
    *
    * @return the new logger (null in this implementation).
    */
   
   public Logger getNamedLogger(String name,
                                String logFileName, long maxLogSize)
      throws java.io.IOException
   {
      return null;
   }

}
