how to configure log4j for Mybatis to print my SQL

前端 未结 3 1930
有刺的猬
有刺的猬 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:43

    I encountered a similar problem. The following code helped me (i've used it in the mybatis class configuration):

    org.apache.ibatis.logging.LogFactory.useLog4JLogging(); 
    

    May cause problems become other logging system in your classpath.

    0 讨论(0)
  • 2021-02-05 12:49

    Jaradinor's answer will probably work. But, I think the underlying issue is due to how the MyBatis LogFactory class is implemented. In a static block, it attempts to load slf4j, then commmons-logging, then log4j. So, if you have commons-logging in your classpath it will use that. Since you are using Spring, you probably do have commons-logging.

    It's because of issues like this I have moved to slf4j. Use slf4j-log4j12 to have slf4j call to log4j. Use jcl-over-slf4j to route all the Spring (and other) commons-logging to slf4j (and then to log4j). Make sure you exclude the 'real' commons-logging jar file from your classpath when you do this - maybe with an <exclude> if you are using Maven.

    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题