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.

Logging.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc;
  23. import java.lang.reflect.InvocationTargetException;
  24. import java.lang.reflect.Method;
  25. /**
  26. * Logging using log4j if available.
  27. */
  28. public class Logging {
  29. /** Available Log Levels. */
  30. public enum LogLevel {
  31. TRACE("trace", "isTraceEnabled"),
  32. DEBUG("debug", "isDebugEnabled"),
  33. INFO("info", "isInfoEnabled"),
  34. WARN("warn", "isWarnEnabled"),
  35. ERROR("error", "isErrorEnabled"),
  36. FATAL("fatal", "isFatalEnabled");
  37. /** Method name. */
  38. private final String methodName;
  39. /** Check Method name. */
  40. private final String checkMethodName;
  41. /**
  42. * Create a new LogLevel.
  43. *
  44. * @param methodName Name of method in log4j to log to
  45. * @param checkMethodName Name of method in log4j to sue to check logging
  46. */
  47. LogLevel(final String methodName, final String checkMethodName) {
  48. this.methodName = methodName;
  49. this.checkMethodName = checkMethodName;
  50. }
  51. /**
  52. * Get the Name of method in log4j to log to.
  53. *
  54. * @return Name of method in log4j to log to
  55. */
  56. public String getMethodName() {
  57. return methodName;
  58. }
  59. /**
  60. * Get the Name of the check method in log4j.
  61. *
  62. * @return Name of check method in log4j
  63. */
  64. public String getCheckMethodName() {
  65. return checkMethodName;
  66. }
  67. }
  68. /** Singleton Instance of Logging. */
  69. private static Logging me;
  70. /** Is log4j available. */
  71. private final boolean isAvailable;
  72. /** "Log" object if available. */
  73. private Object log;
  74. /** Create a new Logging. */
  75. @SuppressWarnings({"unchecked", "rawtypes"})
  76. private Logging() {
  77. try {
  78. final Class<?> factory;
  79. // Check for classes
  80. Class.forName("org.apache.commons.logging.Log");
  81. factory = Class.forName("org.apache.commons.logging.LogFactory");
  82. if (factory != null) {
  83. final Method getLog = factory.getMethod("getLog", Class.class);
  84. log = getLog.invoke(null, this.getClass());
  85. }
  86. } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException cnfe) {
  87. }
  88. isAvailable = log != null;
  89. }
  90. /**
  91. * Get an instance of Logging.
  92. *
  93. * @return The instance of Logging
  94. */
  95. public static Logging getLogging() {
  96. synchronized (Logging.class) {
  97. if (me == null) {
  98. me = new Logging();
  99. }
  100. return me;
  101. }
  102. }
  103. /**
  104. * Check is a log level is available.
  105. *
  106. * @param level Level to check
  107. *
  108. * @return true if the method was invoked
  109. */
  110. public boolean levelEnabled(final LogLevel level) {
  111. if (isAvailable) {
  112. try {
  113. final Method check = log.getClass().getMethod(level.getCheckMethodName());
  114. return (Boolean) check.invoke(log);
  115. } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException nsme) {
  116. }
  117. }
  118. return false;
  119. }
  120. /**
  121. * Log a message if log4j is available.
  122. *
  123. * @param level Level to log at
  124. * @param message Message to log
  125. */
  126. public void log(final LogLevel level, final String message) {
  127. log(level, message, null);
  128. }
  129. /**
  130. * Log a message if log4j is available.
  131. *
  132. * @param level Level to log at
  133. * @param message Message to log
  134. * @param throwable Throwable to log alongside message
  135. */
  136. public void log(final LogLevel level, final String message, final Throwable throwable) {
  137. if (!isAvailable) {
  138. return;
  139. }
  140. try {
  141. if (throwable == null) {
  142. final Method method = log.getClass().getMethod(level.getMethodName(), String.class);
  143. method.invoke(log, message);
  144. } else {
  145. final Method method = log.getClass().getMethod(level.getMethodName(), String.class, Throwable.class);
  146. method.invoke(log, message, throwable);
  147. }
  148. } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException nsme) {
  149. }
  150. }
  151. }