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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Method;
  24. import java.lang.reflect.InvocationTargetException;
  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. private 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() { return methodName; }
  57. /**
  58. * Get the Name of the check method in log4j
  59. *
  60. * @return Name of check method in log4j
  61. */
  62. public String getCheckMethodName() { return checkMethodName; }
  63. };
  64. /** Singleton Instance of Logging. */
  65. private static Logging me;
  66. /** Is log4j available. */
  67. private final boolean isAvailable;
  68. /** "Log" object if available. */
  69. private Object log = null;
  70. /**
  71. * Get an instance of Logging.
  72. *
  73. * @return The instance of Logging
  74. */
  75. public static Logging getLogging() {
  76. if (me == null) { me = new Logging(); }
  77. return me;
  78. }
  79. /** Create a new Logging. */
  80. @SuppressWarnings("unchecked")
  81. private Logging() {
  82. boolean classExists = false;
  83. try {
  84. Class factory = null;
  85. // Check for classes
  86. Class.forName("org.apache.commons.logging.Log");
  87. factory = Class.forName("org.apache.commons.logging.LogFactory");
  88. classExists = (factory != null);
  89. if (classExists) {
  90. final Method getLog = factory.getMethod("getLog", new Class[]{Class.class});
  91. log = getLog.invoke(null, this.getClass());
  92. }
  93. } catch (ClassNotFoundException cnfe) {
  94. } catch (NoSuchMethodException nsme) {
  95. } catch (IllegalAccessException iae) {
  96. } catch (InvocationTargetException ite) {
  97. }
  98. isAvailable = (classExists && log != null);
  99. }
  100. /**
  101. * Check is a log level is available.
  102. *
  103. * @param level Level to check
  104. */
  105. public boolean levelEnabled(final LogLevel level) {
  106. if (isAvailable) {
  107. try {
  108. final Method check = log.getClass().getMethod(level.getCheckMethodName(), new Class[0]);
  109. return (Boolean)check.invoke(log, new Object[0]);
  110. } catch (NoSuchMethodException nsme) {
  111. } catch (IllegalAccessException iae) {
  112. } catch (InvocationTargetException ite) {
  113. }
  114. }
  115. return false;
  116. }
  117. /**
  118. * Log a message if log4j is available.
  119. *
  120. * @param level Level to log at
  121. * @param message Message to log
  122. */
  123. public void log(final LogLevel level, final String message) {
  124. log(level, message, null);
  125. }
  126. /**
  127. * Log a message if log4j is available.
  128. *
  129. * @param level Level to log at
  130. * @param message Message to log
  131. * @param throwable Throwable to log alongside message
  132. */
  133. public void log(final LogLevel level, final String message, final Throwable throwable) {
  134. if (!isAvailable) { return; }
  135. try {
  136. if (throwable == null) {
  137. final Method method = log.getClass().getMethod(level.getMethodName(), new Class[]{String.class});
  138. method.invoke(log, new Object[]{message});
  139. } else {
  140. final Method method = log.getClass().getMethod(level.getMethodName(), new Class[]{String.class, Throwable.class});
  141. method.invoke(log, new Object[]{message, throwable});
  142. }
  143. } catch (NoSuchMethodException nsme) {
  144. } catch (IllegalAccessException iae) {
  145. } catch (InvocationTargetException ite) {
  146. }
  147. }
  148. }