KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > base > log > DefaultLog


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * DefaultLog.java
29  * ---------------
30  * (C) Copyright 2004, by Object Refinery Limited.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: DefaultLog.java,v 1.9 2006/02/19 21:10:48 taqua Exp $
36  *
37  * Changes
38  * -------
39  * 07-Jun-2004 : Added JCommon header (DG);
40  *
41  */

42
43 package org.jfree.base.log;
44
45 import org.jfree.util.Log;
46 import org.jfree.util.LogTarget;
47 import org.jfree.util.PrintStreamLogTarget;
48
49 /**
50  * A default log implementation. The Log class defines how to create Logger-contexts
51  * and how to forward messages to the logtargets.
52  *
53  * @author Thomas Morgner
54  */

55 public class DefaultLog extends Log {
56
57     /** The default log target. */
58     private static final PrintStreamLogTarget DEFAULT_LOG_TARGET =
59           new PrintStreamLogTarget();
60
61     /** The default log instance. */
62     private static final DefaultLog defaultLogInstance;
63
64     /**
65      * Creates a new log.
66      */

67     protected DefaultLog () {
68         // nothing required
69
}
70
71     static {
72         defaultLogInstance = new DefaultLog();
73         defaultLogInstance.addTarget(DEFAULT_LOG_TARGET);
74         try {
75             // check the system property. This is the developers backdoor to activate
76
// debug output as soon as possible.
77
final String JavaDoc property = System.getProperty("org.jfree.DebugDefault", "false");
78           if (Boolean.valueOf(property).booleanValue()) {
79               defaultLogInstance.setDebuglevel(LogTarget.DEBUG);
80           }
81           else {
82               defaultLogInstance.setDebuglevel(LogTarget.WARN);
83           }
84         }
85         catch (SecurityException JavaDoc se) {
86             defaultLogInstance.setDebuglevel(LogTarget.WARN);
87         }
88     }
89
90     /**
91      * Initializes the log system after the log module was loaded and a log target
92      * was defined. This is the second step of the log initialisation.
93      */

94     public void init() {
95         removeTarget(DEFAULT_LOG_TARGET);
96         final String JavaDoc logLevel = LogConfiguration.getLogLevel();
97         if (logLevel.equalsIgnoreCase("error")) {
98             setDebuglevel(LogTarget.ERROR);
99         }
100         else if (logLevel.equalsIgnoreCase("warn")) {
101             setDebuglevel(LogTarget.WARN);
102         }
103         else if (logLevel.equalsIgnoreCase("info")) {
104             setDebuglevel(LogTarget.INFO);
105         }
106         else if (logLevel.equalsIgnoreCase("debug")) {
107             setDebuglevel(LogTarget.DEBUG);
108         }
109     }
110
111     /**
112      * Adds a log target to this facility. Log targets get informed, via the
113      * LogTarget interface, whenever a message is logged with this class.
114      *
115      * @param target the target.
116      */

117     public synchronized void addTarget(final LogTarget target)
118     {
119       super.addTarget(target);
120       // as soon as there is a real log target added, we do no longer need
121
// the default logging. This was only installed to be able to send messages
122
// if the deepest basic logging failed.
123
if (target != DEFAULT_LOG_TARGET) {
124           removeTarget(DEFAULT_LOG_TARGET);
125       }
126     }
127
128   /**
129      * Returns the default log.
130      *
131      * @return The default log.
132      */

133     public static DefaultLog getDefaultLog() {
134         return defaultLogInstance;
135     }
136
137     /**
138      * Makes this implementation the default instance.
139      */

140     public static void installDefaultLog () {
141       Log.defineLog(defaultLogInstance);
142     }
143 }
144
Popular Tags