Java tutorial
/******************************************************************************* * ENdoSnipe 5.0 - (https://github.com/endosnipe) * * The MIT License (MIT) * * Copyright (c) 2012 Acroquest Technology Co.,Ltd. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ package jp.co.acroquest.endosnipe.common.logger; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.LogFactory; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.xml.DOMConfigurator; import org.seasar.framework.message.MessageFormatter; import org.seasar.framework.util.AssertionUtil; /** * ?????<br /> * ???Eclipse ????????? * ????<br /> * <ul> * <li>Eclipse ?????log4j ?????</li> * <li>Eclipse ??????? * (? Eclipse for RCP/Plug-in Developers ??????)</li> * </ul> * * ???????????????? * * <h2>ENdoSnipeLogger ??</h2> * <p> * ??????????????? {@link Class} ? * ?? {@link Class} ??log4j ?? * ????? * </p> * * <pre> * private static final ENdoSnipeLogger LOGGER = * ENdoSnipeLogger.getLogger(OutputAction.class); * </pre> * * <p> * ???Eclipse ??????? * ???2? {@link PluginProvider} ? static ??????? * ????????? * </p> * * <pre> * private static final ENdoSnipeLogger LOGGER = * ENdoSnipeLogger.getLogger(OutputAction.class, ArrowVisionPluginProvider.INSTANCE); * </pre> * </li> * * <h2>??</h2> * <p> * ???????????????? * </p> * <ul> * <li>{@link #log(String, Object...)}</li> * <li>{@link #log(String, Throwable, Object...)}</li> * </ul> * <ol> * <li> * ID ?<br /> * 3 ?? ID ???<br /> * ID ?????????????<br /> * () ENdoSnipeDataCollector <code>EDC</code> * </li> * <li> * ??<br /> * <code><i> ID</i>Messages</code>??????? * ????<br /> * ??(? src/main/resources )?????????<br /> * () <code>EDCMessages_ja.properties</code> * </li> * <li> * ?<br /> * <p>???????</p> * * <i></i>=<i></i> * * <p>???????</p> * * <i></i><i> ID</i><i>?</i> * * <p> * ?? 1 ???(F,E,W,I,D,T) ??????<br /> * ?? 3 ? ID ???<br /> * ? 4 ??????????<br /> * </p> * * () <code>IEDC0001=ENdoSnipe DataCollector ???.</code> * * <p> * ????{0}?{1}????????<br /> * </p> * * () <code>IEDC0008=Javelin ?????.(: {0}:{1})</code> * </li> * </ol> * * @author y-komori * @author nagai */ public abstract class ENdoSnipeLogger { private static final Map<Class<?>, ENdoSnipeLogger> LOGGERS = new HashMap<Class<?>, ENdoSnipeLogger>(); private static boolean initialized__; private static boolean isEclipseAvailable__; private static boolean useSystemLogger__; /** * {@link ENdoSnipeLogger} ????<br /> */ protected ENdoSnipeLogger() { // Do nothing. } /** * ? {@link SystemLogger} ????????<br /> * Javelin ?? {@link SystemLogger} ?????? * ???? {@link SystemLogger} ??????????<br /> * {@link SystemLogger} ????????? * {@link ENdoSnipeLogger} ????? {@link SystemLogger} ?????? * @param useSystemLogger {@link SystemLogger} ???<code>true</code> */ public static synchronized void setSystemLoggerMode(final boolean useSystemLogger) { useSystemLogger__ = useSystemLogger; } /** * {@link ENdoSnipeLogger} ???<br /> * * @param clazz {@link Class} * @return {@link ENdoSnipeLogger} */ public static synchronized ENdoSnipeLogger getLogger(final Class<?> clazz) { return getLogger(clazz, null); } /** * {@link ENdoSnipeLogger} ???<br /> * * @param clazz {@link Class} * @param provider {@link PluginProvider} * @return {@link ENdoSnipeLogger} */ public static synchronized ENdoSnipeLogger getLogger(final Class<?> clazz, final PluginProvider provider) { if (!initialized__) { initialize(); } ENdoSnipeLogger logger = LOGGERS.get(clazz); if (logger == null) { logger = createENdoSnpeLogger(clazz, provider); LOGGERS.put(clazz, logger); } return logger; } /** * {@link ENdoSnipeLogger} ????<br /> */ protected static synchronized void initialize() { initialized__ = true; } /** * ???<br /> */ public static synchronized void dispose() { LogFactory.releaseAll(); LOGGERS.clear(); initialized__ = false; isEclipseAvailable__ = false; } private static ENdoSnipeLogger createENdoSnpeLogger(final Class<?> clazz, final PluginProvider provider) { if (useSystemLogger__ == true) { // SystemLogger ?? return new SystemENdoSnipeLogger(); } else { // ?????? return new Log4jENdoSnipeLogger(clazz); } } /** * XML ?????<br /> * ??? log4j ???? * * @param config ? {@link URL} */ public void addXmlConfig(final URL config) { AssertionUtil.assertNotNull("config", config); DOMConfigurator.configure(config); } /** * properties ?????<br /> * ??? log4j ???? * * @param config ? {@link URL} */ public void addPropertyConfig(final URL config) { AssertionUtil.assertNotNull("config", config); PropertyConfigurator.configure(config); } /** * TRACE?????????<br /> * {@link SystemLogger} ??????DEBUG????????? * * @return TRACE?????? */ public abstract boolean isTraceEnabled(); /** * TRACE???<br /> * * @param message * @param throwable {@link Throwable} * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void trace(final Object message, final Throwable throwable); /** * TRACE???<br /> * * @param message * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void trace(final Object message); /** * DEBUG?????????<br /> * * @return DEBUG?????? */ public abstract boolean isDebugEnabled(); /** * DEBUG???<br /> * * @param message * @param throwable {@link Throwable} * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void debug(final Object message, final Throwable throwable); /** * DEBUG???<br /> * * @param message * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void debug(final Object message); /** * INFO?????????<br /> * * @return INFO?????? */ public abstract boolean isInfoEnabled(); /** * INFO???<br /> * * @param message * @param throwable {@link Throwable} * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void info(final Object message, final Throwable throwable); /** * INFO???<br /> * * @param message * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void info(final Object message); /** * WARN???<br /> * * @param message * @param throwable {@link Throwable} * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void warn(final Object message, final Throwable throwable); /** * WARN???<br /> * * @param throwable * @deprecated ???????????????? log() ??????? */ @Deprecated public void warn(final Throwable throwable) { this.warn(throwable.toString(), throwable); } /** * WARN???<br /> * * @param message * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void warn(final Object message); /** * ERROR???<br /> * * @param message * @param throwable {@link Throwable} * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void error(final Object message, final Throwable throwable); /** * ERROR???<br /> * * @param message * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void error(final Object message); /** * FATAL???<br /> * * @param message * @param throwable {@link Throwable} * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void fatal(final Object message, final Throwable throwable); /** * FATAL???<br /> * * @param message * @deprecated ???????????????? log() ??????? */ @Deprecated public abstract void fatal(final Object message); /** * ???<br /> * * @param throwable {@link Throwable} * @deprecated ????????????? * ?? {@link #log(String, Throwable, Object...)} ??????? */ @Deprecated public void log(final Throwable throwable) { error(throwable.getClass().getName() + " : " + throwable.getMessage(), throwable); } /** * ?????<br /> * * @param messageCode * @param args */ public void log(final String messageCode, final Object... args) { log(messageCode, null, args); } /** * ?????<br /> * * @param messageCode * @param throwable {@link Throwable} * @param args */ public void log(final String messageCode, final Throwable throwable, final Object... args) { char messageType = messageCode.charAt(0); if (isEnabledFor(messageType)) { String message = getMessage(messageCode, args); switch (messageType) { case 'T': trace(message, throwable); break; case 'D': debug(message, throwable); break; case 'I': info(message, throwable); break; case 'W': warn(message, throwable); break; case 'E': error(message, throwable); break; case 'F': fatal(message, throwable); break; default: throw new IllegalArgumentException(String.valueOf(messageType)); } } } /** * properties???????<br /> * * @param messageCode ? * @param args ?? * @return */ protected String getMessage(final String messageCode, final Object... args) { return MessageFormatter.getMessage(messageCode, args); } /** * ????????? * @param messageType * @return ????????true/?????????false */ protected abstract boolean isEnabledFor(final char messageType); /** * ????<br /> * * @param obj * @return */ public static String getObjectDescription(final Object obj) { if (obj != null) { return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode()); } else { return "NULL"; } } /** * message?toString?? * null???"Unknown Error"? * * @param message * @return message?toString?null???"Unknown Error" */ protected String createMessage(final Object message) { String messageString; if (message != null) { messageString = message.toString(); } else { messageString = getMessage(CommonLogMessageCodes.UNEXPECTED_ERROR); } return messageString; } }