001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/framework/log/LoggerFactory.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2006 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
057 * service can be provided by the application server or a 3rd party logging
058 * service such as Apache log4j. The logging service is configured by a set of
059 * Properties which are provided to the class init.
060 * <p>
061 * There are some global properties as well: <BR>
062 * <UL>
063 * <LI><B>log.class </B>: the logging class.
064 * </UL>
065 * To use the Log4J logging framework set this property: <code>
066 * log.class=org.deegree_impl.log.Log4JLogger
067 * </code>
068 * Other supported logging facilites are the Java logging API with
069 * <code>org.deegree_impl.log.JavaLogger</code> (default), and JBoss Logserver
070 * with <code>org.deegree_impl.log.JBossLogger</code>.
071 * <P>
072 * <B>Example Code: </B> <code>
073 * public class MyClass {<BR>
074 * private static ILogger logger = LoggerFactory.getLogger(this.getClass());<BR>
075 * ...<BR>
076 * public void doSomething() {<BR>
077 * logger.logDebug("have done something");<BR>
078 * }<BR>
079 * </code>
080 *
081 * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A>
082 *
083 * @author last edited by: $Author: bezema $
084 *
085 * @version $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
086 *
087 * @see ILogger
088 *
089 */
090 public final class LoggerFactory {
091
092 /** logging class name */
093 private static String LOG_CLASS;
094
095 /** default log handler, if not specified in resources */
096 private static final String DEFAULT_LOG_CLS = "org.deegree.framework.log.JavaLogger";
097
098 /**
099 * Stores all named loggers.
100 */
101 private static final Map<String, ILogger> NAMED_LOGGER = Collections.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.getResourceAsStream("/LoggerService.properties");
111 if (is == null) {
112 is = LoggerService.class.getResourceAsStream("LoggerService.properties");
113 }
114 props.load(is);
115 LOG_CLASS = props.getProperty("log.class");
116 // try to load the logger class
117 ILogger log = (ILogger) Class.forName(LOG_CLASS).newInstance();
118 log.init (props);
119 is.close();
120 } catch (Throwable ex) {
121 LOG_CLASS = DEFAULT_LOG_CLS;
122 BootLogger.logError("Error while initializing "
123 + LoggerFactory.class.getName() + " : " + ex.getMessage(),
124 ex);
125 } finally {
126 BootLogger.log("Using Logging Class: " + LOG_CLASS);
127 }
128 }
129
130 /**
131 * Nothing to do, constructor is hidden
132 */
133 private LoggerFactory() {
134 //constructor is hidden.
135 }
136
137 /**
138 * Factory method to retrieve the instance of the concrete logging class.
139 * Return the named logger for the given name.
140 *
141 * @param name
142 * of the logger
143 *
144 * @return the assigned logger
145 */
146 public static final ILogger getLogger(String name) {
147 ILogger logger = NAMED_LOGGER.get(name);
148
149 if (logger == null) {
150 try {
151 logger = (ILogger) Class.forName(LOG_CLASS).newInstance();
152 logger.bindClass(name);
153 NAMED_LOGGER.put(name, logger);
154 } catch (Throwable ex) {
155 //BootLogger.logError("Exception: " + ex.getMessage(), ex);
156 BootLogger.logError( "Logging system is failing, shutting down?", null );
157 }
158 }
159
160 return logger;
161 }
162
163 /**
164 * Return the named logger for the given class
165 *
166 * @param name
167 * the class to be logged
168 *
169 * @return the assigned logger
170 */
171 public static final ILogger getLogger(Class name) {
172 return LoggerFactory.getLogger(name.getName());
173 }
174 }
175
176 /* ****************************************************************************
177 * Changes to this class. What the people have been up to: $Log:
178 * LoggerFactory.java,v $ Revision 1.3 2004/06/15 15:06:35 tf fixed bugs
179 *
180 * Revision 1.2 2004/06/15 14:58:19 tf refactored Logger and add new methods
181 *
182 * Revision 1.1 2004/05/14 15:26:38 tf initial checkin
183 *
184 * **************************************************************************** */