001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.internal;
013:
014: import java.util.HashMap;
015: import java.util.Map;
016: import java.util.Map.Entry;
017:
018: import net.sf.oval.internal.util.StringUtils;
019: import net.sf.oval.logging.Logger;
020: import net.sf.oval.logging.LoggerFactory;
021: import net.sf.oval.logging.LoggerFactoryJDKImpl;
022:
023: /**
024: * @author Sebastian Thomschke
025: */
026: public final class Log {
027: /* cannot use CollectionFactoryHolder.getFactory().createMap(32) here, since
028: * the collection factory uses the Log itself which is not yet initialized
029: */
030: private final static Map<String, Log> logRegistry = new HashMap<String, Log>(
031: 32);
032:
033: private static LoggerFactory loggerFactory = new LoggerFactoryJDKImpl();
034:
035: public static Log getLog(final Class clazz) {
036: if (clazz == null)
037: throw new IllegalArgumentException("clazz cannot be null");
038: return getLog(clazz.getName());
039: }
040:
041: public synchronized static Log getLog(final String name) {
042: if (name == null)
043: throw new IllegalArgumentException("name cannot be null");
044: Log log = logRegistry.get(name);
045: if (log == null) {
046: log = new Log(loggerFactory.createLogger(name));
047: }
048: return log;
049: }
050:
051: /**
052: * @return the loggerFactory
053: */
054: public static LoggerFactory getLoggerFactory() {
055: synchronized (logRegistry) {
056: return loggerFactory;
057: }
058: }
059:
060: /**
061: * @param loggerFactory the loggerFactory to set
062: */
063: public static void setLoggerFactory(
064: final LoggerFactory loggerFactory) {
065: if (loggerFactory == null)
066: throw new IllegalArgumentException(
067: "loggerFactory cannot be null");
068:
069: synchronized (logRegistry) {
070: Log.loggerFactory = loggerFactory;
071: for (final Entry<String, Log> entry : logRegistry
072: .entrySet()) {
073: entry.getValue().setLogger(
074: loggerFactory.createLogger(entry.getKey()));
075: }
076: }
077: }
078:
079: private Logger logger;
080:
081: /**
082: * private constructor to avoid external instantiation
083: */
084: private Log(final Logger logger) {
085: setLogger(logger);
086: }
087:
088: public void debug(final String msg) {
089: logger.debug(msg);
090: }
091:
092: public void debug(final String msgFormat, final Object arg1) {
093: if (logger.isDebug())
094: logger.debug(StringUtils.replaceAll(msgFormat, "{}",
095: arg1 == null ? "null" : arg1.toString()));
096: }
097:
098: public void debug(final String msgFormat, final Object arg1,
099: final Throwable t) {
100: if (logger.isDebug())
101: logger.debug(StringUtils.replaceAll(msgFormat, "{}",
102: arg1 == null ? "null" : arg1.toString()), t);
103: }
104:
105: public void debug(final String msg, final Throwable t) {
106: logger.debug(msg, t);
107: }
108:
109: public void error(final String msg) {
110: logger.error(msg);
111: }
112:
113: public void error(final String msgFormat, final Object arg1) {
114: if (logger.isError())
115: logger.error(StringUtils.replaceAll(msgFormat, "{}",
116: arg1 == null ? "null" : arg1.toString()));
117: }
118:
119: public void error(final String msgFormat, final Object arg1,
120: final Throwable t) {
121: if (logger.isError())
122: logger.error(StringUtils.replaceAll(msgFormat, "{}",
123: arg1 == null ? "null" : arg1.toString()), t);
124: }
125:
126: public void error(final String msg, final Throwable t) {
127: logger.error(msg, t);
128: }
129:
130: public void info(final String msg) {
131: logger.info(msg);
132: }
133:
134: public void info(final String msgFormat, final Object arg1) {
135: if (logger.isInfo())
136: logger.info(StringUtils.replaceAll(msgFormat, "{}",
137: arg1 == null ? "null" : arg1.toString()));
138: }
139:
140: public void info(final String msgFormat, final Object arg1,
141: final Throwable t) {
142: if (logger.isInfo())
143: logger.info(StringUtils.replaceAll(msgFormat, "{}",
144: arg1 == null ? "null" : arg1.toString()), t);
145: }
146:
147: public void info(final String msg, final Throwable t) {
148: logger.info(msg, t);
149: }
150:
151: public boolean isDebug() {
152: return logger.isDebug();
153: }
154:
155: public boolean isError() {
156: return logger.isError();
157: }
158:
159: public boolean isInfo() {
160: return logger.isInfo();
161: }
162:
163: public boolean isTrace() {
164: return logger.isTrace();
165: }
166:
167: public boolean isWarn() {
168: return logger.isWarn();
169: }
170:
171: private void setLogger(final Logger logger) {
172: if (logger == null)
173: throw new IllegalArgumentException("logger cannot be null");
174:
175: this .logger = logger;
176: }
177:
178: public void trace(final String msg) {
179: logger.debug(msg);
180: }
181:
182: public void trace(final String msgFormat, final Object arg1) {
183: if (logger.isDebug())
184: logger.trace(StringUtils.replaceAll(msgFormat, "{}",
185: arg1 == null ? "null" : arg1.toString()));
186: }
187:
188: public void trace(final String msgFormat, final Object arg1,
189: final Throwable t) {
190: if (logger.isDebug())
191: logger.trace(StringUtils.replaceAll(msgFormat, "{}",
192: arg1 == null ? "null" : arg1.toString()), t);
193: }
194:
195: public void trace(final String msg, final Throwable t) {
196: logger.trace(msg, t);
197: }
198:
199: public void warn(final String msg) {
200: logger.warn(msg);
201: }
202:
203: public void warn(final String msgFormat, final Object arg1) {
204: if (logger.isWarn())
205: logger.warn(StringUtils.replaceAll(msgFormat, "{}",
206: arg1 == null ? "null" : arg1.toString()));
207: }
208:
209: public void warn(final String msgFormat, final Object arg1,
210: final Throwable t) {
211: if (logger.isWarn())
212: logger.warn(StringUtils.replaceAll(msgFormat, "{}",
213: arg1 == null ? "null" : arg1.toString()), t);
214: }
215:
216: public void warn(final String msg, final Throwable t) {
217: logger.warn(msg, t);
218: }
219:
220: }
|