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 java.util.logging.LogRecord;
  21. import java.util.StringTokenizer;
  22. import java.io.PrintWriter;
  23. import java.io.StringWriter;
  24. import org.apache.commons.logging.Log;
  25. /**
  26. * <p>Implementation of the <code>org.apache.commons.logging.Log</code>
  27. * interface that wraps the standard JDK logging mechanisms that are
  28. * available in SourceForge's Lumberjack for JDKs prior to 1.4.</p>
  29. *
  30. * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
  31. * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  32. * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
  33. * @author <a href="mailto:vince256@comcast.net">Vince Eagen</a>
  34. * @version $Revision: 1.6 $ $Date: 2004/06/06 21:13:43 $
  35. */
  36. public class Jdk13LumberjackLogger implements Log, Serializable {
  37. // ----------------------------------------------------- Instance Variables
  38. /**
  39. * The underlying Logger implementation we are using.
  40. */
  41. protected transient Logger logger = null;
  42. protected String name = null;
  43. private String sourceClassName = "unknown";
  44. private String sourceMethodName = "unknown";
  45. private boolean classAndMethodFound = false;
  46. // ----------------------------------------------------------- Constructors
  47. /**
  48. * Construct a named instance of this Logger.
  49. *
  50. * @param name Name of the logger to be constructed
  51. */
  52. public Jdk13LumberjackLogger(String name) {
  53. this.name = name;
  54. logger = getLogger();
  55. }
  56. // --------------------------------------------------------- Public Methods
  57. private void log( Level level, String msg, Throwable ex ) {
  58. if( getLogger().isLoggable(level) ) {
  59. LogRecord record = new LogRecord(level, msg);
  60. if( !classAndMethodFound ) {
  61. getClassAndMethod();
  62. }
  63. record.setSourceClassName(sourceClassName);
  64. record.setSourceMethodName(sourceMethodName);
  65. if( ex != null ) {
  66. record.setThrown(ex);
  67. }
  68. getLogger().log(record);
  69. }
  70. }
  71. /**
  72. * <p>Gets the class and method by looking at the stack trace for the
  73. * first entry that is not this class.</p>
  74. */
  75. private void getClassAndMethod() {
  76. try {
  77. Throwable throwable = new Throwable();
  78. throwable.fillInStackTrace();
  79. StringWriter stringWriter = new StringWriter();
  80. PrintWriter printWriter = new PrintWriter( stringWriter );
  81. throwable.printStackTrace( printWriter );
  82. String traceString = stringWriter.getBuffer().toString();
  83. StringTokenizer tokenizer =
  84. new StringTokenizer( traceString, "\n" );
  85. tokenizer.nextToken();
  86. String line = tokenizer.nextToken();
  87. while ( line.indexOf( this.getClass().getName() ) == -1 ) {
  88. line = tokenizer.nextToken();
  89. }
  90. while ( line.indexOf( this.getClass().getName() ) >= 0 ) {
  91. line = tokenizer.nextToken();
  92. }
  93. int start = line.indexOf( "at " ) + 3;
  94. int end = line.indexOf( '(' );
  95. String temp = line.substring( start, end );
  96. int lastPeriod = temp.lastIndexOf( '.' );
  97. sourceClassName = temp.substring( 0, lastPeriod );
  98. sourceMethodName = temp.substring( lastPeriod + 1 );
  99. } catch ( Exception ex ) {
  100. // ignore - leave class and methodname unknown
  101. }
  102. classAndMethodFound = true;
  103. }
  104. /**
  105. * Log a message with debug log level.
  106. */
  107. public void debug(Object message) {
  108. log(Level.FINE, String.valueOf(message), null);
  109. }
  110. /**
  111. * Log a message and exception with debug log level.
  112. */
  113. public void debug(Object message, Throwable exception) {
  114. log(Level.FINE, String.valueOf(message), exception);
  115. }
  116. /**
  117. * Log a message with error log level.
  118. */
  119. public void error(Object message) {
  120. log(Level.SEVERE, String.valueOf(message), null);
  121. }
  122. /**
  123. * Log a message and exception with error log level.
  124. */
  125. public void error(Object message, Throwable exception) {
  126. log(Level.SEVERE, String.valueOf(message), exception);
  127. }
  128. /**
  129. * Log a message with fatal log level.
  130. */
  131. public void fatal(Object message) {
  132. log(Level.SEVERE, String.valueOf(message), null);
  133. }
  134. /**
  135. * Log a message and exception with fatal log level.
  136. */
  137. public void fatal(Object message, Throwable exception) {
  138. log(Level.SEVERE, String.valueOf(message), exception);
  139. }
  140. /**
  141. * Return the native Logger instance we are using.
  142. */
  143. public Logger getLogger() {
  144. if (logger == null) {
  145. logger = Logger.getLogger(name);
  146. }
  147. return (logger);
  148. }
  149. /**
  150. * Log a message with info log level.
  151. */
  152. public void info(Object message) {
  153. log(Level.INFO, String.valueOf(message), null);
  154. }
  155. /**
  156. * Log a message and exception with info log level.
  157. */
  158. public void info(Object message, Throwable exception) {
  159. log(Level.INFO, String.valueOf(message), exception);
  160. }
  161. /**
  162. * Is debug logging currently enabled?
  163. */
  164. public boolean isDebugEnabled() {
  165. return (getLogger().isLoggable(Level.FINE));
  166. }
  167. /**
  168. * Is error logging currently enabled?
  169. */
  170. public boolean isErrorEnabled() {
  171. return (getLogger().isLoggable(Level.SEVERE));
  172. }
  173. /**
  174. * Is fatal logging currently enabled?
  175. */
  176. public boolean isFatalEnabled() {
  177. return (getLogger().isLoggable(Level.SEVERE));
  178. }
  179. /**
  180. * Is info logging currently enabled?
  181. */
  182. public boolean isInfoEnabled() {
  183. return (getLogger().isLoggable(Level.INFO));
  184. }
  185. /**
  186. * Is trace logging currently enabled?
  187. */
  188. public boolean isTraceEnabled() {
  189. return (getLogger().isLoggable(Level.FINEST));
  190. }
  191. /**
  192. * Is warn logging currently enabled?
  193. */
  194. public boolean isWarnEnabled() {
  195. return (getLogger().isLoggable(Level.WARNING));
  196. }
  197. /**
  198. * Log a message with trace log level.
  199. */
  200. public void trace(Object message) {
  201. log(Level.FINEST, String.valueOf(message), null);
  202. }
  203. /**
  204. * Log a message and exception with trace log level.
  205. */
  206. public void trace(Object message, Throwable exception) {
  207. log(Level.FINEST, String.valueOf(message), exception);
  208. }
  209. /**
  210. * Log a message with warn log level.
  211. */
  212. public void warn(Object message) {
  213. log(Level.WARNING, String.valueOf(message), null);
  214. }
  215. /**
  216. * Log a message and exception with warn log level.
  217. */
  218. public void warn(Object message, Throwable exception) {
  219. log(Level.WARNING, String.valueOf(message), exception);
  220. }
  221. }