How to configure log4j to register SQL queries from mybatis

I am using MyBatis3 I need a way to register all my select, insert, update commands in the 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 
+4
source share
2 answers

I found a way by interposing so that others can also win.

To register sql queries, download Simple Logging Facade for Java ( download slf4j here )

Added the following to my classpath besides the usual mybatis, odbc and oracle jars

  • log4j-xxxx.jar
  • log4j-over-SLF4J-xxxx.jar
  • log4j rolling appender.jar
  • SLF4J-api-xxxx.jar
  • SLF-log4j12-xxxx.jar

Note : xxxx is a suitable version here

and add the following lines to 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 an example Groovy class that I used to test

 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") } } 
+3
source

Here you can see Log4J configuration information here . In short, you need to set Log4J loglevel to DEBUG or TRACE on your cartographer or card package or specific matching method. For instance. log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE . TRACE will print SQL, params and resultsets, and DEBUG will print only SQL and parameters.

+2
source

Source: https://habr.com/ru/post/1446870/


All Articles