Java tutorial
/* * Copyright 2004-2008 the Seasar Foundation and the Others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package org.seasar.uruma.log; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.Log; 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.uruma.core.UrumaConstants; import org.seasar.uruma.util.AssertionUtil; /** * ?????<br /> * * @author y-komori */ public final class UrumaLogger { private static final Map<Class<?>, UrumaLogger> loggers = new HashMap<Class<?>, UrumaLogger>(); private static boolean initialized; private final Log log; /** * {@link UrumaLogger} ???<br /> * * @param clazz * {@link Class} * @return {@link UrumaLogger} */ public static synchronized UrumaLogger getLogger(final Class<?> clazz) { if (!initialized) { initialize(); } UrumaLogger logger = loggers.get(clazz); if (logger == null) { logger = new UrumaLogger(clazz); loggers.put(clazz, logger); } return logger; } /** * {@link UrumaLogger} ????<br /> */ public static synchronized void initialize() { initialized = true; } /** * ???<br /> */ public synchronized static void dispose() { LogFactory.releaseAll(); loggers.clear(); initialized = false; } private UrumaLogger(final Class<?> clazz) { log = LogFactory.getLog(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 /> * * @return TRACE?????? */ public final boolean isTraceEnabled() { return log.isTraceEnabled(); } /** * TRACE???<br /> * * @param message * * @param throwable * {@link Throwable} */ public final void trace(final Object message, final Throwable throwable) { if (isTraceEnabled()) { log.trace(message, throwable); } } /** * TRACE???<br /> * * @param message * */ public final void trace(final Object message) { if (isTraceEnabled()) { log.trace(message); } } /** * DEBUG?????????<br /> * * @return DEBUG?????? */ public final boolean isDebugEnabled() { return log.isDebugEnabled(); } /** * DEBUG???<br /> * * @param message * * @param throwable * {@link Throwable} */ public final void debug(final Object message, final Throwable throwable) { if (isDebugEnabled()) { log.debug(message, throwable); } } /** * DEBUG???<br /> * * @param message * */ public final void debug(final Object message) { if (isDebugEnabled()) { log.debug(message); } } /** * INFO?????????<br /> * * @return INFO?????? */ public final boolean isInfoEnabled() { return log.isInfoEnabled(); } /** * INFO???<br /> * * @param message * * @param throwable * {@link Throwable} */ public final void info(final Object message, final Throwable throwable) { if (isInfoEnabled()) { log.info(message, throwable); } } /** * INFO???<br /> * * @param message * */ public final void info(final Object message) { if (isInfoEnabled()) { log.info(message); } } /** * WARN???<br /> * * @param message * * @param throwable * {@link Throwable} */ public final void warn(final Object message, final Throwable throwable) { log.warn(message, throwable); } /** * WARN???<br /> * * @param message * */ public final void warn(final Object message) { log.warn(message); } /** * ERROR???<br /> * * @param message * * @param throwable * {@link Throwable} */ public final void error(final Object message, final Throwable throwable) { log.error(message, throwable); } /** * ERROR???<br /> * * @param message * */ public final void error(final Object message) { log.error(message); } /** * FATAL???<br /> * * @param message * * @param throwable * {@link Throwable} */ public final void fatal(final Object message, final Throwable throwable) { log.fatal(message, throwable); } /** * FATAL???<br /> * * @param message * */ public final void fatal(final Object message) { log.fatal(message); } /** * ???<br /> * * @param throwable * {@link Throwable} */ public final void log(final Throwable throwable) { error(throwable.getClass().getName() + " : " + throwable.getMessage(), throwable); } /** * ???<br /> * * @param messageCode * * @param args * */ public final void log(final String messageCode, final Object... args) { log(messageCode, null, args); } /** * ???<br /> * * @param messageCode * * @param throwable * {@link Throwable} * @param args * */ public final void log(final String messageCode, final Throwable throwable, final Object... args) { char messageType = messageCode.charAt(0); if (isEnabledFor(messageType)) { String message = MessageFormatter.getMessage(messageCode, args); switch (messageType) { case 'T': log.trace(message, throwable); break; case 'D': log.debug(message, throwable); break; case 'I': log.info(message, throwable); break; case 'W': log.warn(message, throwable); break; case 'E': log.error(message, throwable); break; case 'F': log.fatal(message, throwable); break; default: throw new IllegalArgumentException(String.valueOf(messageType)); } } } private boolean isEnabledFor(final char messageType) { switch (messageType) { case 'T': return log.isTraceEnabled(); case 'D': return log.isDebugEnabled(); case 'I': return log.isInfoEnabled(); case 'W': return log.isWarnEnabled(); case 'E': return log.isErrorEnabled(); case 'F': return log.isFatalEnabled(); default: throw new IllegalArgumentException(String.valueOf(messageType)); } } /** * ????<br /> * * @param obj * * @return */ public static final String getObjectDescription(final Object obj) { if (obj != null) { return obj.getClass().getName() + UrumaConstants.AT_MARK + Integer.toHexString(obj.hashCode()); } else { return "NULL"; } } }