java 如何配置 log4j 来记录来自 mybatis 的 sql 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13448260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:46:18  来源:igfitidea点击:

how to configure log4j to log sql statements from mybatis

javagroovylog4jmybatis

提问by AabinGunz

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

我正在使用 MyBatis3 我需要一种方法来将我所有的选择、插入、更新语句记录到我的 log4j 日志文件中。

Here is my log4j file. Please help

这是我的 log4j 文件。请帮忙

# 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

采纳答案by AabinGunz

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)

为了记录 sql 语句,请下载 Simple Logging Facade for Java(在此处下载 slf4j

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

除了常规的 mybatis、odbc 和 oracle jar 之外,在我的类路径中添加了以下内容

  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
  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

注意:xxxx 是这里的合适版本

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

并在我的 log4j 中附加以下几行(请参阅我的问题)

# 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

这是我用于测试的 Groovy 类示例

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")

    }   

}

回答by Konstantin V. Salikhov

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.

您可以在此处查看 Log4J 配置信息。简而言之 - 您需要在映射器或映射器包或特定映射器方法上将 Log4J 日志级别设置为 DEBUG 或 TRACE。例如log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE。TRACE 将打印 SQL、参数和结果集,而 DEBUG 将仅打印 SQL 和参数。