PageRenderTime 23ms CodeModel.GetById 11ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 158 lines | 84 code | 22 blank | 52 comment | 15 complexity | 94df672933bae4753573b21bc7a94f47 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	private String textPriority_;
 53	
 54	/**
 55	 * Access to the map should actually be synchronized, but this
 56	 * adds overhead to accessing the loggers.
 57	 * 
 58	 * Without synchronization, the possibility exists that same logger
 59	 * is added twice. On the other hand, as loggers are stateless, this is not
 60	 * really a problem (the new one will replace the old one without any harm)
 61	 */
 62	public static Logger getInstance(String name) {
 63		Logger logger = (Logger) _LOGS.get(name);
 64		if (logger == null) {
 65			logger = new Logger(name);
 66			_LOGS.put(name, logger);
 67		}
 68		return logger;
 69	}
 70	
 71	public static final int _DEBUG_LEVEL_ = 1;
 72	public static final int _WARN_LEVEL_  = 2;
 73	public static final int _INFO_LEVEL_  = 3;
 74	public static final int _ERROR_LEVEL_ = 4;
 75	public static final int _FATAL_LEVEL_ = 5;
 76	
 77	private Logger(String nam) {
 78		name_ = nam;
 79		leastPriority_ = _DEBUG_LEVEL_;
 80		textPriority_ = "DEBUG";
 81	}
 82	
 83	/**
 84	 * Set the priority of the logger.
 85	 * @param priority - one of 'DEBUG', 'WARN', 'INFO', 'ERROR' or 'FATAL'
 86	 * @throws IllegalArgumentException if the given argument is not one of the above logging levels.
 87	 */
 88	public void setPriority(String priority) throws IllegalArgumentException {
 89		leastPriority_ = textToLevel(priority);
 90		textPriority_ = priority;
 91	}
 92	
 93	public void debug(String msg) {
 94		log(_DEBUG_LEVEL_, msg, null);
 95	}
 96	
 97	public void debug(String msg, Throwable exc) {
 98		log(_DEBUG_LEVEL_, msg, exc);
 99	}
100	
101	public void warn(String msg) {
102		log(_WARN_LEVEL_, msg, null);
103	}
104	
105	public void warn(String msg, Throwable exc) {
106		log(_WARN_LEVEL_, msg, exc);
107	}
108	
109	public void info(String msg) {
110		log(_INFO_LEVEL_, msg, null);
111	}
112	
113	public void info(String msg, Throwable exc) {
114		log(_INFO_LEVEL_, msg, exc);
115	}
116	
117	public void error(String msg) {
118		log(_ERROR_LEVEL_, msg, null);
119	}
120	
121	public void error(String msg, Throwable exc) {
122		log(_ERROR_LEVEL_, msg, exc);
123	}
124	
125	public void fatal(String msg) {
126		log(_FATAL_LEVEL_, msg, null);
127	}
128	
129	public void fatal(String msg, Throwable exc) {
130		log(_FATAL_LEVEL_, msg, exc);
131	}
132	
133	private void log(int priority, String msg, Throwable exc) {
134		if (priority >= leastPriority_) {
135			// format: date priority logname - message
136			System.err.println(new Date().toString() + " " + textPriority_ + " "+ name_ + " - " + msg);
137			if (exc != null) {
138				exc.printStackTrace(System.err);
139			}
140		}
141	}
142	
143	private int textToLevel(String priority) throws IllegalArgumentException {
144		if (priority.equalsIgnoreCase("DEBUG")) {
145			return _DEBUG_LEVEL_;
146		} else if (priority.equalsIgnoreCase("WARN")) {
147			return _WARN_LEVEL_;
148		} else if (priority.equalsIgnoreCase("INFO")) {
149			return _INFO_LEVEL_;
150		} else if (priority.equalsIgnoreCase("ERROR")) {
151			return _ERROR_LEVEL_;
152		} else if (priority.equalsIgnoreCase("FATAL")) {
153			return _FATAL_LEVEL_;
154		} else {
155			throw new IllegalArgumentException("Illegal priority value: " + priority);
156		}
157	}
158}