/interpreter/tags/at2-build270707/src/edu/vub/at/util/logging/Logger.java

http://ambienttalk.googlecode.com/ · Java · 220 lines · 84 code · 22 blank · 114 comment · 15 complexity · 6482a8213e2a398e2671ab496ed5f5e2 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. /**
  32. * A logger object modelled after the interface of the Log4J framework.
  33. *
  34. * @author tvcutsem
  35. */
  36. public class Logger {
  37. /**
  38. * A map for pooling all of the loggers.
  39. */
  40. private static final HashMap _LOGS = new HashMap();
  41. private final String name_;
  42. /**
  43. * Logs with a priority less than this will not get logged.
  44. */
  45. private int leastPriority_;
  46. private String textPriority_;
  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. /** When loggers are initialised with this setting, all messages (including debug info) are being logged. */
  64. public static final int _DEBUG_LEVEL_ = 1;
  65. /** When loggers are initialised with this setting, all messages except for debug info are being logged. */
  66. public static final int _WARN_LEVEL_ = 2;
  67. /** When loggers are initialised with this setting, errors as well as info (e.g. regarding connection status) is being logged. */
  68. public static final int _INFO_LEVEL_ = 3;
  69. /** When loggers are initialised with this setting, all errors (including tolerable ones) are reported. */
  70. public static final int _ERROR_LEVEL_ = 4;
  71. /** When loggers are initialised with this setting, only fatal errors are reported. */
  72. public static final int _FATAL_LEVEL_ = 5;
  73. private Logger(String nam) {
  74. name_ = nam;
  75. leastPriority_ = _DEBUG_LEVEL_;
  76. textPriority_ = "DEBUG";
  77. }
  78. /**
  79. * Set the priority of the logger.
  80. * @param priority - one of 'DEBUG', 'WARN', 'INFO', 'ERROR' or 'FATAL'
  81. * @throws IllegalArgumentException if the given argument is not one of the above logging levels.
  82. */
  83. public void setPriority(String priority) throws IllegalArgumentException {
  84. leastPriority_ = textToLevel(priority);
  85. textPriority_ = priority;
  86. }
  87. /**
  88. * Reports debugging information to be logged. Such messages are ignored when the logger has
  89. * its priority set to be higher than or equal to WARNING.
  90. * @param msg the message to be logged.
  91. */
  92. public void debug(String msg) {
  93. log(_DEBUG_LEVEL_, msg, null);
  94. }
  95. /**
  96. * Reports debugging information to be logged. Such messages are ignored when the logger has
  97. * its priority set to be higher than or equal to WARNING.
  98. * @param msg the message to be logged.
  99. * @param exc the underlying exception that triggered the log request.
  100. */
  101. public void debug(String msg, Throwable exc) {
  102. log(_DEBUG_LEVEL_, msg, exc);
  103. }
  104. /**
  105. * Reports a warning (i.e a suspected inconsistency in the interpreter) to be logged. Such
  106. * messages are ignored when the logger has its priority set to be higher than or equal to
  107. * INFO.
  108. * @param msg the message to be logged.
  109. */
  110. public void warn(String msg) {
  111. log(_WARN_LEVEL_, msg, null);
  112. }
  113. /**
  114. * Reports a warning (i.e a suspected inconsistency in the interpreter) to be logged. Such
  115. * messages are ignored when the logger has its priority set to be higher than or equal to
  116. * INFO.
  117. * @param msg the message to be logged.
  118. * @param exc the underlying exception that triggered the log request.
  119. */
  120. public void warn(String msg, Throwable exc) {
  121. log(_WARN_LEVEL_, msg, exc);
  122. }
  123. /**
  124. * Reports useful information on the interpreter's functioning (e.g. changes in connection
  125. * state) to be logged. Such messages are ignored when the logger has its priority set to be
  126. * higher than or equal to ERROR.
  127. * @param msg the message to be logged.
  128. */
  129. public void info(String msg) {
  130. log(_INFO_LEVEL_, msg, null);
  131. }
  132. /**
  133. * Reports useful information on the interpreter's functioning (e.g. changes in connection
  134. * state) to be logged. Such messages are ignored when the logger has its priority set to be
  135. * higher than or equal to ERROR.
  136. * @param msg the message to be logged.
  137. * @param exc the underlying exception that triggered the log request.
  138. */
  139. public void info(String msg, Throwable exc) {
  140. log(_INFO_LEVEL_, msg, exc);
  141. }
  142. /**
  143. * Reports a tolerable error which occurred in the interpreter. Such messages are logged unless
  144. * the logger set to FATAL.
  145. * @param msg the message to be logged.
  146. */
  147. public void error(String msg) {
  148. log(_ERROR_LEVEL_, msg, null);
  149. }
  150. /**
  151. * Reports a tolerable error which occurred in the interpreter. Such messages are logged unless
  152. * the logger set to FATAL.
  153. * @param msg the message to be logged.
  154. * @param exc the underlying exception that triggered the log request.
  155. */
  156. public void error(String msg, Throwable exc) {
  157. log(_ERROR_LEVEL_, msg, exc);
  158. }
  159. /**
  160. * Reports a fatal error in the interpreter. Such messages are always logged.
  161. * @param msg the message to be logged.
  162. */
  163. public void fatal(String msg) {
  164. log(_FATAL_LEVEL_, msg, null);
  165. }
  166. /**
  167. * Reports a fatal error in the interpreter. Such messages are always logged.
  168. * @param msg the message to be logged.
  169. * @param exc the underlying exception that triggered the log request.
  170. */
  171. public void fatal(String msg, Throwable exc) {
  172. log(_FATAL_LEVEL_, msg, exc);
  173. }
  174. private void log(int priority, String msg, Throwable exc) {
  175. if (priority >= leastPriority_) {
  176. // format: date priority logname - message
  177. System.err.println(new Date().toString() + " " + textPriority_ + " "+ name_ + " - " + msg);
  178. if (exc != null) {
  179. exc.printStackTrace(System.err);
  180. }
  181. }
  182. }
  183. private int textToLevel(String priority) throws IllegalArgumentException {
  184. if (priority.equalsIgnoreCase("DEBUG")) {
  185. return _DEBUG_LEVEL_;
  186. } else if (priority.equalsIgnoreCase("WARN")) {
  187. return _WARN_LEVEL_;
  188. } else if (priority.equalsIgnoreCase("INFO")) {
  189. return _INFO_LEVEL_;
  190. } else if (priority.equalsIgnoreCase("ERROR")) {
  191. return _ERROR_LEVEL_;
  192. } else if (priority.equalsIgnoreCase("FATAL")) {
  193. return _FATAL_LEVEL_;
  194. } else {
  195. throw new IllegalArgumentException("Illegal priority value: " + priority);
  196. }
  197. }
  198. }