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.commons.logging.Log;
  19. import org.apache.log4j.Logger;
  20. import org.apache.log4j.Priority;
  21. import org.apache.log4j.Level;
  22. /**
  23. * <p>Implementation of {@link Log} that maps directly to a Log4J
  24. * <strong>Logger</strong>. Initial configuration of the corresponding
  25. * Logger instances should be done in the usual manner, as outlined in
  26. * the Log4J documentation.</p>
  27. *
  28. * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
  29. * @author Rod Waldhoff
  30. * @author Robert Burrell Donkin
  31. * @version $Id: Log4JLogger.java,v 1.11 2004/05/19 21:01:23 rdonkin Exp $
  32. */
  33. public class Log4JLogger implements Log, Serializable {
  34. // ------------------------------------------------------------- Attributes
  35. /** The fully qualified name of the Log4JLogger class. */
  36. private static final String FQCN = Log4JLogger.class.getName();
  37. private static final boolean is12 = Priority.class.isAssignableFrom(Level.class);
  38. /** Log to this logger */
  39. private transient Logger logger = null;
  40. /** Logger name */
  41. private String name = null;
  42. // ------------------------------------------------------------ Constructor
  43. public Log4JLogger() {
  44. }
  45. /**
  46. * Base constructor.
  47. */
  48. public Log4JLogger(String name) {
  49. this.name = name;
  50. this.logger = getLogger();
  51. }
  52. /** For use with a log4j factory.
  53. */
  54. public Log4JLogger(Logger logger ) {
  55. this.name = logger.getName();
  56. this.logger=logger;
  57. }
  58. // --------------------------------------------------------- Implementation
  59. /**
  60. * Log a message to the Log4j Logger with <code>TRACE</code> priority.
  61. * Currently logs to <code>DEBUG</code> level in Log4J.
  62. */
  63. public void trace(Object message) {
  64. if(is12) {
  65. getLogger().log(FQCN, (Priority) Level.DEBUG, message, null );
  66. } else {
  67. getLogger().log(FQCN, Level.DEBUG, message, null );
  68. }
  69. }
  70. /**
  71. * Log an error to the Log4j Logger with <code>TRACE</code> priority.
  72. * Currently logs to <code>DEBUG</code> level in Log4J.
  73. */
  74. public void trace(Object message, Throwable t) {
  75. if(is12) {
  76. getLogger().log(FQCN, (Priority) Level.DEBUG, message, t );
  77. } else {
  78. getLogger().log(FQCN, Level.DEBUG, message, t );
  79. }
  80. }
  81. /**
  82. * Log a message to the Log4j Logger with <code>DEBUG</code> priority.
  83. */
  84. public void debug(Object message) {
  85. if(is12) {
  86. getLogger().log(FQCN, (Priority) Level.DEBUG, message, null );
  87. } else {
  88. getLogger().log(FQCN, Level.DEBUG, message, null );
  89. }
  90. }
  91. /**
  92. * Log an error to the Log4j Logger with <code>DEBUG</code> priority.
  93. */
  94. public void debug(Object message, Throwable t) {
  95. if(is12) {
  96. getLogger().log(FQCN, (Priority) Level.DEBUG, message, t );
  97. } else {
  98. getLogger().log(FQCN, Level.DEBUG, message, t );
  99. }
  100. }
  101. /**
  102. * Log a message to the Log4j Logger with <code>INFO</code> priority.
  103. */
  104. public void info(Object message) {
  105. if(is12) {
  106. getLogger().log(FQCN, (Priority) Level.INFO, message, null );
  107. } else {
  108. getLogger().log(FQCN, Level.INFO, message, null );
  109. }
  110. }
  111. /**
  112. * Log an error to the Log4j Logger with <code>INFO</code> priority.
  113. */
  114. public void info(Object message, Throwable t) {
  115. if(is12) {
  116. getLogger().log(FQCN, (Priority) Level.INFO, message, t );
  117. } else {
  118. getLogger().log(FQCN, Level.INFO, message, t );
  119. }
  120. }
  121. /**
  122. * Log a message to the Log4j Logger with <code>WARN</code> priority.
  123. */
  124. public void warn(Object message) {
  125. if(is12) {
  126. getLogger().log(FQCN, (Priority) Level.WARN, message, null );
  127. } else {
  128. getLogger().log(FQCN, Level.WARN, message, null );
  129. }
  130. }
  131. /**
  132. * Log an error to the Log4j Logger with <code>WARN</code> priority.
  133. */
  134. public void warn(Object message, Throwable t) {
  135. if(is12) {
  136. getLogger().log(FQCN, (Priority) Level.WARN, message, t );
  137. } else {
  138. getLogger().log(FQCN, Level.WARN, message, t );
  139. }
  140. }
  141. /**
  142. * Log a message to the Log4j Logger with <code>ERROR</code> priority.
  143. */
  144. public void error(Object message) {
  145. if(is12) {
  146. getLogger().log(FQCN, (Priority) Level.ERROR, message, null );
  147. } else {
  148. getLogger().log(FQCN, Level.ERROR, message, null );
  149. }
  150. }
  151. /**
  152. * Log an error to the Log4j Logger with <code>ERROR</code> priority.
  153. */
  154. public void error(Object message, Throwable t) {
  155. if(is12) {
  156. getLogger().log(FQCN, (Priority) Level.ERROR, message, t );
  157. } else {
  158. getLogger().log(FQCN, Level.ERROR, message, t );
  159. }
  160. }
  161. /**
  162. * Log a message to the Log4j Logger with <code>FATAL</code> priority.
  163. */
  164. public void fatal(Object message) {
  165. if(is12) {
  166. getLogger().log(FQCN, (Priority) Level.FATAL, message, null );
  167. } else {
  168. getLogger().log(FQCN, Level.FATAL, message, null );
  169. }
  170. }
  171. /**
  172. * Log an error to the Log4j Logger with <code>FATAL</code> priority.
  173. */
  174. public void fatal(Object message, Throwable t) {
  175. if(is12) {
  176. getLogger().log(FQCN, (Priority) Level.FATAL, message, t );
  177. } else {
  178. getLogger().log(FQCN, Level.FATAL, message, t );
  179. }
  180. }
  181. /**
  182. * Return the native Logger instance we are using.
  183. */
  184. public Logger getLogger() {
  185. if (logger == null) {
  186. logger = Logger.getLogger(name);
  187. }
  188. return (this.logger);
  189. }
  190. /**
  191. * Check whether the Log4j Logger used is enabled for <code>DEBUG</code> priority.
  192. */
  193. public boolean isDebugEnabled() {
  194. return getLogger().isDebugEnabled();
  195. }
  196. /**
  197. * Check whether the Log4j Logger used is enabled for <code>ERROR</code> priority.
  198. */
  199. public boolean isErrorEnabled() {
  200. if(is12) {
  201. return getLogger().isEnabledFor((Priority) Level.ERROR);
  202. } else {
  203. return getLogger().isEnabledFor(Level.ERROR);
  204. }
  205. }
  206. /**
  207. * Check whether the Log4j Logger used is enabled for <code>FATAL</code> priority.
  208. */
  209. public boolean isFatalEnabled() {
  210. if(is12) {
  211. return getLogger().isEnabledFor((Priority) Level.FATAL);
  212. } else {
  213. return getLogger().isEnabledFor(Level.FATAL);
  214. }
  215. }
  216. /**
  217. * Check whether the Log4j Logger used is enabled for <code>INFO</code> priority.
  218. */
  219. public boolean isInfoEnabled() {
  220. return getLogger().isInfoEnabled();
  221. }
  222. /**
  223. * Check whether the Log4j Logger used is enabled for <code>TRACE</code> priority.
  224. * For Log4J, this returns the value of <code>isDebugEnabled()</code>
  225. */
  226. public boolean isTraceEnabled() {
  227. return getLogger().isDebugEnabled();
  228. }
  229. /**
  230. * Check whether the Log4j Logger used is enabled for <code>WARN</code> priority.
  231. */
  232. public boolean isWarnEnabled() {
  233. if(is12) {
  234. return getLogger().isEnabledFor((Priority) Level.WARN);
  235. } else {
  236. return getLogger().isEnabledFor(Level.WARN);
  237. }
  238. }
  239. }