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

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

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