    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/framework/log/JCLLogger.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    import java.net.URL;
    039
    040    import org.apache.commons.logging.Log;
    041    import org.apache.commons.logging.LogFactory;
    042    import org.apache.log4j.BasicConfigurator;
    043    import org.apache.log4j.PropertyConfigurator;
    044
    045    /**
    046     * Log service provided for Apache Commons Logging (JCL) log service.
    047     *
    048     * @author <a href="mailto:tonnhofer@lat-lon.de">Oliver Tonnhofer</a>
    049     *
    050     * @author last edited by: UID=$Author: mschneider $
    051     *
    052     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    053     */
    054    public class JCLLogger extends LoggerService {
    055
    056        private Log log;
    057
    058        private static String LOG4J_PROP_FILE = "log4j.properties";
    059
    060        private static String LOG4JLOGGER_CLASS = "org.apache.commons.logging.impl.Log4JLogger";
    061
    062        static {
    063            Log log = null;
    064            try {
    065                log = LogFactory.getLog( JCLLogger.class );
    066            } catch ( NoClassDefFoundError ncdfe ) {
    067                StringBuilder sb = new StringBuilder( 1024 );
    068                sb.append( " Could not instantiate the logger because(" );
    069                sb.append( ncdfe.getMessage() );
    070                sb.append( " to use the deegree logging framewormk, please put following libraries in your classpath:" );
    071                sb.append( "\n - $deegree-base$/lib/commons/commons-logging.jar" );
    072                sb.append( "\n - $deegree-base$/lib/log4j/log4j-1.2.9.jar" );
    073                System.out.println( sb.toString() );
    074                throw ncdfe;
    075            }
    076
    077            // when log4j is used load log4j.properties from class path or package resource
    078            // for backwards compatiblity with old Log4JLogger
    079            if ( log.getClass().getName().equals( LOG4JLOGGER_CLASS ) ) {
    080                URL urlToLog4jProps = JCLLogger.class.getResource( "/" + LOG4J_PROP_FILE );
    081                if ( urlToLog4jProps == null ) {
    082                    urlToLog4jProps = JCLLogger.class.getResource( LOG4J_PROP_FILE );
    083                }
    084                if ( urlToLog4jProps != null ) {
    085                    PropertyConfigurator.configure( urlToLog4jProps );
    086                    log.debug( "Log4J: found log4j.properties, initialized the Logger with configuration found in file "
    087                               + urlToLog4jProps );
    088                } else {
    089                    // Set up a simple configuration that logs on the console.
    090                    BasicConfigurator.configure();
    091                    log.warn( "Log4J: No log4j.properties found, initialized Log4J with a BasicConfiguration." );
    092                }
    093            } else {
    094                try {
    095                    @SuppressWarnings("unused")
    096                    Class<?> log4jclass = Class.forName( LOG4JLOGGER_CLASS );
    097                } catch ( ClassNotFoundException e ) {
    098                    log.warn( "Logging: Did not find complete version of Apache Commons Logging. Provide a propper commons-logging.jar in your lib/ directory for full logging functionality." );
    099                }
    100            }
    101        }
    102
    103        public void bindClass( String name ) {
    104            this.log = LogFactory.getLog( name );
    105        }
    106
    107        public void bindClass( Class<?> name ) {
    108            this.log = LogFactory.getLog( name );
    109        }
    110
    111        public int getLevel() {
    112            if ( log.isDebugEnabled() || log.isTraceEnabled() ) {
    113                return ILogger.LOG_DEBUG;
    114            } else if ( log.isInfoEnabled() ) {
    115                return ILogger.LOG_INFO;
    116            } else if ( log.isWarnEnabled() ) {
    117                return ILogger.LOG_WARNING;
    118            } else { // log.isErrorEnabled() || log.isFatalEnabled()
    119                return ILogger.LOG_ERROR;
    120            }
    121        }
    122
    123        public void setLevel( int level ) {
    124            log.error( "Can't change log level at runtime. " + "Use the appropriate properties file for configuration." );
    125        }
    126
    127        public boolean isDebug() {
    128            return log.isDebugEnabled();
    129        }
    130
    131        @Override
    132        public void logDebug( String message ) {
    133            log.debug( message );
    134        }
    135
    136        @Override
    137        public void logDebug( String message, Throwable e ) {
    138            log.debug( message, e );
    139        }
    140
    141        public void logDebug( String message, Object tracableObject ) {
    142            if ( log.isDebugEnabled() ) {
    143                log.debug( message + tracableObject );
    144            }
    145        }
    146
    147        public void logDebug( String message, Object... tracableObjects ) {
    148            if ( log.isDebugEnabled() ) {
    149                log.debug( stringFromObjects( message, tracableObjects ) );
    150            }
    151        }
    152
    153        public void logError( String message ) {
    154            log.error( message );
    155        }
    156
    157        public void logError( String message, Throwable e ) {
    158            log.error( message, e );
    159        }
    160
    161        @Override
    162        public void logInfo( String message ) {
    163            log.info( message );
    164        }
    165
    166        @Override
    167        public void logInfo( String message, Throwable e ) {
    168            log.info( message, e );
    169        }
    170
    171        public void logInfo( String message, Object tracableObject ) {
    172            log.info( message + ": " + tracableObject );
    173        }
    174
    175        public void logInfo( String message, Object... tracableObject ) {
    176            if ( log.isInfoEnabled() ) {
    177                log.info( stringFromObjects( message, tracableObject ) );
    178            }
    179        }
    180
    181        @Override
    182        public void logWarning( String message ) {
    183            log.warn( message );
    184        }
    185
    186        @Override
    187        public void logWarning( String message, Throwable e ) {
    188            log.warn( message, e );
    189        }
    190
    191        // not used?
    192        public void log( int priority, String message, Throwable ex ) {
    193            logDebug( message, ex );
    194        }
    195
    196        // not used?
    197        public void log( int priority, String message, Object source, Throwable ex ) {
    198            logDebug( message, ex );
    199        }
    200
    201        private String stringFromObjects( String message, Object... objects ) {
    202            StringBuilder sb = new StringBuilder( message );
    203            for ( Object part : objects ) {
    204                sb.append( ' ' ).append( part.toString() );
    205            }
    206            return sb.toString();
    207        }
    208
    209        public void logError( Throwable e ) {
    210            if ( e != null ) {
    211                logError( e.getLocalizedMessage(), e );
    212            } else {
    213                logError( "No exception to log.", new NullPointerException( "No exception to log." ) );
    214            }
    215        }
    216    }