Java IRC bot
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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /** Get an instance of Logging */
  71. public static Logging getLogging() {
  72. if (me == null) { me = new Logging(); }
  73. return me;
  74. }
  75. /** Create a new Logging */
  76. @SuppressWarnings("unchecked")
  77. private Logging() {
  78. boolean classExists = false;
  79. try {
  80. Class factory = null;
  81. // Check for classes
  82. Class.forName("org.apache.commons.logging.Log");
  83. factory = Class.forName("org.apache.commons.logging.LogFactory");
  84. classExists = (factory != null);
  85. if (classExists) {
  86. final Method getLog = factory.getMethod("getLog", new Class[]{Class.class});
  87. log = getLog.invoke(null, this.getClass());
  88. }
  89. } catch (ClassNotFoundException cnfe) {
  90. } catch (NoSuchMethodException nsme) {
  91. } catch (IllegalAccessException iae) {
  92. } catch (InvocationTargetException ite) {
  93. }
  94. isAvailable = (classExists && log != null);
  95. }
  96. /**
  97. * Check is a log level is available.
  98. *
  99. * @param level Level to check
  100. */
  101. public boolean levelEnabled(final LogLevel level) {
  102. if (isAvailable) {
  103. try {
  104. final Method check = log.getClass().getMethod(level.getCheckMethodName(), new Class[0]);
  105. return (Boolean)check.invoke(log, new Object[0]);
  106. } catch (NoSuchMethodException nsme) {
  107. } catch (IllegalAccessException iae) {
  108. } catch (InvocationTargetException ite) {
  109. }
  110. }
  111. return false;
  112. }
  113. /**
  114. * Log a message if log4j is available.
  115. *
  116. * @param level Level to log at
  117. * @param message Message to log
  118. */
  119. public void log(final LogLevel level, final String message) {
  120. log(level, message, null);
  121. }
  122. /**
  123. * Log a message if log4j is available.
  124. *
  125. * @param level Level to log at
  126. * @param message Message to log
  127. * @param throwable Throwable to log alongside message
  128. */
  129. public void log(final LogLevel level, final String message, final Throwable throwable) {
  130. if (!isAvailable) { return; }
  131. try {
  132. if (throwable == null) {
  133. final Method method = log.getClass().getMethod(level.getMethodName(), new Class[]{String.class});
  134. method.invoke(log, new Object[]{message});
  135. } else {
  136. final Method method = log.getClass().getMethod(level.getMethodName(), new Class[]{String.class, Throwable.class});
  137. method.invoke(log, new Object[]{message, throwable});
  138. }
  139. } catch (NoSuchMethodException nsme) {
  140. } catch (IllegalAccessException iae) {
  141. } catch (InvocationTargetException ite) {
  142. }
  143. }
  144. }