PageRenderTime 33ms CodeModel.GetById 19ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 28package edu.vub.at.util.logging;
 29
 30import java.util.Date;
 31import java.util.HashMap;
 32import java.util.Iterator;
 33
 34/**
 35 * A logger object modelled after the interface of the Log4J framework.
 36 * 
 37 * @author tvcutsem
 38 */
 39public class Logger {
 40
 41	/**
 42	 *  A map for pooling all of the loggers.
 43	 */
 44	private static final HashMap _LOGS = new HashMap();
 45	
 46	private final String name_;
 47	
 48	/**
 49	 * Logs with a priority less than this will not get logged.
 50	 */
 51	private int leastPriority_;
 52	
 53	/**
 54	 * Access to the map should actually be synchronized, but this
 55	 * adds overhead to accessing the loggers.
 56	 * 
 57	 * Without synchronization, the possibility exists that same logger
 58	 * is added twice. On the other hand, as loggers are stateless, this is not
 59	 * really a problem (the new one will replace the old one without any harm)
 60	 */
 61	public static Logger getInstance(String name) {
 62		Logger logger = (Logger) _LOGS.get(name);
 63		if (logger == null) {
 64			logger = new Logger(name);
 65			_LOGS.put(name, logger);
 66		}
 67		return logger;
 68	}
 69	
 70	public static Object[] getAllInstances() {
 71		return _LOGS.values().toArray();
 72	}
 73	
 74	/** When loggers are initialised with this setting, all messages (including debug info) are being logged. */ 
 75	public static final int _DEBUG_LEVEL_ = 1;
 76	/** When loggers are initialised with this setting, all messages except for debug info are being logged. */ 
 77	public static final int _WARN_LEVEL_  = 2;
 78	/** When loggers are initialised with this setting, errors as well as info (e.g. regarding connection status) is being logged. */ 
 79	public static final int _INFO_LEVEL_  = 3;
 80	/** When loggers are initialised with this setting, all errors (including tolerable ones) are reported. */ 
 81	public static final int _ERROR_LEVEL_ = 4;
 82	/** When loggers are initialised with this setting, only fatal errors are reported. */ 
 83	public static final int _FATAL_LEVEL_ = 5;
 84	
 85	private Logger(String nam) {
 86		name_ = nam;
 87		leastPriority_ = _DEBUG_LEVEL_;
 88	}
 89	
 90	/**
 91	 * Set the priority of the logger.
 92	 * @param priority - one of 'DEBUG', 'WARN', 'INFO', 'ERROR' or 'FATAL'
 93	 * @throws IllegalArgumentException if the given argument is not one of the above logging levels.
 94	 */
 95	public void setPriority(String priority) throws IllegalArgumentException {
 96		leastPriority_ = textToLevel(priority);
 97	}
 98	
 99	/**
100	 * Reports debugging information to be logged. Such messages are ignored when the logger has
101	 * its priority set to be higher than or equal to WARNING. 
102	 * @param msg the message to be logged.
103	 */
104	public void debug(String msg) {
105		log(_DEBUG_LEVEL_, "DEBUG", msg, null);
106	}
107	
108	/**
109	 * Reports debugging information to be logged. Such messages are ignored when the logger has
110	 * its priority set to be higher than or equal to WARNING. 
111	 * @param msg the message to be logged.
112	 * @param exc the underlying exception that triggered the log request.
113	 */
114	public void debug(String msg, Throwable exc) {
115		log(_DEBUG_LEVEL_, "DEBUG", msg, exc);
116	}
117	
118	/**
119	 * Reports a warning (i.e a suspected inconsistency in the interpreter) to be logged. Such 
120	 * messages are ignored when the logger has its priority set to be higher than or equal to 
121	 * INFO. 
122	 * @param msg the message to be logged.
123	 */
124	public void warn(String msg) {
125		log(_WARN_LEVEL_, "WARN", msg, null);
126	}
127	
128	/**
129	 * Reports a warning (i.e a suspected inconsistency in the interpreter) to be logged. Such 
130	 * messages are ignored when the logger has its priority set to be higher than or equal to 
131	 * INFO. 
132	 * @param msg the message to be logged.
133	 * @param exc the underlying exception that triggered the log request.
134	 */
135	public void warn(String msg, Throwable exc) {
136		log(_WARN_LEVEL_, "WARN", msg, exc);
137	}
138	
139	/**
140	 * Reports useful information on the interpreter's functioning (e.g. changes in connection 
141	 * state) to be logged. Such messages are ignored when the logger has its priority set to be 
142	 * higher than or equal to ERROR. 
143	 * @param msg the message to be logged.
144	 */
145	public void info(String msg) {
146		log(_INFO_LEVEL_, "INFO", msg, null);
147	}
148	
149	/**
150	 * Reports useful information on the interpreter's functioning (e.g. changes in connection 
151	 * state) to be logged. Such messages are ignored when the logger has its priority set to be 
152	 * higher than or equal to ERROR. 
153	 * @param msg the message to be logged.
154	 * @param exc the underlying exception that triggered the log request.
155	 */
156	public void info(String msg, Throwable exc) {
157		log(_INFO_LEVEL_, "INFO", msg, exc);
158	}
159	
160	/**
161	 * Reports a tolerable error which occurred in the interpreter. Such messages are logged unless
162	 * the logger set to FATAL. 
163	 * @param msg the message to be logged.
164	 */
165	public void error(String msg) {
166		log(_ERROR_LEVEL_, "ERROR", msg, null);
167	}
168	
169	/**
170	 * Reports a tolerable error which occurred in the interpreter. Such messages are logged unless
171	 * the logger set to FATAL. 
172	 * @param msg the message to be logged.
173	 * @param exc the underlying exception that triggered the log request.
174	 */
175	public void error(String msg, Throwable exc) {
176		log(_ERROR_LEVEL_, "ERROR", msg, exc);
177	}
178	
179	/**
180	 * Reports a fatal error in the interpreter. Such messages are always logged.
181	 * @param msg the message to be logged.
182	 */
183	public void fatal(String msg) {
184		log(_FATAL_LEVEL_, "FATAL", msg, null);
185	}
186	
187	/**
188	 * Reports a fatal error in the interpreter. Such messages are always logged.
189	 * @param msg the message to be logged.
190	 * @param exc the underlying exception that triggered the log request.
191	 */
192	public void fatal(String msg, Throwable exc) {
193		log(_FATAL_LEVEL_, "FATAL", msg, exc);
194	}
195	
196	private void log(int priority, String textPriority, String msg, Throwable exc) {
197		if (priority >= leastPriority_) {
198			// format: date priority logname - message
199			System.err.println(new Date().toString() + " " + textPriority + " "+ name_ + " - " + msg);
200			if (exc != null) {
201				exc.printStackTrace(System.err);
202			}
203		}
204	}
205	
206	private int textToLevel(String priority) throws IllegalArgumentException {
207		if (priority.equalsIgnoreCase("DEBUG")) {
208			return _DEBUG_LEVEL_;
209		} else if (priority.equalsIgnoreCase("WARN")) {
210			return _WARN_LEVEL_;
211		} else if (priority.equalsIgnoreCase("INFO")) {
212			return _INFO_LEVEL_;
213		} else if (priority.equalsIgnoreCase("ERROR")) {
214			return _ERROR_LEVEL_;
215		} else if (priority.equalsIgnoreCase("FATAL")) {
216			return _FATAL_LEVEL_;
217		} else {
218			throw new IllegalArgumentException("Illegal priority value: " + priority);
219		}
220	}
221}