001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------
028: * DefaultLog.java
029: * ---------------
030: * (C) Copyright 2004, by Object Refinery Limited.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: DefaultLog.java,v 1.9 2006/02/19 21:10:48 taqua Exp $
036: *
037: * Changes
038: * -------
039: * 07-Jun-2004 : Added JCommon header (DG);
040: *
041: */
042:
043: package org.jfree.base.log;
044:
045: import org.jfree.util.Log;
046: import org.jfree.util.LogTarget;
047: import org.jfree.util.PrintStreamLogTarget;
048:
049: /**
050: * A default log implementation. The Log class defines how to create Logger-contexts
051: * and how to forward messages to the logtargets.
052: *
053: * @author Thomas Morgner
054: */
055: public class DefaultLog extends Log {
056:
057: /** The default log target. */
058: private static final PrintStreamLogTarget DEFAULT_LOG_TARGET = new PrintStreamLogTarget();
059:
060: /** The default log instance. */
061: private static final DefaultLog defaultLogInstance;
062:
063: /**
064: * Creates a new log.
065: */
066: protected DefaultLog() {
067: // nothing required
068: }
069:
070: static {
071: defaultLogInstance = new DefaultLog();
072: defaultLogInstance.addTarget(DEFAULT_LOG_TARGET);
073: try {
074: // check the system property. This is the developers backdoor to activate
075: // debug output as soon as possible.
076: final String property = System.getProperty(
077: "org.jfree.DebugDefault", "false");
078: if (Boolean.valueOf(property).booleanValue()) {
079: defaultLogInstance.setDebuglevel(LogTarget.DEBUG);
080: } else {
081: defaultLogInstance.setDebuglevel(LogTarget.WARN);
082: }
083: } catch (SecurityException se) {
084: defaultLogInstance.setDebuglevel(LogTarget.WARN);
085: }
086: }
087:
088: /**
089: * Initializes the log system after the log module was loaded and a log target
090: * was defined. This is the second step of the log initialisation.
091: */
092: public void init() {
093: removeTarget(DEFAULT_LOG_TARGET);
094: final String logLevel = LogConfiguration.getLogLevel();
095: if (logLevel.equalsIgnoreCase("error")) {
096: setDebuglevel(LogTarget.ERROR);
097: } else if (logLevel.equalsIgnoreCase("warn")) {
098: setDebuglevel(LogTarget.WARN);
099: } else if (logLevel.equalsIgnoreCase("info")) {
100: setDebuglevel(LogTarget.INFO);
101: } else if (logLevel.equalsIgnoreCase("debug")) {
102: setDebuglevel(LogTarget.DEBUG);
103: }
104: }
105:
106: /**
107: * Adds a log target to this facility. Log targets get informed, via the
108: * LogTarget interface, whenever a message is logged with this class.
109: *
110: * @param target the target.
111: */
112: public synchronized void addTarget(final LogTarget target) {
113: super .addTarget(target);
114: // as soon as there is a real log target added, we do no longer need
115: // the default logging. This was only installed to be able to send messages
116: // if the deepest basic logging failed.
117: if (target != DEFAULT_LOG_TARGET) {
118: removeTarget(DEFAULT_LOG_TARGET);
119: }
120: }
121:
122: /**
123: * Returns the default log.
124: *
125: * @return The default log.
126: */
127: public static DefaultLog getDefaultLog() {
128: return defaultLogInstance;
129: }
130:
131: /**
132: * Makes this implementation the default instance.
133: */
134: public static void installDefaultLog() {
135: Log.defineLog(defaultLogInstance);
136: }
137: }
|