how to configure log4j for Mybatis to print my SQL

前端 未结 3 1931
有刺的猬
有刺的猬 2021-02-05 12:24

My project is set up with SpringMVC+Mybatis+EXTJS4. This is my configuration:

# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL

# Global loggi         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 12:51

    You need to put slf4j-api-xxx.jar, slf4j-log4j12-xxx.jar, log4j-over-slf4j-xxx.jar, log4j-xxx.jar (replace xxx by the version, for example 1.6.3) in your classpath and in your src package, put the log4j.properties (see below).

    log4j.debug=true
    
    log4j.rootCategory=DEBUG
    
    ## uncoment when run in production ##
    #log4j.threshold=INFO
    
    # logger error
    log4j.logger.br.danilo.psc.exceptions=ERROR, psc-error
    log4j.appender.psc-error = org.apache.log4j.RollingFileAppender
    log4j.appender.psc-error.File=c:\\log-psc\\error\\log-error.log
    log4j.appender.psc-error.MaxFileSize=1200KB
    log4j.appender.psc-error.MaxBackupIndex=40
    log4j.appender.psc-error.layout=org.apache.log4j.PatternLayout
    log4j.appender.psc-error.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss,SSS} [%-5p] %m %n
    # end logger error
    
    # logger debug
    log4j.logger.br.danilo.psc=DEBUG, psc-debug
    log4j.appender.psc-debug = org.apache.log4j.ConsoleAppender
    log4j.appender.psc-debug.layout=org.apache.log4j.PatternLayout
    log4j.appender.psc-debug.layout.ConversionPattern=[%d{HH:mm:ss}] %-5p %c{3} %x - %m%n
    # end logger debug
    
    ### mybatis loggers ###
    log4j.logger.com.ibatis=DEBUG, psc-debug
    log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG, psc-debug
    log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG, psc-debug
    log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG, psc-debug
    
    # sql loggers
    log4j.logger.java.sql.Connection=DEBUG, psc-debug
    log4j.logger.java.sql.Statement=DEBUG, psc-debug
    log4j.logger.java.sql.PreparedStatement=DEBUG, psc-debug
    log4j.logger.java.sql.ResultSet=DEBUG, psc-debug
    

    with this .properties, you log all steps in mybatis and sql like open connection, fetch results, close connections and etc.

    cheers!

提交回复
热议问题