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.

POILogger.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  17. * A logger interface that strives to make it as easy as possible for
  18. * developers to write log calls, while simultaneously making those
  19. * calls as cheap as possible by performing lazy evaluation of the log
  20. * message.
  21. *
  22. * A logger can be selected via system properties, e.g.
  23. * <code>
  24. * -Dorg.apache.poi.util.POILogger=org.apache.poi.util.SystemOutLogger
  25. * </code>
  26. *
  27. * The following Logger-implementations are provided:
  28. *
  29. * <ul>
  30. * <li>NullLogger</li>
  31. * <li>CommonsLogger</li>
  32. * <li>SystemOutLogger</li>
  33. * </ul>
  34. */
  35. @Internal
  36. public interface POILogger {
  37. int DEBUG = 1;
  38. int INFO = 3;
  39. int WARN = 5;
  40. int ERROR = 7;
  41. int FATAL = 9;
  42. /**
  43. * Initialize the Logger - belongs to the SPI, called from the POILogFactory
  44. * @param cat the String that defines the log
  45. */
  46. void initialize(String cat);
  47. /**
  48. * Log a message - belongs to the SPI, usually not called from user code
  49. *
  50. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  51. * @param obj1 The object to log. This is converted to a string.
  52. */
  53. @Internal
  54. void _log(int level, Object obj1);
  55. /**
  56. * Log a message - belongs to the SPI, usually not called from user code
  57. *
  58. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  59. * @param obj1 The object to log. This is converted to a string.
  60. * @param exception An exception to be logged
  61. */
  62. @Internal
  63. void _log(int level, Object obj1, final Throwable exception);
  64. /**
  65. * Check if a logger is enabled to log at the specified level
  66. * This allows code to avoid building strings or evaluating functions in
  67. * the arguments to log.
  68. *
  69. * An example:
  70. * <code><pre>
  71. * if (logger.check(POILogger.INFO)) {
  72. * logger.log(POILogger.INFO, "Avoid concatenating ", " strings and evaluating ", functions());
  73. * }
  74. * </pre></code>
  75. *
  76. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  77. */
  78. boolean check(int level);
  79. /**
  80. * Log a message. Lazily appends Object parameters together.
  81. * If the last parameter is a {@link Throwable} it is logged specially.
  82. *
  83. * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
  84. * @param objs the objects to place in the message
  85. */
  86. default void log(int level, Object... objs) {
  87. if (!check(level)) return;
  88. Throwable lastEx = null;
  89. String msg;
  90. if (objs.length == 0) {
  91. msg = "";
  92. } else if (objs.length == 1) {
  93. if (objs[0] instanceof Throwable) {
  94. lastEx = (Throwable)objs[0];
  95. }
  96. msg = objs[0].toString();
  97. } else {
  98. StringBuilder sb = new StringBuilder(32);
  99. for (int i=0; i<objs.length; i++) {
  100. if (i == objs.length-1 && objs[i] instanceof Throwable) {
  101. lastEx = (Throwable)objs[i];
  102. } else {
  103. sb.append(objs[i]);
  104. }
  105. }
  106. msg = sb.toString();
  107. }
  108. // log forging escape
  109. msg = msg.replaceAll("[\r\n]+", " ");
  110. if (lastEx == null) {
  111. _log(level, msg);
  112. } else {
  113. _log(level, msg, lastEx);
  114. }
  115. }
  116. }