001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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: aschmitz $
083     * 
084     * @version $Revision: 9768 $, $Date: 2008-01-25 13:35:59 +0100 (Fr, 25 Jan 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.synchronizedMap( new HashMap<String, ILogger>() );
101    
102        /**
103         * Initialization done at class loading time
104         */
105        static {
106            try {
107                // fetch all configuration parameters
108                Properties props = new Properties();
109                InputStream is = LoggerService.class.getResourceAsStream( "/LoggerService.properties" );
110                if ( is == null ) {
111                    is = LoggerService.class.getResourceAsStream( "LoggerService.properties" );
112                }
113                props.load( is );
114                LOG_CLASS = props.getProperty( "log.class" );
115                // try to load the logger class
116                ILogger log = (ILogger) Class.forName( LOG_CLASS ).newInstance();
117                log.init( props );
118                is.close();
119            } catch ( Throwable ex ) {
120                LOG_CLASS = DEFAULT_LOG_CLS;
121                BootLogger.logError( "Error while initializing " + LoggerFactory.class.getName() + " : " + ex.getMessage(),
122                                     ex );
123            } finally {
124                BootLogger.logDebug( "Using Logging Class: " + LOG_CLASS );
125            }
126        }
127    
128        /**
129         * Nothing to do, constructor is hidden
130         */
131        private LoggerFactory() {
132            // constructor is hidden.
133        }
134    
135        /**
136         * Factory method to retrieve the instance of the concrete logging class. Return the named
137         * logger for the given name.
138         * 
139         * @param name
140         *            of the logger
141         * 
142         * @return the assigned logger
143         */
144        public static final ILogger getLogger( String name ) {
145            if ( LOG_CLASS == null ) {
146                return null;
147            }
148    
149            ILogger logger = NAMED_LOGGER.get( name );
150    
151            if ( logger == null ) {
152                try {
153                    logger = (ILogger) Class.forName( LOG_CLASS ).newInstance();
154                    logger.bindClass( name );
155                    NAMED_LOGGER.put( name, logger );
156                } catch ( Throwable ex ) {
157                    BootLogger.logError( "Logging system is failing, shutting down?", null );
158                }
159            }
160    
161            return logger;
162        }
163    
164        /**
165         * Return the named logger for the given class
166         * 
167         * @param name
168         *            the class to be logged
169         * 
170         * @return the assigned logger
171         */
172        public static final ILogger getLogger( Class name ) {
173            return LoggerFactory.getLogger( name.getName() );
174        }
175    }