/interpreter/tags/at2dist110511/src/edu/vub/at/util/logging/Logger.java

http://ambienttalk.googlecode.com/ · Java · 221 lines · 85 code · 22 blank · 114 comment · 15 complexity · d237525856c4ccf8c3d2c543d32a7ec9 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * Logger.java created on 5-apr-2007 at 10:50:03
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.util.logging;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.Iterator;
  32. /**
  33. * A logger object modelled after the interface of the Log4J framework.
  34. *
  35. * @author tvcutsem
  36. */
  37. public class Logger {
  38. /**
  39. * A map for pooling all of the loggers.
  40. */
  41. private static final HashMap _LOGS = new HashMap();
  42. private final String name_;
  43. /**
  44. * Logs with a priority less than this will not get logged.
  45. */
  46. private int leastPriority_;
  47. /**
  48. * Access to the map should actually be synchronized, but this
  49. * adds overhead to accessing the loggers.
  50. *
  51. * Without synchronization, the possibility exists that same logger
  52. * is added twice. On the other hand, as loggers are stateless, this is not
  53. * really a problem (the new one will replace the old one without any harm)
  54. */
  55. public static Logger getInstance(String name) {
  56. Logger logger = (Logger) _LOGS.get(name);
  57. if (logger == null) {
  58. logger = new Logger(name);
  59. _LOGS.put(name, logger);
  60. }
  61. return logger;
  62. }
  63. public static Object[] getAllInstances() {
  64. return _LOGS.values().toArray();
  65. }
  66. /** When loggers are initialised with this setting, all messages (including debug info) are being logged. */
  67. public static final int _DEBUG_LEVEL_ = 1;
  68. /** When loggers are initialised with this setting, all messages except for debug info are being logged. */
  69. public static final int _WARN_LEVEL_ = 2;
  70. /** When loggers are initialised with this setting, errors as well as info (e.g. regarding connection status) is being logged. */
  71. public static final int _INFO_LEVEL_ = 3;
  72. /** When loggers are initialised with this setting, all errors (including tolerable ones) are reported. */
  73. public static final int _ERROR_LEVEL_ = 4;
  74. /** When loggers are initialised with this setting, only fatal errors are reported. */
  75. public static final int _FATAL_LEVEL_ = 5;
  76. private Logger(String nam) {
  77. name_ = nam;
  78. leastPriority_ = _DEBUG_LEVEL_;
  79. }
  80. /**
  81. * Set the priority of the logger.
  82. * @param priority - one of 'DEBUG', 'WARN', 'INFO', 'ERROR' or 'FATAL'
  83. * @throws IllegalArgumentException if the given argument is not one of the above logging levels.
  84. */
  85. public void setPriority(String priority) throws IllegalArgumentException {
  86. leastPriority_ = textToLevel(priority);
  87. }
  88. /**
  89. * Reports debugging information to be logged. Such messages are ignored when the logger has
  90. * its priority set to be higher than or equal to WARNING.
  91. * @param msg the message to be logged.
  92. */
  93. public void debug(String msg) {
  94. log(_DEBUG_LEVEL_, "DEBUG", msg, null);
  95. }
  96. /**
  97. * Reports debugging information to be logged. Such messages are ignored when the logger has
  98. * its priority set to be higher than or equal to WARNING.
  99. * @param msg the message to be logged.
  100. * @param exc the underlying exception that triggered the log request.
  101. */
  102. public void debug(String msg, Throwable exc) {
  103. log(_DEBUG_LEVEL_, "DEBUG", msg, exc);
  104. }
  105. /**
  106. * Reports a warning (i.e a suspected inconsistency in the interpreter) to be logged. Such
  107. * messages are ignored when the logger has its priority set to be higher than or equal to
  108. * INFO.
  109. * @param msg the message to be logged.
  110. */
  111. public void warn(String msg) {
  112. log(_WARN_LEVEL_, "WARN", msg, null);
  113. }
  114. /**
  115. * Reports a warning (i.e a suspected inconsistency in the interpreter) to be logged. Such
  116. * messages are ignored when the logger has its priority set to be higher than or equal to
  117. * INFO.
  118. * @param msg the message to be logged.
  119. * @param exc the underlying exception that triggered the log request.
  120. */
  121. public void warn(String msg, Throwable exc) {
  122. log(_WARN_LEVEL_, "WARN", msg, exc);
  123. }
  124. /**
  125. * Reports useful information on the interpreter's functioning (e.g. changes in connection
  126. * state) to be logged. Such messages are ignored when the logger has its priority set to be
  127. * higher than or equal to ERROR.
  128. * @param msg the message to be logged.
  129. */
  130. public void info(String msg) {
  131. log(_INFO_LEVEL_, "INFO", msg, null);
  132. }
  133. /**
  134. * Reports useful information on the interpreter's functioning (e.g. changes in connection
  135. * state) to be logged. Such messages are ignored when the logger has its priority set to be
  136. * higher than or equal to ERROR.
  137. * @param msg the message to be logged.
  138. * @param exc the underlying exception that triggered the log request.
  139. */
  140. public void info(String msg, Throwable exc) {
  141. log(_INFO_LEVEL_, "INFO", msg, exc);
  142. }
  143. /**
  144. * Reports a tolerable error which occurred in the interpreter. Such messages are logged unless
  145. * the logger set to FATAL.
  146. * @param msg the message to be logged.
  147. */
  148. public void error(String msg) {
  149. log(_ERROR_LEVEL_, "ERROR", msg, null);
  150. }
  151. /**
  152. * Reports a tolerable error which occurred in the interpreter. Such messages are logged unless
  153. * the logger set to FATAL.
  154. * @param msg the message to be logged.
  155. * @param exc the underlying exception that triggered the log request.
  156. */
  157. public void error(String msg, Throwable exc) {
  158. log(_ERROR_LEVEL_, "ERROR", msg, exc);
  159. }
  160. /**
  161. * Reports a fatal error in the interpreter. Such messages are always logged.
  162. * @param msg the message to be logged.
  163. */
  164. public void fatal(String msg) {
  165. log(_FATAL_LEVEL_, "FATAL", msg, null);
  166. }
  167. /**
  168. * Reports a fatal error in the interpreter. Such messages are always logged.
  169. * @param msg the message to be logged.
  170. * @param exc the underlying exception that triggered the log request.
  171. */
  172. public void fatal(String msg, Throwable exc) {
  173. log(_FATAL_LEVEL_, "FATAL", msg, exc);
  174. }
  175. private void log(int priority, String textPriority, String msg, Throwable exc) {
  176. if (priority >= leastPriority_) {
  177. // format: date priority logname - message
  178. System.err.println(new Date().toString() + " " + textPriority + " "+ name_ + " - " + msg);
  179. if (exc != null) {
  180. exc.printStackTrace(System.err);
  181. }
  182. }
  183. }
  184. private int textToLevel(String priority) throws IllegalArgumentException {
  185. if (priority.equalsIgnoreCase("DEBUG")) {
  186. return _DEBUG_LEVEL_;
  187. } else if (priority.equalsIgnoreCase("WARN")) {
  188. return _WARN_LEVEL_;
  189. } else if (priority.equalsIgnoreCase("INFO")) {
  190. return _INFO_LEVEL_;
  191. } else if (priority.equalsIgnoreCase("ERROR")) {
  192. return _ERROR_LEVEL_;
  193. } else if (priority.equalsIgnoreCase("FATAL")) {
  194. return _FATAL_LEVEL_;
  195. } else {
  196. throw new IllegalArgumentException("Illegal priority value: " + priority);
  197. }
  198. }
  199. }