3

I am using MyBatis3 I need a way to log all my select, insert, update statements to my log4j log file.

Here is my log4j file. Please help

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=test.log
log4j.appender.file.MaxFileSize=2MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

2 Answers 2

2

You can see the Log4J configuration info here. In short - you need to set Log4J loglevel to DEBUG or TRACE on your mapper or mapper package or specific mapper method. E.g. log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE. TRACE will print SQL, params and resultsets, and DEBUG will print SQL and params only.

Sign up to request clarification or add additional context in comments.

Comments

2

I found a way, putting in so others can benefit too

In order to log sql statements download Simple Logging Facade for Java (download slf4j here)

Added the following to my classpath, apart from regular mybatis, odbc and oracle jars

  1. log4j-xxxx.jar
  2. log4j-over-slf4j-xxxx.jar
  3. log4j-rolling-appender.jar
  4. slf4j-api-xxxx.jar
  5. slf-log4j12-xxxx.jar

Note: xxxx is appropriate version here

and append the following lines in my log4j (see my question)

# logger debug
log4j.logger.test.Log4jTestMyBatis=DEBUG, convert
log4j.appender.convert = org.apache.log4j.ConsoleAppender
log4j.appender.convert.layout=org.apache.log4j.PatternLayout
log4j.appender.convert.layout.ConversionPattern=[%d{HH:mm:ss}] %-5p %c{3} %x - %m%n
# end logger debug

# mybatis loggers #
log4j.logger.com.ibatis=DEBUG, convert
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG, convert
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG, convert
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG, convert

Here is a Groovy class example that I used for testing

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.PropertyConfigurator;

import com.abc.db.ConfigInfo;
import com.abc.db.ConfigInfoExample;
import com.abc.db.client.ConfigInfoMapper;
import com.abc.db.init.DatabaseConnectivity;



class Log4jTestMyBatis {

    static Logger logger = LoggerFactory.getLogger(Log4jTestMyBatis.class)

    static main(args) {
        PropertyConfigurator.configure(Log4jTestMyBatis.class.getResource("log4j.properties"));

        DatabaseConnectivity.init()
        SqlSession newABCSession = DatabaseConnectivity.getNewABCSessionFactory().openSession()

        ConfigInfoMapper mapper = newABCSession.getMapper(ConfigInfoMapper.class)
        ConfigInfoExample qExample = new ConfigInfoExample()
        qExample.createCriteria().andProjectIdEqualTo("0-12170")
        List<ConfigInfo> ctlist = mapper.selectByExample(qExample)

        logger.debug(ctlist.get(0).getCfgName())

        newABCSession.close()
        logger.debug("debug")

    }   

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.