Package org.objectweb.util.monolog.wrapper.javaLog

Source Code of org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory

/**
* Copyright (C) 2001-2003 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package org.objectweb.util.monolog.wrapper.javaLog;

import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.wrapper.common.AbstractFactory;
import org.objectweb.util.monolog.wrapper.javaLog.Logger;
import org.objectweb.util.monolog.Monolog;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.LogManager;

/**
* is the LoggerFactory for the wrapper to java.util.logging
*
* @author S.Chassande-Barrioz
*/
public class LoggerFactory extends AbstractFactory {

    /**
     * the LogManager of java.util.logging
     */
  protected static LogManager manager = null;

  /**
   * the root logger
   */
  protected static Logger rootLogger = null;


    /**
     * Keep a reference to loggers.
     * @see java.util.logging.LogManager addLogger javadoc
     */
    private ArrayList loggers = new ArrayList();

    /**
     * This static code initialize the value of the variable defined into
     * BasicLevel.
     */
    static {
        BasicLevel.INHERIT = -1;
        debug("INHERIT= " + BasicLevel.INHERIT);
        BasicLevel.DEBUG = java.util.logging.Level.FINEST.intValue();
        debug("DEBUG= " + BasicLevel.DEBUG);
        BasicLevel.INFO = java.util.logging.Level.INFO.intValue();
        debug("INFO= " + BasicLevel.INFO);
        BasicLevel.WARN = java.util.logging.Level.WARNING.intValue();
        debug("WARN= " + BasicLevel.WARN);
        BasicLevel.ERROR = java.util.logging.Level.SEVERE.intValue();
        debug("ERROR= " + BasicLevel.ERROR);
        BasicLevel.FATAL = java.util.logging.Level.SEVERE.intValue();
        debug("FATAL= " + BasicLevel.FATAL);

        BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT", BasicLevel.INHERIT);
        BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG", BasicLevel.DEBUG);
        BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO);
        BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN);
        BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR", BasicLevel.ERROR);
        BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL", BasicLevel.FATAL);

        manager = LogManager.getLogManager();
        if (classLoaderIsoltion) {
            int i = 0;
            while (rootLogger == null) {
                rootLoggerName = "root" + i;
                synchronized (manager) {
                    // the manager object is shared between the multiple instances of this class
                    if (manager.getLogger(rootLoggerName) == null) {
                        rootLogger = new Logger(rootLoggerName, null);
                        manager.addLogger(rootLogger);
                    } else {
                        i ++;
                    }
                }
            }
            rootLogger.setUseParentHandlers(false);
            Monolog.debug("Instanciate the root logger " + rootLoggerName);
            rootLoggerPrefix = rootLoggerName + '.';
        } else {
            rootLogger = new Logger(manager.getLogger(""));
        }
    }

    /**
     * This constant is used to initialize the factory with the configure method
     */
    public final static String JAVALOG_CONFIGURATION = "javaLogConfiguration";

    /**
     * This constant means that this java log system must be initialize with
     * the default configuration
     */
    public final static String DEFAULT = "default";

    /**
     * This constant means that this java log system must be initialize with
     * a property file
     */
    public final static String PROPERTY = "property";

    /**
     * This constant means that this java log system must be initialize with
     * a xml file
     */
    public final static String CLASS = "class";

    /**
     * This constant is the properties file name with wich the java log system
     * must be initialized.
     */
    public final static String JAVALOG_CONFIGURATION_FILE
            = "javaLogConfigurationFile";

    /**
     * This constant is the properties class name with wich the java log system
     * must be initialized.
     */
    public final static String JAVALOG_CONFIGURATION_CLASS
            = "javaLogConfigurationClass";

    public LoggerFactory() {
        super();
    }

  public String getWrapperName() {
    return "javaLog";
  }

  protected String[][] getDefaultHandlerType2className() {
    return new String[][] {
      {handlerTypes[0], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"},
      {handlerTypes[1], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"},
      {handlerTypes[2], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"},
      {handlerTypes[3], "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler"},
      {handlerTypes[4], "org.objectweb.util.monolog.wrapper.javaLog.JMXGenericHandler"}
    };
  }

    /**
     * This method allocates org.objectweb.util.monolog.wrapper.javaLog.Logger
     * objects whic are also java.util.logging.Logger and
     * org.objectweb.util.monolog.api.Logger.
     */
    protected synchronized Logger getMonoLogger(String name, String resName) {
        if (name == null)
            throw new IllegalArgumentException(
                    "The specified Logger name is null");
        if (name.equals("root") || name.length()==0) {
            return rootLogger;
        }
        // isolates the logger hierarchy
        name = monoLoggerName(name);
        // Search if the logger already exist.
        Object o = manager.getLogger(name);
        if (o == null) {
            // It doesn't exist => creates and adds it
            Logger result = new Logger(name, resName);
            manager.addLogger(result);
            Monolog.debug("Instanciate the logger " + name);

            // In the javadoc of the LogManager, it is said that the caller
            // need to keep a reference to this logger to avoid GC.
            // Because LogManager could use some weak reference on it
            loggers.add(result);
            return result;
        } else if (!o.getClass().equals(Logger.class)) {
            throw new RuntimeException("Bad logger class (logger name: '"
                    + name +"'), expected: " + Logger.class
                    + ", found: " + o.getClass());
        } else {
            return (Logger) o;
        }
    }


    // IMPLEMENTATION OF THE Configurable INTERFACE //
    //----------------------------------------------//

    /**
     * This method permits to configure the factory.
     * The properties parameter must contains the JAVALOG_CONFIGURATION property.
     * Its value can be DEFAULT, PROPERTY or XML.
     * In the PROPERTY case of the properties parameter must also contain the
     * JAVALOG_CONFIGURATION_FILE property which the value is the configuration
     *  file name.
     * In the CLASS case of the properties parameter must also contain the
     * JAVALOG_CONFIGURATION_CLASS property which the value is the configuration
     *  class name which will initialized the java log system..
     *
     */
    public void configure(Properties prop) throws Exception {
        if (prop == null)
            return;
        String confMode = prop.getProperty(JAVALOG_CONFIGURATION, null);
        if (confMode == null)
            return;

        String param = null;
        if (confMode.equals(PROPERTY)) {
            param = prop.getProperty(JAVALOG_CONFIGURATION_FILE, null);
            if (param != null)
                System.setProperty("java.util.logging.config.file", param);
            manager.readConfiguration();
        } else if (confMode.equals(CLASS)) {
            param = prop.getProperty(JAVALOG_CONFIGURATION_CLASS, null);
            if (param != null)
                System.setProperty("java.util.logging.config.class", param);
            manager.readConfiguration();
        }
    }


    // IMPLEMENTATION OF LoggerFactory INTERFACE //
    //-------------------------------------------//

    public org.objectweb.util.monolog.api.Logger getLogger(String key) {
        return getMonoLogger(key, resourceBundleName);
    }

    public org.objectweb.util.monolog.api.Logger getLogger(String key, String rbn) {
        return getMonoLogger(key, rbn);
    }

    /**
     * It retrieves a list of all loggers.
     */
    public org.objectweb.util.monolog.api.Logger[] getLoggers() {
        ArrayList res = new ArrayList();
        for (Enumeration e = manager.getLoggerNames(); e.hasMoreElements();) {
             Object o = manager.getLogger((String) e.nextElement());
             if (o instanceof Logger) {
                 res.add(o);
             }
        }
         return (Logger[]) res.toArray(new Logger[0]);
     }
}
TOP

Related Classes of org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.