You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommonsLogger.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import org.apache.commons.logging.Log;
  17. import org.apache.commons.logging.LogFactory;
  18. /**
  19. * An implementation of the {@link POILogger} using the
  20. * Apache Commons Logging framework. Which itself can be configured to
  21. * send log to various different log frameworks and even allows to create
  22. * a small wrapper for custom log frameworks.
  23. */
  24. public class CommonsLogger implements POILogger
  25. {
  26. private static final LogFactory _creator = LogFactory.getFactory();
  27. private Log log;
  28. @Override
  29. public void initialize(final String cat) {
  30. this.log = _creator.getInstance(cat);
  31. }
  32. /**
  33. * Log a message
  34. *
  35. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  36. * @param obj1 The object to log.
  37. */
  38. @Override
  39. public void _log(final int level, final Object obj1) {
  40. // FIXME: What happens if level is in between two levels (an even number)?
  41. // Should this be `if (level >= FATAL) ...`?
  42. switch (level) {
  43. case FATAL:
  44. if (log.isFatalEnabled()) {
  45. log.fatal(obj1);
  46. }
  47. break;
  48. case ERROR:
  49. if (log.isErrorEnabled()) {
  50. log.error(obj1);
  51. }
  52. break;
  53. case WARN:
  54. if (log.isWarnEnabled()) {
  55. log.warn(obj1);
  56. }
  57. break;
  58. case INFO:
  59. if (log.isInfoEnabled()) {
  60. log.info(obj1);
  61. }
  62. break;
  63. case DEBUG:
  64. if (log.isDebugEnabled()) {
  65. log.debug(obj1);
  66. }
  67. break;
  68. default:
  69. if (log.isTraceEnabled()) {
  70. log.trace(obj1);
  71. }
  72. break;
  73. }
  74. }
  75. /**
  76. * Log a message
  77. *
  78. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  79. * @param obj1 The object to log. This is converted to a string.
  80. * @param exception An exception to be logged
  81. */
  82. @Override
  83. public void _log(final int level, final Object obj1, final Throwable exception) {
  84. // FIXME: What happens if level is in between two levels (an even number)?
  85. // Should this be `if (level >= FATAL) ...`?
  86. switch (level) {
  87. case FATAL:
  88. if (log.isFatalEnabled()) {
  89. if (obj1 != null) {
  90. log.fatal(obj1, exception);
  91. } else {
  92. log.fatal(exception);
  93. }
  94. }
  95. break;
  96. case ERROR:
  97. if (log.isErrorEnabled()) {
  98. if (obj1 != null) {
  99. log.error(obj1, exception);
  100. } else {
  101. log.error(exception);
  102. }
  103. }
  104. break;
  105. case WARN:
  106. if (log.isWarnEnabled()) {
  107. if (obj1 != null) {
  108. log.warn(obj1, exception);
  109. } else {
  110. log.warn(exception);
  111. }
  112. }
  113. break;
  114. case INFO:
  115. if (log.isInfoEnabled()) {
  116. if (obj1 != null) {
  117. log.info(obj1, exception);
  118. } else {
  119. log.info(exception);
  120. }
  121. }
  122. break;
  123. case DEBUG:
  124. if (log.isDebugEnabled()) {
  125. if (obj1 != null) {
  126. log.debug(obj1, exception);
  127. } else {
  128. log.debug(exception);
  129. }
  130. }
  131. break;
  132. default:
  133. if (log.isTraceEnabled()) {
  134. if (obj1 != null) {
  135. log.trace(obj1, exception);
  136. } else {
  137. log.trace(exception);
  138. }
  139. }
  140. break;
  141. }
  142. }
  143. /**
  144. * Check if a logger is enabled to log at the specified level
  145. *
  146. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  147. */
  148. @Override
  149. public boolean check(final int level)
  150. {
  151. // FIXME: What happens if level is in between two levels (an even number)?
  152. // Should this be `if (level >= FATAL) ...`?
  153. switch (level) {
  154. case FATAL:
  155. return log.isFatalEnabled();
  156. case ERROR:
  157. return log.isErrorEnabled();
  158. case WARN:
  159. return log.isWarnEnabled();
  160. case INFO:
  161. return log.isInfoEnabled();
  162. case DEBUG:
  163. return log.isDebugEnabled();
  164. default:
  165. return false;
  166. }
  167. }
  168. }