Re: How to change the log format?

171 views
Skip to first unread message

Eduardo Macarron

unread,
Oct 30, 2012, 1:18:09 PM10/30/12
to mybati...@googlegroups.com
I am afraid not.

2012/10/30 jim <relym...@gmail.com>
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

jim

unread,
Oct 30, 2012, 6:04:08 PM10/30/12
to mybati...@googlegroups.com
Thanks for your reply.

If I modify Mybatis source code, How can I simply add a session_id to the log because I need to know which request(session_id) print the log?

I want the log format like so.

session_id : 1A38559F2F0577DF9F75570E627416AE ==>  Preparing: select count(*) from Abc where a = ? and b = ? and c = ?
session_id : 1A38559F2F0577DF9F75570E627416AE ==> Parameters: 1(Integer), 1(Integer), service(String)

Any suggestions?

Thanks,
Jim

Steve Hill

unread,
Oct 30, 2012, 8:19:54 PM10/30/12
to mybati...@googlegroups.com
You could when the request comes in add the sessionid to thread local storage, and in the modified mybatis class, simply pull it out.  You should make sure before the request is over you remove it too so other threads will not end up with the wrong value

I also believe some containers allow you to specify a prefix to any log statements although I am not sure how they work.

Thanks!
Steve.

Sent from my iPhone

jim

unread,
Oct 30, 2012, 9:00:07 PM10/30/12
to mybati...@googlegroups.com
Thanks for your reply.

I'm using Spring mvc and slf4j + log4j.
I think the following also is a good idea.


Jim.

Eduardo Macarron

unread,
Oct 31, 2012, 1:46:41 AM10/31/12
to mybati...@googlegroups.com
If you are going to do your own version, the easiest way would be printing the current jdbc connection instead of the session.

Note that a session holds a connection so they are pretty much the same thing.

Poitras Christian

unread,
Oct 31, 2012, 8:37:58 AM10/31/12
to mybati...@googlegroups.com

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?

jim

unread,
Oct 31, 2012, 5:55:37 PM10/31/12
to mybati...@googlegroups.com
Thank you all.

I've solved it using log4j's MDC.

Jim

Ikchan Sim

unread,
Oct 31, 2012, 8:33:50 PM10/31/12
to mybati...@googlegroups.com
hi. 

used plugin


1. CustomPlugin.javal
==================================================================

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 SIM
Software Architeture.
E-mail:plusp...@gmail.com
H.P : +82 - 010 - 8242 - 2727
Nothing in the world can take the place of persistence.
==================================

jim

unread,
Oct 31, 2012, 11:44:53 PM10/31/12
to mybati...@googlegroups.com
Thanks for your reply.

That's a good idea.

Jim
==================================
IkChan SIM
Software Architeture.
E-mail:p...@gmail.com
Reply all
Reply to author
Forward
0 new messages