001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util.log;
028:
029: import java.util.Map;
030: import java.util.Properties;
031: import org.cougaar.bootstrap.SystemProperties;
032:
033: /**
034: * Factory to create Logger and LoggerController instances.
035: * <p>
036: * Typically the "requestor" classname is used to identify
037: * loggers. A special "name" is "root", which is used
038: * to specify the root (no-parent) logger.
039: *
040: * @property org.cougaar.util.log.LoggerFactory Specifies the LoggerFactory implementation class to instantiate
041: * for use as the singleton logger. The default value is "org.cougaar.util.log.log4j.Log4jLoggerFactory". If
042: * the specified factory cannot be found and initialized, an error message will be printed, and it will use
043: * an instance of {@link NullLoggerFactory} instead.
044: * @property org.cougaar.util.log.config Specifies a URL where a LoggerFactory configuration
045: * file may be found. The interpretation of this file (including ignoring it) is up to the implementation
046: * class, but implementations are encouraged to use it.
047: *
048: * @see org.cougaar.util.log.log4j.Log4jLoggerFactory
049: * @see NullLoggerFactory
050: */
051: public abstract class LoggerFactory {
052: public static final String LF_PREFIX = "org.cougaar.util.log";
053: public static final String LF_PROP = LF_PREFIX + ".LoggerFactory";
054: public static final String LF_CONFIG_PROP = LF_PREFIX + ".config";
055: public static final String LF_DEFAULT_CLASS = "org.cougaar.util.log.log4j.Log4jLoggerFactory";
056:
057: private static LoggerFactory singleton = null;
058:
059: public synchronized static final LoggerFactory getInstance() {
060: if (singleton == null) {
061: singleton = makeInstance();
062: }
063: return singleton;
064: }
065:
066: private static final LoggerFactory makeInstance() {
067: String lfname = SystemProperties.getProperty(LF_PROP,
068: LF_DEFAULT_CLASS);
069: try {
070: Class clazz = Class.forName(lfname);
071: return (LoggerFactory) clazz.newInstance();
072: } catch (Exception e) {
073: System.err
074: .println("Could not enable LoggerFactory \""
075: + lfname
076: + "\": will use NullLoggerFactory instead");
077: e.printStackTrace();
078: return new NullLoggerFactory();
079: }
080: }
081:
082: /** Implementations may override to provide
083: * additional configuration information to the underlying logging facility.
084: * The default implementation does nothing.
085: **/
086: public void configure(Properties props) {
087: getInstance().createLogger(LoggerFactory.class).error(
088: "Clients may not configure Loggers", new Throwable());
089: }
090:
091: /** Implementations may override to provide
092: * additional configuration information to the underlying logging facility.
093: * The default implementation does nothing.
094: **/
095: public void configure(Map m) {
096: getInstance().createLogger(LoggerFactory.class).error(
097: "Clients may not configure Loggers", new Throwable());
098: }
099:
100: /** Create a logger as named by the parameter.
101: * If requestor is a Class, will use the class name. If it is a String, it will
102: * use it directly. Anything else will use the name of the class that the
103: * object is an instance of.
104: * <p>
105: * The default implementation now just invokes Logging.getLogger(Object) which will
106: * invoke newLogger IFF needed.
107: **/
108: public Logger createLogger(Object requestor) {
109: return Logging.getLogger(requestor);
110: }
111:
112: /** called by the default implementation of createLogger to do the dirty work.
113: * merely invokes the protected method instantiateLogger.
114: **/
115: final Logger newLogger(Object requestor) {
116: return instantiateLogger(requestor);
117: }
118:
119: protected abstract Logger instantiateLogger(Object requestor);
120:
121: public abstract LoggerController createLoggerController(
122: String requestor);
123: }
|