001: /*--
002:
003: Copyright (C) 2000-2003 Anthony Eden.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The name "EdenLib" must not be used to endorse or promote products
019: derived from this software without prior written permission. For
020: written permission, please contact me@anthonyeden.com.
021:
022: 4. Products derived from this software may not be called "EdenLib", nor
023: may "EdenLib" appear in their name, without prior written permission
024: from Anthony Eden (me@anthonyeden.com).
025:
026: In addition, I request (but do not require) that you include in the
027: end-user documentation provided with the redistribution and/or in the
028: software itself an acknowledgement equivalent to the following:
029: "This product includes software developed by
030: Anthony Eden (http://www.anthonyeden.com/)."
031:
032: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
034: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
035: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
036: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
037: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
038: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
039: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
040: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
041: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
042: POSSIBILITY OF SUCH DAMAGE.
043:
044: For more information on EdenLib, please see <http://edenlib.sf.net/>.
045:
046: */
047:
048: package com.anthonyeden.lib.log;
049:
050: import java.io.File;
051: import java.lang.reflect.Method;
052: import java.util.HashMap;
053:
054: import com.anthonyeden.lib.util.ClassUtilities;
055: import com.anthonyeden.lib.util.MethodUtilities;
056:
057: /** Core class for obtaining Loggers.
058:
059: <p>If you would like to implement a custom logger you must follow these
060: rules:
061:
062: <ul>
063: <li>Must implement LoggerInternal</li>
064: <li>Must include two static configuration methods:
065: <ul>
066: <li><code>public static void configure()</code></li>
067: <li><code>public static void configure(File file)</code></li>
068: </ul>
069: </li>
070: </ul>
071:
072: <p>Loggers will often wrap other logging APIs. By having a consistant
073: interface to log facilities you do not need to embed code from any
074: one log facility into your system.
075:
076: <b>This class is deprecated.</b> All EdenLib classes now use the
077: Apache Jakarta Commons logging library.
078:
079: @author Anthony Eden
080: @deprecated
081: */
082:
083: public class LogManager {
084:
085: /** Configure the underlying log implementation.
086:
087: @throws Exception
088: */
089:
090: public static final void configure() throws Exception {
091: Class loggerClass = ClassUtilities.loadClass(
092: getLoggerClassName(), LogManager.class);
093: MethodUtilities.invoke("configure", null, loggerClass, null);
094: }
095:
096: /** Configure the underlying log implementation using the given
097: file.
098:
099: @throws Exception
100: */
101:
102: public static final void configure(File file) throws Exception {
103: Class loggerClass = ClassUtilities.loadClass(
104: getLoggerClassName(), LogManager.class);
105: MethodUtilities.invoke("configure", null, loggerClass, file);
106: }
107:
108: /** Return the name of the logger implementation class to use.
109:
110: @return The logger implementation classname
111: */
112:
113: public static String getLoggerClassName() {
114: return loggerClassName;
115: }
116:
117: /** Set the name of the logger implementation class to use.
118:
119: @param className The new logger implementation classname
120: */
121:
122: public static void setLoggerClassName(String className) {
123: loggerClassName = className;
124: }
125:
126: /** Get a logger for the given object. This method will use the
127: objects class name as the logging identifier.
128:
129: @param object The object
130: @return The logger
131: */
132:
133: public static Logger getLogger(Object object) {
134: return getLogger(object.getClass().getName());
135: }
136:
137: /** Get the logger using the given name.
138:
139: @param name The name of the logger
140: @return The logger
141: */
142:
143: public static synchronized Logger getLogger(String name) {
144: LoggerInternal logger = (LoggerInternal) loggers.get(name);
145: if (logger == null) {
146: try {
147: logger = (LoggerInternal) ClassUtilities.loadClass(
148: loggerClassName, LogManager.class)
149: .newInstance();
150: } catch (Exception e) {
151: logger = defaultLogger;
152: }
153:
154: logger.init(name);
155: loggers.put(name, logger);
156: }
157: return logger;
158: }
159:
160: public static final int DEBUG = 1;
161: public static final int INFO = 2;
162: public static final int WARN = 3;
163: public static final int ERROR = 4;
164: public static final int FATAL = 5;
165:
166: private static String loggerClassName = SystemErrorLogger.class
167: .getName();
168: private static LoggerInternal defaultLogger = new Log4JLogger();
169: private static HashMap loggers = new HashMap();
170:
171: }
|