1 16 17 package org.springframework.orm.toplink.support; 18 19 import java.lang.reflect.Method ; 20 21 import oracle.toplink.logging.AbstractSessionLog; 22 import oracle.toplink.logging.SessionLogEntry; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 51 public class CommonsLoggingSessionLog extends AbstractSessionLog { 52 53 public static final String NAMESPACE_PREFIX = "oracle.toplink."; 54 55 public static final String DEFAULT_NAMESPACE = "session"; 56 57 public static final String DEFAULT_SEPARATOR = "--"; 58 59 60 private static Method getExceptionMethod; 61 62 static { 63 try { 64 getExceptionMethod = SessionLogEntry.class.getMethod("getException", new Class [0]); 65 } 66 catch (NoSuchMethodException ex) { 67 throw new IllegalStateException ("Could not find method SessionLogEntry.getException()"); 68 } 69 } 70 71 72 private String separator = DEFAULT_SEPARATOR; 73 74 75 79 public void setSeparator(String separator) { 80 this.separator = separator; 81 } 82 83 87 public String getSeparator() { 88 return separator; 89 } 90 91 92 public void log(SessionLogEntry entry) { 93 Log logger = LogFactory.getLog(getCategory(entry)); 94 switch (entry.getLevel()) { 95 case SEVERE: 96 if (logger.isErrorEnabled()) { 97 if (entry.hasException()) { 98 logger.error(getMessageString(entry), getException(entry)); 99 } 100 else { 101 logger.error(getMessageString(entry)); 102 } 103 } 104 break; 105 case WARNING: 106 if (logger.isWarnEnabled()) { 107 if (entry.hasException()) { 108 logger.warn(getMessageString(entry), getException(entry)); 109 } 110 else { 111 logger.warn(getMessageString(entry)); 112 } 113 } 114 break; 115 case INFO: 116 if (logger.isInfoEnabled()) { 117 if (entry.hasException()) { 118 logger.info(getMessageString(entry), getException(entry)); 119 } 120 else { 121 logger.info(getMessageString(entry)); 122 } 123 } 124 break; 125 case CONFIG: 126 case FINE: 127 case FINER: 128 if (logger.isDebugEnabled()) { 129 if (entry.hasException()) { 130 logger.debug(getMessageString(entry), getException(entry)); 131 } 132 else { 133 logger.debug(getMessageString(entry)); 134 } 135 } 136 break; 137 case FINEST: 138 if (logger.isTraceEnabled()) { 139 if (entry.hasException()) { 140 logger.trace(getMessageString(entry), getException(entry)); 141 } 142 else { 143 logger.trace(getMessageString(entry)); 144 } 145 } 146 break; 147 } 148 } 149 150 156 protected String getCategory(SessionLogEntry entry) { 157 if (entry.getNameSpace() != null) { 158 return NAMESPACE_PREFIX + entry.getNameSpace(); 159 } 160 return NAMESPACE_PREFIX + DEFAULT_NAMESPACE; 161 } 162 163 171 protected String getMessageString(SessionLogEntry entry) { 172 StringBuffer buf = new StringBuffer (); 173 if (entry.getSession() != null) { 174 buf.append(getSessionString(entry.getSession())); 175 buf.append(getSeparator()); 176 } 177 if (entry.getConnection() != null) { 178 buf.append(getConnectionString(entry.getConnection())); 179 buf.append(getSeparator()); 180 } 181 buf.append(formatMessage(entry)); 182 return buf.toString(); 183 } 184 185 193 protected Throwable getException(SessionLogEntry entry) { 194 try { 195 return (Throwable ) getExceptionMethod.invoke(entry, new Object [0]); 196 } 197 catch (Exception ex) { 198 throw new IllegalStateException ( 199 "Could not invoke method SessionLogEntry.getException(): " + ex.getMessage()); 200 } 201 } 202 203 204 211 public void log(oracle.toplink.sessions.SessionLogEntry entry) { 212 throw new UnsupportedOperationException ("oracle.toplink.sessions.SessionLogEntry not supported"); 213 } 214 215 } 216 | Popular Tags |