CommonsLoggingSessionLog.java 6.9 KB
Newer Older
A
Arjen Poutsma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
/*
 * Copyright 2002-2007 the original author or authors.
 *
 * 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.springframework.orm.toplink.support;

import java.lang.reflect.Method;

import oracle.toplink.internal.databaseaccess.Accessor;
import oracle.toplink.logging.AbstractSessionLog;
import oracle.toplink.logging.SessionLogEntry;
import oracle.toplink.publicinterface.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.ReflectionUtils;

/**
 * TopLink 10.1.3+ SessionLog implementation that logs through Commons Logging.
 *
 * <p>The namespace used is "oracle.toplink.xxx", with the latter part being
 * the TopLink log category ("sql"/"transaction"/etc). In case of no category
 * given, "session" will be used as default. This allows for fine-grained
 * filtering of log messages, for example through Log4J configuration.
 *
 * <p>Maps TopLink's SEVERE level to CL ERROR, TopLink's WARNING to CL WARN,
 * TopLink's INFO to CL INFO, TopLink's CONFIG/FINE/FINER to CL DEBUG,
 * and TopLink's FINEST to CL TRACE. This results in common CL log behavior:
 * INFO logging only at startup; operation logging at DEBUG level. Debug logging
 * can be further filtered according to categories: for example, activate Log4J
 * DEBUG logging for category "oracle.toplink.sql" to see the generated SQL.
 *
 * <p><b>Note:</b> This implementation will only work on TopLink 10.1.3 or higher,
 * as it is built against TopLink's new SessionLog facilities in the
 * <code>oracle.toplink.logging</code> package, supporting log categories.
 *
 * @author Juergen Hoeller
 * @since 1.2
 * @see CommonsLoggingSessionLog904
 * @see oracle.toplink.logging.JavaLog
 * @see org.springframework.orm.toplink.LocalSessionFactoryBean#setSessionLog
 */
public class CommonsLoggingSessionLog extends AbstractSessionLog {

	public static final String NAMESPACE_PREFIX = "oracle.toplink.";

	public static final String DEFAULT_NAMESPACE = "session";

	public static final String DEFAULT_SEPARATOR = "--";


	private static Method getSessionMethod;

	private static Method getExceptionMethod;

	static {
		try {
			getSessionMethod = SessionLogEntry.class.getMethod("getSession", new Class[0]);
		}
		catch (NoSuchMethodException ex) {
			throw new IllegalStateException("Could not find method SessionLogEntry.getSession()");
		}
		try {
			getExceptionMethod = SessionLogEntry.class.getMethod("getException", new Class[0]);
		}
		catch (NoSuchMethodException ex) {
			throw new IllegalStateException("Could not find method SessionLogEntry.getException()");
		}
	}


	private String separator = DEFAULT_SEPARATOR;


	/**
	 * Specify the separator between TopLink's supplemental details
	 * (session, connection) and the log message itself. Default is "--".
	 */
	public void setSeparator(String separator) {
		this.separator = separator;
	}

	/**
	 * Return the separator between TopLink's supplemental details
	 * (session, connection) and the log message itself. Default is "--".
	 */
	public String getSeparator() {
		return this.separator;
	}


	public void log(SessionLogEntry entry) {
		Log logger = LogFactory.getLog(getCategory(entry));
		switch (entry.getLevel()) {
			case SEVERE:
				if (logger.isErrorEnabled()) {
					if (entry.hasException()) {
						logger.error(getMessageString(entry), getException(entry));
					}
					else {
						logger.error(getMessageString(entry));
					}
				}
				break;
			case WARNING:
				if (logger.isWarnEnabled()) {
					if (entry.hasException()) {
						logger.warn(getMessageString(entry), getException(entry));
					}
					else {
						logger.warn(getMessageString(entry));
					}
				}
				break;
			case INFO:
				if (logger.isInfoEnabled()) {
					if (entry.hasException()) {
						logger.info(getMessageString(entry), getException(entry));
					}
					else {
						logger.info(getMessageString(entry));
					}
				}
				break;
			case CONFIG:
			case FINE:
			case FINER:
				if (logger.isDebugEnabled()) {
					if (entry.hasException()) {
						logger.debug(getMessageString(entry), getException(entry));
					}
					else {
						logger.debug(getMessageString(entry));
					}
				}
				break;
			case FINEST:
				if (logger.isTraceEnabled()) {
					if (entry.hasException()) {
						logger.trace(getMessageString(entry), getException(entry));
					}
					else {
						logger.trace(getMessageString(entry));
					}
				}
				break;
		}
	}

	/**
	 * Determine the log category for the given log entry.
	 * <p>If the entry carries a name space value, it will be appended
	 * to the "oracle.toplink." prefix; else, "oracle.toplink.session"
	 * will be used.
	 */
	protected String getCategory(SessionLogEntry entry) {
		String namespace = entry.getNameSpace();
		return NAMESPACE_PREFIX + (namespace != null ? namespace : DEFAULT_NAMESPACE);
	}

	/**
	 * Build the message String for the given log entry, including the
	 * supplemental details (session, connection) and the formatted message.
	 * @see #getSessionString(oracle.toplink.sessions.Session)
	 * @see #getConnectionString(oracle.toplink.internal.databaseaccess.Accessor)
	 * @see #formatMessage(oracle.toplink.logging.SessionLogEntry)
	 * @see #getSeparator()
	 */
	protected String getMessageString(SessionLogEntry entry) {
		StringBuffer buf = new StringBuffer();
		Session session = getSession(entry);
		if (session != null) {
			buf.append(getSessionString(session));
			buf.append(getSeparator());
		}
		Accessor connection = entry.getConnection();
		if (connection != null) {
			buf.append(getConnectionString(connection));
			buf.append(getSeparator());
		}
		buf.append(formatMessage(entry));
		return buf.toString();
	}

	/**
	 * Extract the exception from the given log entry.
	 * <p>The default implementation calls <code>SessionLogEntry.getSession</code>
	 * via reflection: The return type varies between TopLink 10.1.3 and 11
	 * (<code>Session</code> vs <code>AbstractSession</code>, respectively).
	 */
	protected Session getSession(SessionLogEntry entry) {
		return (Session) ReflectionUtils.invokeMethod(getSessionMethod, entry);
	}

	/**
	 * Extract the exception from the given log entry.
	 * <p>The default implementation calls <code>SessionLogEntry.getException</code>
	 * via reflection: The return type varies between TopLink 9.0.4 and 10.1.3
	 * (<code>Exception</code> vs <code>Throwable</code>, respectively).
	 */
	protected Throwable getException(SessionLogEntry entry) {
		return (Throwable) ReflectionUtils.invokeMethod(getExceptionMethod, entry);
	}

}