001 // SECTION-START[License Header]
002 // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003 /*
004 * Copyright (c) 2009 The JOMC Project
005 * Copyright (c) 2005 Christian Schulte <cs@jomc.org>
006 * All rights reserved.
007 *
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions
010 * are met:
011 *
012 * o Redistributions of source code must retain the above copyright
013 * notice, this list of conditions and the following disclaimer.
014 *
015 * o Redistributions in binary form must reproduce the above copyright
016 * notice, this list of conditions and the following disclaimer in
017 * the documentation and/or other materials provided with the
018 * distribution.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 *
032 * $Id: JdkLogger.java 1131 2009-12-09 23:42:18Z schulte2005 $
033 *
034 */
035 // </editor-fold>
036 // SECTION-END
037 package org.jomc.logging.ri.jdk;
038
039 import java.util.logging.Level;
040
041 // SECTION-START[Documentation]
042 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
043 /**
044 * JDK logging system implementation.
045 * <p><b>Specifications</b><ul>
046 * <li>{@code org.jomc.logging.Logger} {@code 1.0} {@code Multiton}</li>
047 * </ul></p>
048 * <p><b>Properties</b><ul>
049 * <li>"{@link #getName name}"
050 * <blockquote>Property of type {@code java.lang.String}.
051 * <p>Name of the component events are logged for.</p>
052 * </blockquote></li>
053 * <li>"{@link #getStackDepth stackDepth}"
054 * <blockquote>Property of type {@code int}.
055 * <p>Number of frames between the logger and the frame of the caller.</p>
056 * </blockquote></li>
057 * </ul></p>
058 *
059 * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0
060 * @version $Id: JdkLogger.java 1131 2009-12-09 23:42:18Z schulte2005 $
061 */
062 // </editor-fold>
063 // SECTION-END
064 // SECTION-START[Annotations]
065 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
066 @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
067 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-11/jomc-tools" )
068 // </editor-fold>
069 // SECTION-END
070 public class JdkLogger
071 implements
072 org.jomc.logging.Logger
073 {
074 // SECTION-START[Constructors]
075 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
076
077 /** Creates a new {@code JdkLogger} instance. */
078 @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
079 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-11/jomc-tools" )
080 public JdkLogger()
081 {
082 // SECTION-START[Default Constructor]
083 super();
084 // SECTION-END
085 }
086 // </editor-fold>
087 // SECTION-END
088 // SECTION-START[JdkLogger]
089
090 public boolean isDebugEnabled()
091 {
092 return this.getLogger().isLoggable( Level.FINE );
093 }
094
095 public void debug( final String message )
096 {
097 this.log( Level.FINE, message, null );
098 }
099
100 public void debug( final Throwable throwable )
101 {
102 this.log( Level.FINE, throwable.getMessage(), throwable );
103 }
104
105 public void debug( final String message, final Throwable throwable )
106 {
107 this.log( Level.FINE, message, throwable );
108 }
109
110 public boolean isErrorEnabled()
111 {
112 return this.getLogger().isLoggable( Level.SEVERE );
113 }
114
115 public void error( final String message )
116 {
117 this.log( Level.SEVERE, message, null );
118 }
119
120 public void error( final Throwable throwable )
121 {
122 this.log( Level.SEVERE, throwable.getMessage(), throwable );
123 }
124
125 public void error( final String message, final Throwable throwable )
126 {
127 this.log( Level.SEVERE, message, throwable );
128 }
129
130 public boolean isFatalEnabled()
131 {
132 return this.getLogger().isLoggable( Level.SEVERE );
133 }
134
135 public void fatal( final String message )
136 {
137 this.log( Level.SEVERE, message, null );
138 }
139
140 public void fatal( final Throwable throwable )
141 {
142 this.log( Level.SEVERE, throwable.getMessage(), throwable );
143 }
144
145 public void fatal( final String message, final Throwable throwable )
146 {
147 this.log( Level.SEVERE, message, throwable );
148 }
149
150 public boolean isInfoEnabled()
151 {
152 return this.getLogger().isLoggable( Level.INFO );
153 }
154
155 public void info( final String message )
156 {
157 this.log( Level.INFO, message, null );
158 }
159
160 public void info( final Throwable throwable )
161 {
162 this.log( Level.INFO, throwable.getMessage(), throwable );
163 }
164
165 public void info( final String message, final Throwable throwable )
166 {
167 this.log( Level.INFO, message, throwable );
168 }
169
170 public boolean isTraceEnabled()
171 {
172 return this.getLogger().isLoggable( Level.FINEST );
173 }
174
175 public void trace( final String message )
176 {
177 this.log( Level.FINEST, message, null );
178 }
179
180 public void trace( final Throwable throwable )
181 {
182 this.log( Level.FINEST, throwable.getMessage(), throwable );
183 }
184
185 public void trace( final String message, final Throwable throwable )
186 {
187 this.log( Level.FINEST, message, throwable );
188 }
189
190 public boolean isWarnEnabled()
191 {
192 return this.getLogger().isLoggable( Level.WARNING );
193 }
194
195 public void warn( final String message )
196 {
197 this.log( Level.WARNING, message, null );
198 }
199
200 public void warn( final Throwable throwable )
201 {
202 this.log( Level.WARNING, throwable.getMessage(), throwable );
203 }
204
205 public void warn( final String message, final Throwable throwable )
206 {
207 this.log( Level.WARNING, message, throwable );
208 }
209
210 /**
211 * Requests the JDK logger for the name given by property {@code name}.
212 *
213 * @return the JDK logger for the name given by property {@code name}.
214 */
215 public java.util.logging.Logger getLogger()
216 {
217 return java.util.logging.Logger.getLogger( this.getName() );
218 }
219
220 private void log( final Level level, final String msg, final Throwable t )
221 {
222 if ( this.getLogger().isLoggable( level ) )
223 {
224 StackTraceElement caller;
225 final Throwable x = new Throwable();
226 final StackTraceElement[] elements = x.getStackTrace();
227
228 String cname = "unknown";
229 String method = "unknown";
230
231 if ( elements != null && elements.length >= this.getStackDepth() )
232 {
233 caller = elements[this.getStackDepth()];
234 cname = caller.getClassName();
235 method = caller.getMethodName();
236 }
237
238 if ( t == null )
239 {
240 this.getLogger().logp( level, cname, method, msg );
241 }
242 else
243 {
244 this.getLogger().logp( level, cname, method, msg, t );
245 }
246 }
247 }
248
249 // SECTION-END
250 // SECTION-START[Dependencies]
251 // SECTION-END
252 // SECTION-START[Properties]
253 // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
254
255 /**
256 * Gets the value of the {@code name} property.
257 * @return Name of the component events are logged for.
258 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
259 */
260 @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
261 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-11/jomc-tools" )
262 public java.lang.String getName()
263 {
264 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "name" );
265 assert _p != null : "'name' property not found.";
266 return _p;
267 }
268
269 /**
270 * Gets the value of the {@code stackDepth} property.
271 * @return Number of frames between the logger and the frame of the caller.
272 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
273 */
274 @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
275 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-11/jomc-tools" )
276 private int getStackDepth()
277 {
278 final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "stackDepth" );
279 assert _p != null : "'stackDepth' property not found.";
280 return _p.intValue();
281 }
282 // </editor-fold>
283 // SECTION-END
284 // SECTION-START[Messages]
285 // SECTION-END
286 }