001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/framework/log/JCLLogger.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    import java.net.URL;
047    
048    import org.apache.commons.logging.Log;
049    import org.apache.commons.logging.LogFactory;
050    import org.apache.log4j.BasicConfigurator;
051    import org.apache.log4j.PropertyConfigurator;
052    
053    /**
054     * Log service provided for Apache Commons Logging (JCL) log service.
055     * 
056     * @author <a href="mailto:tonnhofer@lat-lon.de">Oliver Tonnhofer</a>
057     * 
058     * @author last edited by: UID=$Author: otonnhofer $
059     *
060     * @version $Revision: 9720 $, $Date: 2008-01-24 10:57:36 +0100 (Do, 24 Jan 2008) $
061     */
062    public class JCLLogger extends LoggerService {
063        
064        Log log;
065     
066        private static String LOG4J_PROP_FILE = "log4j.properties";
067        private static String LOG4JLOGGER_CLASS = "org.apache.commons.logging.impl.Log4JLogger";
068    
069        static {
070            Log log = LogFactory.getLog( JCLLogger.class );
071        
072            // when log4j is used load log4j.properties from class path or package resource
073            // for backwards compatiblity with old Log4JLogger
074            if ( log.getClass().getName().equals( LOG4JLOGGER_CLASS ) ) {
075                URL urlToLog4jProps = JCLLogger.class.getResource( "/" + LOG4J_PROP_FILE );
076                if ( urlToLog4jProps == null ) {
077                    urlToLog4jProps = JCLLogger.class.getResource( LOG4J_PROP_FILE );
078                }
079                if ( urlToLog4jProps != null ) {
080                    PropertyConfigurator.configure( urlToLog4jProps );
081                    log.debug( "Log4J: found log4j.properties, initialized the Logger with configuration found in file "
082                               + urlToLog4jProps );
083                } else {
084                    // Set up a simple configuration that logs on the console.
085                    BasicConfigurator.configure();
086                    log.warn( "Log4J: No log4j.properties found, initialized Log4J with a BasicConfiguration." );
087                }
088            }
089        }
090    
091        public void bindClass( String name ) {
092            this.log = LogFactory.getLog( name );
093        }
094    
095        public void bindClass( Class name ) {
096            this.log = LogFactory.getLog( name );
097        }
098    
099        public int getLevel() {
100            if ( log.isDebugEnabled() || log.isTraceEnabled() ) { 
101                return ILogger.LOG_DEBUG;
102            } else if ( log.isInfoEnabled() ) {
103                return ILogger.LOG_INFO;
104            } else if ( log.isWarnEnabled() ) {
105                return ILogger.LOG_WARNING; 
106            } else { // log.isErrorEnabled() || log.isFatalEnabled()
107                return ILogger.LOG_ERROR;
108            } 
109        }
110        
111    
112        public void setLevel( int level ) {
113            log.error( "Can't change log level at runtime. " +
114                       "Use the appropriate properties file for configuration." );
115        }
116    
117        public boolean isDebug() {
118            return log.isDebugEnabled();
119        }
120    
121        public void logDebug( String message ) {
122            log.debug( message );
123        }
124    
125        public void logDebug( String message, Throwable e ) {
126            log.debug( message, e );
127        }
128    
129        public void logDebug( String message, Object tracableObject ) {
130            log.debug( message + ": " + tracableObject );
131        }
132        
133        public void logDebug( String message, Object... tracableObjects ) {
134            if ( log.isDebugEnabled() ) {
135                log.debug( stringFromObjects( message, tracableObjects ) );
136            }
137        }
138    
139     
140        public void logError( String message ) {
141            log.error( message );
142        }
143    
144        public void logError( String message, Throwable e ) {
145            log.error( message, e );
146        }
147    
148        
149        public void logInfo( String message ) {
150            log.info( message );
151        }
152        
153        public void logInfo( String message, Throwable e ) {
154            log.info( message, e );
155        }
156    
157        public void logInfo( String message, Object tracableObject ) {
158            log.info( message + ": " + tracableObject );
159        }
160    
161        public void logInfo( String message, Object... tracableObject ) {
162            if ( log.isInfoEnabled() ) {
163                log.info( stringFromObjects( message, tracableObject ) );
164            }
165        }
166    
167        public void logWarning( String message ) {
168            log.warn( message );
169        }
170    
171        public void logWarning( String message, Throwable e ) {
172            log.warn( message, e );
173        }
174        
175        // not used?
176        public void log( int priority, String message, Throwable ex ) {
177            logDebug( message, ex );
178        }
179    
180        // not used?
181        public void log( int priority, String message, Object source, Throwable ex ) {
182            logDebug( message, ex );
183        }
184    
185        
186        private String stringFromObjects( String message, Object... objects ) {
187            StringBuilder sb = new StringBuilder( message );
188            for ( Object part: objects ) {
189                sb.append( ' ' ).append( part.toString() );
190            }
191            return sb.toString();
192        }    
193    }