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.

POILogFactory.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 java.util.HashMap;
  17. import java.util.Map;
  18. /**
  19. * Provides logging without clients having to mess with
  20. * configuration/initialization.
  21. *
  22. * @author Andrew C. Oliver (acoliver at apache dot org)
  23. * @author Marc Johnson (mjohnson at apache dot org)
  24. * @author Nicola Ken Barozzi (nicolaken at apache.org)
  25. */
  26. @Internal
  27. public final class POILogFactory {
  28. /**
  29. * Map of POILogger instances, with classes as keys
  30. */
  31. private static final Map<String,POILogger> _loggers = new HashMap<>();
  32. /**
  33. * A common instance of NullLogger, as it does nothing
  34. * we only need the one
  35. */
  36. private static final POILogger _nullLogger = new NullLogger();
  37. /**
  38. * The name of the class to use. Initialised the
  39. * first time we need it
  40. */
  41. static String _loggerClassName;
  42. /**
  43. * Construct a POILogFactory.
  44. */
  45. private POILogFactory() {}
  46. /**
  47. * Get a logger, based on a class name
  48. *
  49. * @param theclass the class whose name defines the log
  50. *
  51. * @return a POILogger for the specified class
  52. */
  53. public static POILogger getLogger(final Class<?> theclass) {
  54. return getLogger(theclass.getName());
  55. }
  56. /**
  57. * Get a logger, based on a String
  58. *
  59. * @param cat the String that defines the log
  60. *
  61. * @return a POILogger for the specified class
  62. */
  63. public static POILogger getLogger(final String cat) {
  64. // If we haven't found out what logger to use yet,
  65. // then do so now
  66. // Don't look it up until we're first asked, so
  67. // that our users can set the system property
  68. // between class loading and first use
  69. if(_loggerClassName == null) {
  70. try {
  71. _loggerClassName = System.getProperty("org.apache.poi.util.POILogger");
  72. } catch(Exception e) {
  73. // ignore any exception here
  74. }
  75. // Use the default logger if none specified,
  76. // or none could be fetched
  77. if(_loggerClassName == null) {
  78. _loggerClassName = _nullLogger.getClass().getName();
  79. }
  80. }
  81. // Short circuit for the null logger, which
  82. // ignores all categories
  83. if(_loggerClassName.equals(_nullLogger.getClass().getName())) {
  84. return _nullLogger;
  85. }
  86. // Fetch the right logger for them, creating
  87. // it if that's required
  88. POILogger logger = _loggers.get(cat);
  89. if (logger == null) {
  90. try {
  91. @SuppressWarnings("unchecked")
  92. Class<? extends POILogger> loggerClass =
  93. (Class<? extends POILogger>) Class.forName(_loggerClassName);
  94. logger = loggerClass.getConstructor().newInstance();
  95. logger.initialize(cat);
  96. } catch(Exception e) {
  97. // Give up and use the null logger
  98. logger = _nullLogger;
  99. _loggerClassName = _nullLogger.getClass().getName();
  100. }
  101. // Save for next time
  102. _loggers.put(cat, logger);
  103. }
  104. return logger;
  105. }
  106. }