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