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 org.apache.log.Logger;
  19. import org.apache.log.Hierarchy;
  20. import org.apache.commons.logging.Log;
  21. /**
  22. * <p>Implementation of <code>org.apache.commons.logging.Log</code>
  23. * that wraps the <a href="http://avalon.apache.org/logkit/">avalon-logkit</a>
  24. * logging system. Configuration of <code>LogKit</code> is left to the user.
  25. * </p>
  26. *
  27. * <p><code>LogKit</code> accepts only <code>String</code> messages.
  28. * Therefore, this implementation converts object messages into strings
  29. * by called their <code>toString()</code> method before logging them.</p>
  30. *
  31. * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
  32. * @author Robert Burrell Donkin
  33. * @version $Id: LogKitLogger.java,v 1.9 2004/06/01 19:56:46 rdonkin Exp $
  34. */
  35. public class LogKitLogger implements Log, Serializable {
  36. // ------------------------------------------------------------- Attributes
  37. /** Logging goes to this <code>LogKit</code> logger */
  38. protected transient Logger logger = null;
  39. /** Name of this logger */
  40. protected String name = null;
  41. // ------------------------------------------------------------ Constructor
  42. /**
  43. * Construct <code>LogKitLogger</code> which wraps the <code>LogKit</code>
  44. * logger with given name.
  45. *
  46. * @param name log name
  47. */
  48. public LogKitLogger(String name) {
  49. this.name = name;
  50. this.logger = getLogger();
  51. }
  52. // --------------------------------------------------------- Public Methods
  53. /**
  54. * <p>Return the underlying Logger we are using.</p>
  55. */
  56. public Logger getLogger() {
  57. if (logger == null) {
  58. logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
  59. }
  60. return (logger);
  61. }
  62. // ----------------------------------------------------- Log Implementation
  63. /**
  64. * Log message to <code>LogKit</code> logger with <code>DEBUG</code> priority.
  65. */
  66. public void trace(Object message) {
  67. debug(message);
  68. }
  69. /**
  70. * Log error to <code>LogKit</code> logger with <code>DEBUG</code> priority.
  71. */
  72. public void trace(Object message, Throwable t) {
  73. debug(message, t);
  74. }
  75. /**
  76. * Log message to <code>LogKit</code> logger with <code>DEBUG</code> priority.
  77. */
  78. public void debug(Object message) {
  79. if (message != null) {
  80. getLogger().debug(String.valueOf(message));
  81. }
  82. }
  83. /**
  84. * Log error to <code>LogKit</code> logger with <code>DEBUG</code> priority.
  85. */
  86. public void debug(Object message, Throwable t) {
  87. if (message != null) {
  88. getLogger().debug(String.valueOf(message), t);
  89. }
  90. }
  91. /**
  92. * Log message to <code>LogKit</code> logger with <code>INFO</code> priority.
  93. */
  94. public void info(Object message) {
  95. if (message != null) {
  96. getLogger().info(String.valueOf(message));
  97. }
  98. }
  99. /**
  100. * Log error to <code>LogKit</code> logger with <code>INFO</code> priority.
  101. */
  102. public void info(Object message, Throwable t) {
  103. if (message != null) {
  104. getLogger().info(String.valueOf(message), t);
  105. }
  106. }
  107. /**
  108. * Log message to <code>LogKit</code> logger with <code>WARN</code> priority.
  109. */
  110. public void warn(Object message) {
  111. if (message != null) {
  112. getLogger().warn(String.valueOf(message));
  113. }
  114. }
  115. /**
  116. * Log error to <code>LogKit</code> logger with <code>WARN</code> priority.
  117. */
  118. public void warn(Object message, Throwable t) {
  119. if (message != null) {
  120. getLogger().warn(String.valueOf(message), t);
  121. }
  122. }
  123. /**
  124. * Log message to <code>LogKit</code> logger with <code>ERROR</code> priority.
  125. */
  126. public void error(Object message) {
  127. if (message != null) {
  128. getLogger().error(String.valueOf(message));
  129. }
  130. }
  131. /**
  132. * Log error to <code>LogKit</code> logger with <code>ERROR</code> priority.
  133. */
  134. public void error(Object message, Throwable t) {
  135. if (message != null) {
  136. getLogger().error(String.valueOf(message), t);
  137. }
  138. }
  139. /**
  140. * Log message to <code>LogKit</code> logger with <code>FATAL_ERROR</code> priority.
  141. */
  142. public void fatal(Object message) {
  143. if (message != null) {
  144. getLogger().fatalError(String.valueOf(message));
  145. }
  146. }
  147. /**
  148. * Log error to <code>LogKit</code> logger with <code>FATAL_ERROR</code> priority.
  149. */
  150. public void fatal(Object message, Throwable t) {
  151. if (message != null) {
  152. getLogger().fatalError(String.valueOf(message), t);
  153. }
  154. }
  155. /**
  156. * Check whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
  157. */
  158. public boolean isDebugEnabled() {
  159. return getLogger().isDebugEnabled();
  160. }
  161. /**
  162. * Check whether the <code>LogKit</code> logger will log messages of priority <code>ERROR</code>.
  163. */
  164. public boolean isErrorEnabled() {
  165. return getLogger().isErrorEnabled();
  166. }
  167. /**
  168. * Check whether the <code>LogKit</code> logger will log messages of priority <code>FATAL_ERROR</code>.
  169. */
  170. public boolean isFatalEnabled() {
  171. return getLogger().isFatalErrorEnabled();
  172. }
  173. /**
  174. * Check whether the <code>LogKit</code> logger will log messages of priority <code>INFO</code>.
  175. */
  176. public boolean isInfoEnabled() {
  177. return getLogger().isInfoEnabled();
  178. }
  179. /**
  180. * Check whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
  181. */
  182. public boolean isTraceEnabled() {
  183. return getLogger().isDebugEnabled();
  184. }
  185. /**
  186. * Check whether the <code>LogKit</code> logger will log messages of priority <code>WARN</code>.
  187. */
  188. public boolean isWarnEnabled() {
  189. return getLogger().isWarnEnabled();
  190. }
  191. }