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.avalon.framework.logger.Logger;
  19. import org.apache.commons.logging.Log;
  20. /**
  21. * <p>Implementation of commons-logging Log interface that delegates all
  22. * logging calls to the Avalon logging abstraction: the Logger interface.
  23. * </p>
  24. * <p>
  25. * There are two ways in which this class can be used:
  26. * </p>
  27. * <ul>
  28. * <li>the instance can be constructed with an Avalon logger
  29. * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts
  30. * as a simple thin wrapping implementation over the logger. This is
  31. * particularly useful when using a property setter.
  32. * </li>
  33. * <li>the {@link #setDefaultLogger} class property can be called which
  34. * sets the ancesteral Avalon logger for this class. Any <code>AvalonLogger</code>
  35. * instances created through the <code>LogFactory</code> mechanisms will output
  36. * to child loggers of this <code>Logger</code>.
  37. * </li>
  38. * </ul>
  39. *
  40. * @author <a href="mailto:neeme@apache.org">Neeme Praks</a>
  41. * @version $Revision: 1.9 $ $Date: 2004/06/01 19:56:20 $
  42. */
  43. public class AvalonLogger implements Log, Serializable {
  44. /** Ancesteral avalon logger */
  45. private static Logger defaultLogger = null;
  46. /** Avalon logger used to perform log */
  47. private transient Logger logger = null;
  48. /** The name of this logger */
  49. private String name = null;
  50. /**
  51. * Constructs an <code>AvalonLogger</code> that outputs to the given
  52. * <code>Logger</code> instance.
  53. * @param logger the avalon logger implementation to delegate to
  54. */
  55. public AvalonLogger(Logger logger) {
  56. this.name = name;
  57. this.logger = logger;
  58. }
  59. /**
  60. * Constructs an <code>AvalonLogger</code> that will log to a child
  61. * of the <code>Logger</code> set by calling {@link #setDefaultLogger}.
  62. * @param name the name of the avalon logger implementation to delegate to
  63. */
  64. public AvalonLogger(String name) {
  65. if (defaultLogger == null)
  66. throw new NullPointerException("default logger has to be specified if this constructor is used!");
  67. this.logger = getLogger();
  68. }
  69. /**
  70. * Gets the Avalon logger implementation used to perform logging.
  71. * @return avalon logger implementation
  72. */
  73. public Logger getLogger() {
  74. if (logger == null) {
  75. logger = defaultLogger.getChildLogger(name);
  76. }
  77. return logger;
  78. }
  79. /**
  80. * Sets the ancesteral Avalon logger from which the delegating loggers
  81. * will descend.
  82. * @param logger the default avalon logger,
  83. * in case there is no logger instance supplied in constructor
  84. */
  85. public static void setDefaultLogger(Logger logger) {
  86. defaultLogger = logger;
  87. }
  88. /**
  89. * @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable)
  90. */
  91. public void debug(Object o, Throwable t) {
  92. if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(o), t);
  93. }
  94. /**
  95. * @see org.apache.commons.logging.Log#debug(java.lang.Object)
  96. */
  97. public void debug(Object o) {
  98. if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(o));
  99. }
  100. /**
  101. * @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable)
  102. */
  103. public void error(Object o, Throwable t) {
  104. if (getLogger().isErrorEnabled()) getLogger().error(String.valueOf(o), t);
  105. }
  106. /**
  107. * @see org.apache.commons.logging.Log#error(java.lang.Object)
  108. */
  109. public void error(Object o) {
  110. if (getLogger().isErrorEnabled()) getLogger().error(String.valueOf(o));
  111. }
  112. /**
  113. * @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable)
  114. */
  115. public void fatal(Object o, Throwable t) {
  116. if (getLogger().isFatalErrorEnabled()) getLogger().fatalError(String.valueOf(o), t);
  117. }
  118. /**
  119. * @see org.apache.commons.logging.Log#fatal(java.lang.Object)
  120. */
  121. public void fatal(Object o) {
  122. if (getLogger().isFatalErrorEnabled()) getLogger().fatalError(String.valueOf(o));
  123. }
  124. /**
  125. * @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable)
  126. */
  127. public void info(Object o, Throwable t) {
  128. if (getLogger().isInfoEnabled()) getLogger().info(String.valueOf(o), t);
  129. }
  130. /**
  131. * @see org.apache.commons.logging.Log#info(java.lang.Object)
  132. */
  133. public void info(Object o) {
  134. if (getLogger().isInfoEnabled()) getLogger().info(String.valueOf(o));
  135. }
  136. /**
  137. * @see org.apache.commons.logging.Log#isDebugEnabled()
  138. */
  139. public boolean isDebugEnabled() {
  140. return getLogger().isDebugEnabled();
  141. }
  142. /**
  143. * @see org.apache.commons.logging.Log#isErrorEnabled()
  144. */
  145. public boolean isErrorEnabled() {
  146. return getLogger().isErrorEnabled();
  147. }
  148. /**
  149. * @see org.apache.commons.logging.Log#isFatalEnabled()
  150. */
  151. public boolean isFatalEnabled() {
  152. return getLogger().isFatalErrorEnabled();
  153. }
  154. /**
  155. * @see org.apache.commons.logging.Log#isInfoEnabled()
  156. */
  157. public boolean isInfoEnabled() {
  158. return getLogger().isInfoEnabled();
  159. }
  160. /**
  161. * @see org.apache.commons.logging.Log#isTraceEnabled()
  162. */
  163. public boolean isTraceEnabled() {
  164. return getLogger().isDebugEnabled();
  165. }
  166. /**
  167. * @see org.apache.commons.logging.Log#isWarnEnabled()
  168. */
  169. public boolean isWarnEnabled() {
  170. return getLogger().isWarnEnabled();
  171. }
  172. /**
  173. * @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable)
  174. */
  175. public void trace(Object o, Throwable t) {
  176. if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(o), t);
  177. }
  178. /**
  179. * @see org.apache.commons.logging.Log#trace(java.lang.Object)
  180. */
  181. public void trace(Object o) {
  182. if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(o));
  183. }
  184. /**
  185. * @see org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable)
  186. */
  187. public void warn(Object o, Throwable t) {
  188. if (getLogger().isWarnEnabled()) getLogger().warn(String.valueOf(o), t);
  189. }
  190. /**
  191. * @see org.apache.commons.logging.Log#warn(java.lang.Object)
  192. */
  193. public void warn(Object o) {
  194. if (getLogger().isWarnEnabled()) getLogger().warn(String.valueOf(o));
  195. }
  196. }