헤르메스 LIFE

[iBatis] iBatis의 쿼리로그 변경 본문

Spring Framework

[iBatis] iBatis의 쿼리로그 변경

헤르메스의날개 2010. 10. 27. 10:03
728x90

ibatis-2.3.4.726b.jar
다운로드

iBatis-2.3.4.726 버젼의 수정입니다.

1. com.ibatis.common.jdbc.logging.LoggableStatement 파일 생성

package com.ibatis.common.jdbc.logging;  import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer;  @SuppressWarnings("unchecked") public class LoggableStatement {          public LoggableStatement(String sql, List params) throws Exception {         sqlTemplate = sql;         parameterValues = new ArrayList();         parameterValues = (ArrayList) params;     }          public String getQueryString() {         StringBuffer buf = new StringBuffer();         int qMarkCount = 0;         for(StringTokenizer tok = new StringTokenizer((new StringBuilder(String.valueOf(sqlTemplate))).append(" ").toString(), "?"); tok                 .hasMoreTokens();) {             String oneChunk = tok.nextToken();             buf.append(oneChunk);             try {                 Object value;                 if(parameterValues.size() > qMarkCount) value = parameterValues.get(qMarkCount++);                 else if(tok.hasMoreTokens()) value = null;                 else value = "";                 if(value == null || "".equals(value)) buf.append((new StringBuilder()).append(value).toString());                 else buf.append((new StringBuilder("'")).append(value).append("'").toString());             } catch(Throwable e) {                 buf.append((new StringBuilder("ERROR WHEN PRODUCING QUERY STRING FOR LOG.")).append(e.toString()).toString());             }         }                  sqlTemplate = buf.toString();         sqlTemplate = sqlTemplate.replaceAll("\n[\t| ]*\n", "\n");         sqlTemplate = sqlTemplate.replaceAll("\n[\t| ]*\n", "\n");         sqlTemplate = sqlTemplate.replaceAll("\n[\t| ]*\n", "\n");         return sqlTemplate;     }          private ArrayList parameterValues;     private String    sqlTemplate; } 

2. com.ibatis.common.jdbc.logging.BaseLogProxy 파일 수정

    // Method 추가     protected List getValueList() {         return columnValues;     } 

3. com.ibatis.common.jdbc.logging.PreparedStatementLogProxy 파일 수정

  public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {     try {       if (EXECUTE_METHODS.contains(method.getName())) {         if (log.isDebugEnabled()) {           //log.debug("{pstm-" + id + "} Executing Statement: " + removeBreakingWhitespace(sql));           //log.debug("{pstm-" + id + "} Parameters: " + getValueString());           //log.debug("{pstm-" + id + "} Types: " + getTypeString());             LoggableStatement ls = new LoggableStatement(sql, getValueList());             log.debug("************************************** REQUEST PARAMETER STATEMENT START ******************************");             log.debug(ls.getQueryString());             log.debug("*************************************** REQUEST PARAMETER STATEMENT END *******************************\n");         } 

4. com.ibatis.common.jdbc.logging.StatementLogProxy 파일 수정

  public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {     try {       if (EXECUTE_METHODS.contains(method.getName())) {         if (log.isDebugEnabled()) {           //log.debug("{stmt-" + id + "} Statement: " + removeBreakingWhitespace((String) params[0]));           log.debug("{stmt-" + id + "} Statement: " + ((String) params[0]));         } 

5. com.ibatis.common.jdbc.logging.ConnectionLogProxy 파일 수정

  public Object invoke(Object proxy, Method method, Object[] params)       throws Throwable {     try {       if ("prepareStatement".equals(method.getName())) {         if (log.isDebugEnabled()) {           //log.debug("{conn-" + id + "} Preparing Statement: " + removeBreakingWhitespace((String) params[0]));         }         PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);         stmt = PreparedStatementLogProxy.newInstance(stmt, (String) params[0]);         return stmt;       } else if ("prepareCall".equals(method.getName())) {         if (log.isDebugEnabled()) {           //log.debug("{conn-" + id + "} Preparing Call: " + removeBreakingWhitespace((String) params[0]));         }         PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);         stmt = PreparedStatementLogProxy.newInstance(stmt, (String) params[0]);         return stmt;       } 

6. com.ibatis.sqlmap.engine.mapping.sql.stat.StaticSql 파일 수정

  public StaticSql(String sqlStatement) {     //this.sqlStatement = sqlStatement.replace('\r', ' ').replace('\n', ' ');     this.sqlStatement = sqlStatement;   } 

7. com.ibatis.sqlmap.engine.mapping.sql.SqlText 파일 수정

  public void setText(String text) {     //this.text = text.replace('\r', ' ').replace('\n', ' ').replace('\t', ' ');     this.text = text;     this.isWhiteSpace = text.trim().length() == 0;   } 

8.log4j.xml 설정

    <logger name="java.sql" additivity="false" >
    	<level value="DEBUG" />
        <appender-ref ref="CONSOLE"/>
    </logger>
728x90