001 // $HeadURL: /cvsroot/deegree/src/org/deegree/framework/log/JavaLogger.java,v 1.2
002 // 2004/06/15 14:58:19 tf Exp $
003 /*---------------- FILE HEADER ------------------------------------------
004
005 This file is part of deegree.
006 Copyright (C) 2001-2006 by:
007 EXSE, Department of Geography, University of Bonn
008 http://www.giub.uni-bonn.de/deegree/
009 lat/lon GmbH
010 http://www.lat-lon.de
011
012 This library is free software; you can redistribute it and/or
013 modify it under the terms of the GNU Lesser General Public
014 License as published by the Free Software Foundation; either
015 version 2.1 of the License, or (at your option) any later version.
016
017 This library is distributed in the hope that it will be useful,
018 but WITHOUT ANY WARRANTY; without even the implied warranty of
019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020 Lesser General Public License for more details.
021
022 You should have received a copy of the GNU Lesser General Public
023 License along with this library; if not, write to the Free Software
024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025
026 Contact:
027
028 Andreas Poth
029 lat/lon GmbH
030 Aennchenstr. 19
031 53115 Bonn
032 Germany
033 E-Mail: poth@lat-lon.de
034
035 Prof. Dr. Klaus Greve
036 Department of Geography
037 University of Bonn
038 Meckenheimer Allee 166
039 53115 Bonn
040 Germany
041 E-Mail: greve@giub.uni-bonn.de
042
043
044 ---------------------------------------------------------------------------*/
045
046 package org.deegree.framework.log;
047
048 import java.util.logging.ConsoleHandler;
049 import java.util.logging.Level;
050
051 import org.deegree.framework.util.StringTools;
052
053 /**
054 * Log service provided by Java logging API.<BR/>
055 * The log environment is fully configurable using a configuration file.
056 * The configuration file name is <code>logging.properties</code>.
057 *
058 * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A>
059 *
060 * @author last edited by: $Author: bezema $
061 *
062 * @version 2.0, $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
063 *
064 * @see <a
065 * href="http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html">Java
066 * Logging Overview </a>
067 *
068 * @since 2.0
069 */
070 final class JavaLogger extends LoggerService {
071
072 private java.util.logging.Logger log;
073
074 /**
075 *
076 *
077 */
078 JavaLogger() {
079 super();
080 }
081
082 /**
083 * @see org.deegree.framework.log.ILogger#bindClass(java.lang.String)
084 */
085 public void bindClass( String string ) {
086 log = java.util.logging.Logger.getLogger( string );
087
088 try {
089 // java.util.logging.Handler handler = new FileHandler( "%t" + defaultChannelName
090 // + "_%g.log", 100000, 3, true );
091 java.util.logging.Handler handler = new ConsoleHandler();
092 handler.setFormatter( new java.util.logging.SimpleFormatter() );
093 log.addHandler( handler );
094 } catch ( SecurityException e ) {
095 log.warning( e.getMessage() );
096 }
097 }
098
099 /**
100 * @see org.deegree.framework.log.ILogger#bindClass(java.lang.Class)
101 */
102 public void bindClass( Class class1 ) {
103 bindClass( class1.getName() );
104 }
105
106 /**
107 * @see org.deegree.framework.log.ILogger#logDebug(java.lang.String, java.lang.Throwable)
108 */
109 @Override
110 public void logDebug( String string, Throwable throwable ) {
111 if ( throwable != null )
112 log.fine( new StringBuffer().append( string ).append( ":" ).append(
113 throwable.getMessage() ).toString() );
114
115 else
116 log.fine( string );
117 }
118
119 /**
120 * @see org.deegree.framework.log.ILogger#logInfo(java.lang.String, java.lang.Throwable)
121 */
122 @Override
123 public void logInfo( String string, Throwable throwable ) {
124 if ( throwable != null ) {
125 StringBuffer sb = new StringBuffer( 200 );
126 sb.append( string ).append( ':' ).append( throwable.getMessage() );
127 log.info( sb.toString() );
128
129 } else {
130 log.info( string );
131 }
132 }
133
134 /**
135 * @see org.deegree.framework.log.ILogger#logWarning(java.lang.String, java.lang.Throwable)
136 */
137 @Override
138 public void logWarning( String string, Throwable throwable ) {
139 if ( throwable != null ) {
140 StringBuffer sb = new StringBuffer( 200 );
141 sb.append( string ).append( ':' ).append( throwable.getMessage() );
142 log.warning( sb.toString() );
143
144 } else {
145 log.warning( string );
146 }
147 }
148
149 /**
150 * @see org.deegree.framework.log.ILogger#logError(java.lang.String, java.lang.Throwable)
151 */
152 public void logError( String string, Throwable throwable ) {
153 if ( throwable != null )
154 log.severe( StringTools.concat( 1000, string, ':',
155 StringTools.stackTraceToString( throwable ) ) );
156
157 else
158 log.severe( string );
159
160 sendMail( string, throwable, null );
161 }
162
163 /**
164 * @see org.deegree.framework.log.ILogger#logDebug(java.lang.String)
165 */
166 @Override
167 public void logDebug( String string ) {
168 logDebug( string, null );
169 }
170
171 /**
172 * @see org.deegree.framework.log.ILogger#logInfo(java.lang.String)
173 */
174 @Override
175 public void logInfo( String string ) {
176 logInfo( string, null );
177 }
178
179 /**
180 * @see org.deegree.framework.log.ILogger#logWarning(java.lang.String)
181 */
182 @Override
183 public void logWarning( String string ) {
184 logWarning( string, null );
185 }
186
187 /**
188 * @see org.deegree.framework.log.ILogger#logError(java.lang.String)
189 */
190 public void logError( String string ) {
191 logError( string, null );
192 }
193
194 /**
195 * @see org.deegree.framework.log.ILogger#logInfo(java.lang.String, java.lang.Object)
196 */
197 public void logInfo( String string, Object object ) {
198 if ( object != null )
199 logInfo( new StringBuffer().append( string ).append( object.toString() ).toString() );
200
201 else
202 logInfo( string );
203 }
204
205 /**
206 * @see org.deegree.framework.log.ILogger#logDebug(java.lang.String, java.lang.Object)
207 */
208 public void logDebug( String string, Object object ) {
209 if ( object != null )
210 logDebug( new StringBuffer().append( string ).append( object.toString() ).toString() );
211
212 else
213 logDebug( string );
214 }
215
216 /**
217 * @see org.deegree.framework.log.ILogger#log(int, java.lang.String, java.lang.Throwable)
218 */
219 public void log( int i, String string, Throwable throwable ) {
220 log.log( Level.INFO, string, throwable );
221 }
222
223 /**
224 * @see org.deegree.framework.log.ILogger#log(int, java.lang.String, java.lang.Object, java.lang.Throwable)
225 */
226 public void log( int i, String string, Object object, Throwable throwable ) {
227 log.log( Level.INFO,
228 new StringBuffer().append( string ).append( ":" ).append( throwable.getMessage() ).toString(),
229 object );
230 }
231
232 /**
233 * @see java.lang.Object#toString()
234 */
235 @Override
236 public String toString() {
237 return ( new StringBuffer().append( "Logging Class: " ).append( log.getClass().getName() ).toString() );
238 }
239
240 /**
241 * @see org.deegree.framework.log.ILogger#getLevel()
242 */
243 public int getLevel() {
244 return this.getInternalLevel( this.log.getLevel() );
245 }
246
247 /**
248 * @see org.deegree.framework.log.ILogger#setLevel(int)
249 */
250 public void setLevel( int level ) {
251 this.log.setLevel( this.getJavaLogLevel( level ) );
252 }
253
254 private Level getJavaLogLevel( int logLevel ) {
255 Level javaloglevel;
256 switch ( logLevel ) {
257 case ILogger.LOG_DEBUG:
258 javaloglevel = Level.FINEST;
259 break;
260 case ILogger.LOG_INFO:
261 javaloglevel = Level.INFO;
262 break;
263 case ILogger.LOG_WARNING:
264 javaloglevel = Level.WARNING;
265 break;
266 case ILogger.LOG_ERROR:
267 javaloglevel = Level.SEVERE;
268 break;
269 default:
270 javaloglevel = Level.INFO;
271 break;
272 }
273 return javaloglevel;
274 }
275
276 private int getInternalLevel( Level javaLogLevel ) {
277 int intloglevel = ILogger.LOG_INFO;
278 if ( Level.FINEST.equals( javaLogLevel ) ) {
279 intloglevel = ILogger.LOG_DEBUG;
280 } else if ( Level.INFO.equals( javaLogLevel ) ) {
281 intloglevel = ILogger.LOG_INFO;
282 } else if ( Level.WARNING.equals( javaLogLevel ) ) {
283 intloglevel = ILogger.LOG_WARNING;
284 } else if ( Level.SEVERE.equals( javaLogLevel ) ) {
285 intloglevel = ILogger.LOG_ERROR;
286 }
287 return intloglevel;
288 }
289
290 public boolean isDebug() {
291 return ( log.getLevel().intValue() == Level.FINEST.intValue() );
292 }
293
294 }
295
296 /*******************************************************************************
297 * Changes to this class. What the people have been up to: $Log:
298 * JavaLogger.java,v $ Revision 1.2 2004/06/15 14:58:19 tf refactored Logger and
299 * add new methods
300 *
301 * Revision 1.1 2004/05/14 15:26:38 tf initial checkin
302 *
303 *
304 ******************************************************************************/