001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/framework/log/Log4JLogger.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 53177 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.log4j.BasicConfigurator; 049 import org.apache.log4j.Level; 050 import org.apache.log4j.Logger; 051 import org.apache.log4j.Priority; 052 import org.apache.log4j.PropertyConfigurator; 053 import org.deegree.framework.util.StringTools; 054 055 /** 056 * Log service provided by log4j. The log environment is fully configurable using a configuration 057 * file. The configuration file name is <code>log4j.properties</code>. The default location of 058 * the log file is the system property <code>user.home</code>. 059 * 060 * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A> 061 * @author last edited by: $Author: apoth $ 062 * @version $Revision: 7749 $, $Date: 2007-07-10 19:15:47 +0200 (Di, 10 Jul 2007) $ 063 * 064 * @see <a href="http://jakarta.apache.org/log4j">Log4J home </a> 065 */ 066 final class Log4JLogger extends LoggerService { 067 068 private static String PROP_FILE = "log4j.properties"; 069 070 private Logger log; 071 072 private String className; 073 074 Log4JLogger() { 075 super(); 076 this.bindClass( Log4JLogger.class ); 077 078 URL urlToLog4jProps = Log4JLogger.class.getResource( "/" + PROP_FILE ); 079 if ( urlToLog4jProps == null ) { 080 urlToLog4jProps = Log4JLogger.class.getResource( PROP_FILE ); 081 } 082 if ( urlToLog4jProps != null ) { 083 PropertyConfigurator.configure( urlToLog4jProps ); 084 this.logDebug( "Log4J: found log4j.properties, initialized the Logger with configuration found in file " 085 + urlToLog4jProps ); 086 } else { 087 // Set up a simple configuration that logs on the console. 088 BasicConfigurator.configure(); 089 this.logDebug( "Log4J: No log4j.properties found, initialized Log4J with a BasicConfiguration." ); 090 } 091 } 092 093 public void bindClass( String name ) { 094 log = Logger.getLogger( name ); 095 String[] ss = name.split( "\\." ); 096 this.className = ss[ss.length - 1]; 097 } 098 099 public void bindClass( Class clazz ) { 100 this.bindClass( clazz.getName() ); 101 } 102 103 @Override 104 public void logDebug( String message ) { 105 if ( getLevel() == LOG_DEBUG ) { 106 message = StringTools.concat( 500, '[', this.className, "] ", message ); 107 log.debug( message ); 108 } 109 } 110 111 @Override 112 public void logDebug( String message, Throwable e ) { 113 if ( getLevel() == LOG_DEBUG ) { 114 message = StringTools.concat( 500, '[', this.className, "] ", message ); 115 log.debug( message, e ); 116 } 117 } 118 119 public void logDebug( String message, Object tracableObject ) { 120 if ( getLevel() == LOG_DEBUG ) { 121 message = StringTools.concat( 500, '[', this.className, "] ", message, ": ", tracableObject ); 122 logDebug( message ); 123 } 124 } 125 126 @Override 127 public void logInfo( String message, Throwable e ) { 128 log.info( message, e ); 129 } 130 131 @Override 132 public void logWarning( String message, Throwable e ) { 133 message = StringTools.concat( 500, '[', this.className, "] ", message ); 134 log.warn( message, e ); 135 } 136 137 public void logError( String message, Throwable e ) { 138 message = StringTools.concat( 500, '[', this.className, "] ", message ); 139 log.error( message, e ); 140 } 141 142 @Override 143 public void logInfo( String message ) { 144 log.info( message ); 145 } 146 147 @Override 148 public void logWarning( String message ) { 149 message = StringTools.concat( 500, '[', this.className, "] ", message ); 150 log.warn( message ); 151 } 152 153 public void logError( String message ) { 154 message = StringTools.concat( 500, '[', this.className, "] ", message ); 155 log.error( message ); 156 } 157 158 public void logInfo( String message, Object tracableObject ) { 159 log.info( message + ": " + tracableObject ); 160 } 161 162 public void log( int priority, String message, Throwable ex ) { 163 message = StringTools.concat( 500, '[', this.className, "] ", message ); 164 log.log( Level.DEBUG, message, ex ); 165 } 166 167 public void log( int priority, String message, Object source, Throwable ex ) { 168 message = StringTools.concat( 500, '[', this.className, "] ", message ); 169 log.log( message, Level.DEBUG, source, ex ); 170 } 171 172 @Override 173 public String toString() { 174 return ( "Logging Class: " + this.log.getClass().getName() ); 175 } 176 177 public int getLevel() { 178 return this.getInternalLevel( this.log.getEffectiveLevel() ); 179 } 180 181 public void setLevel( int level ) { 182 this.log.setLevel( this.getLog4JLevel( level ) ); 183 } 184 185 private Level getLog4JLevel( int logLevel ) { 186 Level log4jlevel; 187 switch ( logLevel ) { 188 case ILogger.LOG_DEBUG: 189 log4jlevel = Level.DEBUG; 190 break; 191 case ILogger.LOG_INFO: 192 log4jlevel = Level.INFO; 193 break; 194 case ILogger.LOG_WARNING: 195 log4jlevel = Level.WARN; 196 break; 197 case ILogger.LOG_ERROR: 198 log4jlevel = Level.ERROR; 199 break; 200 default: 201 log4jlevel = Level.INFO; 202 break; 203 } 204 return log4jlevel; 205 } 206 207 private int getInternalLevel( Level log4JLevel ) { 208 int intloglevel = ILogger.LOG_INFO; 209 if ( log4JLevel != null ) { 210 switch ( log4JLevel.toInt() ) { 211 case Priority.DEBUG_INT: 212 intloglevel = ILogger.LOG_DEBUG; 213 break; 214 case Priority.INFO_INT: 215 intloglevel = ILogger.LOG_INFO; 216 break; 217 case Priority.WARN_INT: 218 intloglevel = ILogger.LOG_WARNING; 219 break; 220 case Priority.FATAL_INT: 221 case Priority.ERROR_INT: 222 intloglevel = ILogger.LOG_ERROR; 223 break; 224 default: 225 intloglevel = ILogger.LOG_INFO; 226 break; 227 } 228 } 229 return intloglevel; 230 } 231 232 public boolean isDebug() { 233 return log.isDebugEnabled(); 234 } 235 }