Hi,Is there any way to change the log format without modifying Mybatis source code?I want to remove the prefix text like ===> Preparing: and ===> Parameters:The following messages are the original debug messages.ex.)==> Preparing: select count(*) from Abc where a = ? and b = ? and c = ?==> Parameters: 1(Integer), 1(Integer), service(String)
Thanks,Jim
If you use Log4J, you can use NDC.
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html
If you want to use NDC for all MyBatis logs, you may be interested to use a plugin to push and pop contexts rather than modify the MyBatis source.
Christian
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de Eduardo Macarron
Envoyé : October-31-12 1:47 AM
À : mybati...@googlegroups.com
Objet : Re: How to change the log format?
|
package chapter3.org.mybatis.custom;
import java.sql.Statement; import java.util.Properties; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.ResultHandler;
@Intercepts({@Signature(type=StatementHandler.class, method="query", args={Statement.class, ResultHandler.class})}) public class CustomPlugin implements Interceptor { /** * Print statement and parameters before execute query. */ @Override public Object intercept(Invocation invocation) throws Throwable { // 'StatementHandler' 플러그인 인터페이스의 'getBoundSql' 메소드를 이용해 // 'PreparedStatement' 문장을 가져온다. |
|
StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); String query = statementHandler.getBoundSql().getSql(); |
|
// 'StatementHandler' 플러그인 인터페이스의 'getParameterHandler' 메소드를 이용해 // 'ParameterObject'를 가져온다. |
|
String parameters = statementHandler.getParameterHandler().getParameterObject() != null ? statementHandler.getParameterHandler().getParameterObject().toString() : ""; |
|
// 호출되는 쿼리 구문과 인자를 콘솔에 출력한다. |
|
System.out.println("Query: \r\n " + query); System.out.println("Parameters: \r\n " + parameters); |
|
// 해당 플러그인 인터페이스를 구현한 클래스의 메소드를 호출한다. // 예를들면, 각 플러그인 인터페이스를 구현한 CachingExecutor, DefaultParameterHandler, // FastResultSetHandler, RoutingStatementHandler 클래스의 해당 메소드를 호출한다. |
|
return invocation.proceed(); |
|
}
@Override public Object plugin(Object target) { // 'Executor', 'ParameterHandler', 'ResultSetHandler', 'StatementHandler' // 플러그인 인터페이스를 구현한 클래스 순서로 호출된다. 즉, 플러그인 총갯수인 4회 호출된다. return Plugin.wrap(target, this); }
@Override public void setProperties(Properties properties) { } }
2. config-mybatis.xml ================================================================== <plugins> <plugin interceptor="chapter3.org.mybatis.custom.CustomPlugin" /> </plugins>================================================================== 3. Print log (Log4j log and Custom Plugin print log) ================================================================== DEBUG [main] - ooo Using Connection [oracle.jdbc.driver.T4CConnection@67a9c2] DEBUG [main] - ==> Preparing: SELECT NO, ID, NAME, NOTE, STATUS FROM WRITER WHERE NO = ? Query: SELECT NO, ID, NAME, NOTE, STATUS FROM WRITER WHERE NO = ? Parameters: Writer [no=1, id=null, name=null, note=null, status=null] DEBUG [main] - ==> Parameters: 1(Integer) TRACE [main] - <== Columns: NO, ID, NAME, NOTE, STATUS TRACE [main] - <== Row: 1, Clinton, Clinton Begin, Team Leader, Y ==================================================================
I hope this helps.... |
==================================IkChan SIMSoftware Architeture.
E-mail:p...@gmail.com
H.P : +82 - 010 - 8242 - 2727