1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.logging.impl;
  17. import java.io.Serializable;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import org.apache.commons.logging.Log;
  21. /**
  22. * <p>Implementation of the <code>org.apache.commons.logging.Log</code>
  23. * interface that wraps the standard JDK logging mechanisms that were
  24. * introduced in the Merlin release (JDK 1.4).</p>
  25. *
  26. * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
  27. * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  28. * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
  29. * @version $Revision: 1.13 $ $Date: 2004/06/06 21:10:21 $
  30. */
  31. public class Jdk14Logger implements Log, Serializable {
  32. // ----------------------------------------------------------- Constructors
  33. /**
  34. * Construct a named instance of this Logger.
  35. *
  36. * @param name Name of the logger to be constructed
  37. */
  38. public Jdk14Logger(String name) {
  39. this.name = name;
  40. logger = getLogger();
  41. }
  42. // ----------------------------------------------------- Instance Variables
  43. /**
  44. * The underlying Logger implementation we are using.
  45. */
  46. protected transient Logger logger = null;
  47. /**
  48. * The name of the logger we are wrapping.
  49. */
  50. protected String name = null;
  51. // --------------------------------------------------------- Public Methods
  52. private void log( Level level, String msg, Throwable ex ) {
  53. Logger logger = getLogger();
  54. if (logger.isLoggable(level)) {
  55. // Hack (?) to get the stack trace.
  56. Throwable dummyException=new Throwable();
  57. StackTraceElement locations[]=dummyException.getStackTrace();
  58. // Caller will be the third element
  59. String cname="unknown";
  60. String method="unknown";
  61. if( locations!=null && locations.length >2 ) {
  62. StackTraceElement caller=locations[2];
  63. cname=caller.getClassName();
  64. method=caller.getMethodName();
  65. }
  66. if( ex==null ) {
  67. logger.logp( level, cname, method, msg );
  68. } else {
  69. logger.logp( level, cname, method, msg, ex );
  70. }
  71. }
  72. }
  73. /**
  74. * Log a message with debug log level.
  75. */
  76. public void debug(Object message) {
  77. log(Level.FINE, String.valueOf(message), null);
  78. }
  79. /**
  80. * Log a message and exception with debug log level.
  81. */
  82. public void debug(Object message, Throwable exception) {
  83. log(Level.FINE, String.valueOf(message), exception);
  84. }
  85. /**
  86. * Log a message with error log level.
  87. */
  88. public void error(Object message) {
  89. log(Level.SEVERE, String.valueOf(message), null);
  90. }
  91. /**
  92. * Log a message and exception with error log level.
  93. */
  94. public void error(Object message, Throwable exception) {
  95. log(Level.SEVERE, String.valueOf(message), exception);
  96. }
  97. /**
  98. * Log a message with fatal log level.
  99. */
  100. public void fatal(Object message) {
  101. log(Level.SEVERE, String.valueOf(message), null);
  102. }
  103. /**
  104. * Log a message and exception with fatal log level.
  105. */
  106. public void fatal(Object message, Throwable exception) {
  107. log(Level.SEVERE, String.valueOf(message), exception);
  108. }
  109. /**
  110. * Return the native Logger instance we are using.
  111. */
  112. public Logger getLogger() {
  113. if (logger == null) {
  114. logger = Logger.getLogger(name);
  115. }
  116. return (logger);
  117. }
  118. /**
  119. * Log a message with info log level.
  120. */
  121. public void info(Object message) {
  122. log(Level.INFO, String.valueOf(message), null);
  123. }
  124. /**
  125. * Log a message and exception with info log level.
  126. */
  127. public void info(Object message, Throwable exception) {
  128. log(Level.INFO, String.valueOf(message), exception);
  129. }
  130. /**
  131. * Is debug logging currently enabled?
  132. */
  133. public boolean isDebugEnabled() {
  134. return (getLogger().isLoggable(Level.FINE));
  135. }
  136. /**
  137. * Is error logging currently enabled?
  138. */
  139. public boolean isErrorEnabled() {
  140. return (getLogger().isLoggable(Level.SEVERE));
  141. }
  142. /**
  143. * Is fatal logging currently enabled?
  144. */
  145. public boolean isFatalEnabled() {
  146. return (getLogger().isLoggable(Level.SEVERE));
  147. }
  148. /**
  149. * Is info logging currently enabled?
  150. */
  151. public boolean isInfoEnabled() {
  152. return (getLogger().isLoggable(Level.INFO));
  153. }
  154. /**
  155. * Is trace logging currently enabled?
  156. */
  157. public boolean isTraceEnabled() {
  158. return (getLogger().isLoggable(Level.FINEST));
  159. }
  160. /**
  161. * Is warn logging currently enabled?
  162. */
  163. public boolean isWarnEnabled() {
  164. return (getLogger().isLoggable(Level.WARNING));
  165. }
  166. /**
  167. * Log a message with trace log level.
  168. */
  169. public void trace(Object message) {
  170. log(Level.FINEST, String.valueOf(message), null);
  171. }
  172. /**
  173. * Log a message and exception with trace log level.
  174. */
  175. public void trace(Object message, Throwable exception) {
  176. log(Level.FINEST, String.valueOf(message), exception);
  177. }
  178. /**
  179. * Log a message with warn log level.
  180. */
  181. public void warn(Object message) {
  182. log(Level.WARNING, String.valueOf(message), null);
  183. }
  184. /**
  185. * Log a message and exception with warn log level.
  186. */
  187. public void warn(Object message, Throwable exception) {
  188. log(Level.WARNING, String.valueOf(message), exception);
  189. }
  190. }