001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/framework/log/LoggerFactory.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.framework.log;
045:
046: // JDK 1.3
047: import java.io.InputStream;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.Map;
051: import java.util.Properties;
052:
053: import org.deegree.framework.util.BootLogger;
054:
055: /**
056: * The LoggerFactory is used to get a concrete logging service. The logging service can be provided
057: * by the application server or a 3rd party logging service such as Apache log4j. The logging
058: * service is configured by a set of Properties which are provided to the class init.
059: * <p>
060: * There are some global properties as well: <BR>
061: * <UL>
062: * <LI><B>log.class </B>: the logging class.
063: * </UL>
064: * To use the Log4J logging framework set this property: <code>
065: * log.class=org.deegree_impl.log.Log4JLogger
066: * </code>
067: * Other supported logging facilites are the Java logging API with
068: * <code>org.deegree_impl.log.JavaLogger</code> (default), and JBoss Logserver with
069: * <code>org.deegree_impl.log.JBossLogger</code>.
070: * <P>
071: * <B>Example Code: </B> <code>
072: * public class MyClass {<BR>
073: * private static ILogger logger = LoggerFactory.getLogger(this.getClass());<BR>
074: * ...<BR>
075: * public void doSomething() {<BR>
076: * logger.logDebug("have done something");<BR>
077: * }<BR>
078: * </code>
079: *
080: * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A>
081: *
082: * @author last edited by: $Author: rbezema $
083: *
084: * @version $Revision: 10596 $, $Date: 2008-03-17 06:47:25 -0700 (Mon, 17 Mar 2008) $
085: *
086: * @see ILogger
087: *
088: */
089: public final class LoggerFactory {
090:
091: /** logging class name */
092: private static String LOG_CLASS;
093:
094: /** default log handler, if not specified in resources */
095: private static final String DEFAULT_LOG_CLS = "org.deegree.framework.log.JavaLogger";
096:
097: /**
098: * Stores all named loggers.
099: */
100: private static final Map<String, ILogger> NAMED_LOGGER = Collections
101: .synchronizedMap(new HashMap<String, ILogger>());
102:
103: /**
104: * Initialization done at class loading time
105: */
106: static {
107: try {
108: // fetch all configuration parameters
109: Properties props = new Properties();
110: InputStream is = LoggerService.class
111: .getResourceAsStream("/LoggerService.properties");
112: if (is == null) {
113: is = LoggerService.class
114: .getResourceAsStream("LoggerService.properties");
115: }
116: props.load(is);
117: LOG_CLASS = props.getProperty("log.class");
118: // try to load the logger class
119: ILogger log = (ILogger) Class.forName(LOG_CLASS)
120: .newInstance();
121: log.init(props);
122: is.close();
123: } catch (Throwable ex) {
124: LOG_CLASS = DEFAULT_LOG_CLS;
125: BootLogger.logError("Error while initializing "
126: + LoggerFactory.class.getName() + " : "
127: + ex.getMessage(), ex);
128: } finally {
129: BootLogger.logDebug("Using Logging Class: " + LOG_CLASS);
130: }
131: }
132:
133: /**
134: * Nothing to do, constructor is hidden
135: */
136: private LoggerFactory() {
137: // constructor is hidden.
138: }
139:
140: /**
141: * Factory method to retrieve the instance of the concrete logging class. Return the named
142: * logger for the given name.
143: *
144: * @param name
145: * of the logger
146: *
147: * @return the assigned logger
148: */
149: public static final ILogger getLogger(String name) {
150: if (LOG_CLASS == null) {
151: return null;
152: }
153:
154: ILogger logger = NAMED_LOGGER.get(name);
155:
156: if (logger == null) {
157: try {
158: logger = (ILogger) Class.forName(LOG_CLASS)
159: .newInstance();
160: logger.bindClass(name);
161: NAMED_LOGGER.put(name, logger);
162: } catch (Throwable ex) {
163: BootLogger.logError(
164: "Logging system is failing, shutting down?",
165: null);
166: }
167: }
168:
169: return logger;
170: }
171:
172: /**
173: * Return the named logger for the given class
174: *
175: * @param name
176: * the class to be logged
177: *
178: * @return the assigned logger
179: */
180: public static final ILogger getLogger(Class<?> name) {
181: return LoggerFactory.getLogger(name.getName());
182: }
183: }
|