Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SLF4JLogger.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. /**
  19. * An implementation of the {@link POILogger} using the
  20. * SLF4J 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 SLF4JLogger implements POILogger
  25. {
  26. private Logger log;
  27. @Override
  28. public void initialize(final String cat) {
  29. this.log = LoggerFactory.getLogger(cat);
  30. }
  31. /**
  32. * Log a message
  33. *
  34. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  35. * @param obj1 The object to log.
  36. */
  37. @Override
  38. public void _log(final int level, final Object obj1) {
  39. switch (level) {
  40. case FATAL:
  41. case ERROR:
  42. if (log.isErrorEnabled()) {
  43. log.error(obj1.toString());
  44. }
  45. break;
  46. case WARN:
  47. if (log.isWarnEnabled()) {
  48. log.warn(obj1.toString());
  49. }
  50. break;
  51. case INFO:
  52. if (log.isInfoEnabled()) {
  53. log.info(obj1.toString());
  54. }
  55. break;
  56. case DEBUG:
  57. if (log.isDebugEnabled()) {
  58. log.debug(obj1.toString());
  59. }
  60. break;
  61. default:
  62. if (log.isTraceEnabled()) {
  63. log.trace(obj1.toString());
  64. }
  65. break;
  66. }
  67. }
  68. /**
  69. * Log a message
  70. *
  71. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  72. * @param obj1 The object to log. This is converted to a string.
  73. * @param exception An exception to be logged
  74. */
  75. @Override
  76. public void _log(final int level, final Object obj1, final Throwable exception) {
  77. switch (level) {
  78. case FATAL:
  79. case ERROR:
  80. if (log.isErrorEnabled()) {
  81. if (obj1 != null) {
  82. log.error(obj1.toString(), exception);
  83. } else {
  84. log.error(exception.toString(), exception);
  85. }
  86. }
  87. break;
  88. case WARN:
  89. if (log.isWarnEnabled()) {
  90. if (obj1 != null) {
  91. log.warn(obj1.toString(), exception);
  92. } else {
  93. log.warn(exception.toString(), exception);
  94. }
  95. }
  96. break;
  97. case INFO:
  98. if (log.isInfoEnabled()) {
  99. if (obj1 != null) {
  100. log.info(obj1.toString(), exception);
  101. } else {
  102. log.info(exception.toString(), exception);
  103. }
  104. }
  105. break;
  106. case DEBUG:
  107. if (log.isDebugEnabled()) {
  108. if (obj1 != null) {
  109. log.debug(obj1.toString(), exception);
  110. } else {
  111. log.debug(exception.toString(), exception);
  112. }
  113. }
  114. break;
  115. default:
  116. if (log.isTraceEnabled()) {
  117. if (obj1 != null) {
  118. log.trace(obj1.toString(), exception);
  119. } else {
  120. log.trace(exception.toString(), exception);
  121. }
  122. }
  123. break;
  124. }
  125. }
  126. /**
  127. * Check if a logger is enabled to log at the specified level
  128. *
  129. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  130. */
  131. @Override
  132. public boolean check(final int level)
  133. {
  134. switch (level) {
  135. case FATAL:
  136. case ERROR:
  137. return log.isErrorEnabled();
  138. case WARN:
  139. return log.isWarnEnabled();
  140. case INFO:
  141. return log.isInfoEnabled();
  142. case DEBUG:
  143. return log.isDebugEnabled();
  144. default:
  145. return false;
  146. }
  147. }
  148. }