为什么mybatis强制打印log4j DEBUG信息到控制台
Why mybatis force prints log4j DEBUG info to the console
我已经配置mybatis 使用log4j 进行日志记录。我将 log4j 配置为仅打印警告信息。但是当我运行它时,mybatis 会向控制台打印大量 sql 调试消息:
<http-bio-80-exec-1 10:08:54,969> <org.apache.ibatis.logging.jdbc.BaseJdbcLogger:139> DEBUG - ==> Preparing: select count(*) from accounts
<http-bio-80-exec-1 10:08:54,993> <org.apache.ibatis.logging.jdbc.BaseJdbcLogger:139> DEBUG - ==> Parameters:
<http-bio-80-exec-1 10:08:55,093> <org.apache.ibatis.logging.jdbc.BaseJdbcLogger:139> DEBUG - <== Total: 1
mybatis.xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//xml.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
</configuration>
log4j.properties 文件:
# appenders
logFormat=<%t %d{HH:mm:ss,SSS}> <%C:%L> %-5p - %m%n
log4j.appender.appender_console=org.apache.log4j.ConsoleAppender
log4j.appender.appender_console.layout=org.apache.log4j.PatternLayout
log4j.appender.appender_console.layout.ConversionPattern=${logFormat}
log4j.appender.appender_file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.appender_file.layout=org.apache.log4j.PatternLayout
log4j.appender.appender_file.layout.ConversionPattern=${logFormat}
log4j.appender.appender_file.File=${webRootDir}/../../../logs/log
log4j.appender.appender_file.DatePattern='-'yyyy.MM.dd'.log'
# config for all packages
log4j.rootLogger=WARN, appender_console, appender_file
# config for specific packages
log4j.logger.my_package=DEBUG
log4j.logger.com.ibatis=WARN
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=WARN
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=WARN
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=WARN
log4j.logger.java.sql.Connection=WARN
log4j.logger.java.sql.Statement=WARN
log4j.logger.java.sql.PreparedStatement=WARN
log4j.logger.java.sql.ResultSet=WARN
log4j.logger.org.apache.ibatis.logging.jdbc.BaseJdbcLogger=WARN
applicationContext.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- for springmvc & mybatis source scanning -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="***" />
</bean>
<context:component-scan base-package="***" />
<!-- for database -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:config/jdbc.properties" />
<bean id="dbcp2_mysql" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dbcp2_mysql.driverClassName}" />
<property name="url" value="${dbcp2_mysql.url}" />
<property name="username" value="${dbcp2_mysql.username}" />
<property name="password" value="${dbcp2_mysql.password}" />
<!--<property name="initialSize" value="${dbcp2_mysql.initialSize}" />-->
<!--<property name="maxIdle" value="${dbcp2_mysql.maxIdle}" />-->
<!--<property name="minIdle" value="${dbcp2_mysql.minIdle}" />-->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbcp2_mysql" />
<property name="mapperLocations" value="classpath:***" />
<property name="configLocation" value="classpath:config/mybatis.xml"></property>
<property name="typeAliases" value="org.springframework.util.LinkedCaseInsensitiveMap" />
<property name="transactionFactory">
<bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />
</property>
</bean>
</beans>
PS:我用的是intellij IDEA
您忘记了 log4j.properties
中最后一个 属性 前面的 log4j.logger
。
我自己解决了这个问题
我应该将 log4j.logger.my_package.mapper=LOG_LEVEL
添加到配置文件
我已经配置mybatis 使用log4j 进行日志记录。我将 log4j 配置为仅打印警告信息。但是当我运行它时,mybatis 会向控制台打印大量 sql 调试消息:
<http-bio-80-exec-1 10:08:54,969> <org.apache.ibatis.logging.jdbc.BaseJdbcLogger:139> DEBUG - ==> Preparing: select count(*) from accounts
<http-bio-80-exec-1 10:08:54,993> <org.apache.ibatis.logging.jdbc.BaseJdbcLogger:139> DEBUG - ==> Parameters:
<http-bio-80-exec-1 10:08:55,093> <org.apache.ibatis.logging.jdbc.BaseJdbcLogger:139> DEBUG - <== Total: 1
mybatis.xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//xml.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
</configuration>
log4j.properties 文件:
# appenders
logFormat=<%t %d{HH:mm:ss,SSS}> <%C:%L> %-5p - %m%n
log4j.appender.appender_console=org.apache.log4j.ConsoleAppender
log4j.appender.appender_console.layout=org.apache.log4j.PatternLayout
log4j.appender.appender_console.layout.ConversionPattern=${logFormat}
log4j.appender.appender_file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.appender_file.layout=org.apache.log4j.PatternLayout
log4j.appender.appender_file.layout.ConversionPattern=${logFormat}
log4j.appender.appender_file.File=${webRootDir}/../../../logs/log
log4j.appender.appender_file.DatePattern='-'yyyy.MM.dd'.log'
# config for all packages
log4j.rootLogger=WARN, appender_console, appender_file
# config for specific packages
log4j.logger.my_package=DEBUG
log4j.logger.com.ibatis=WARN
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=WARN
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=WARN
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=WARN
log4j.logger.java.sql.Connection=WARN
log4j.logger.java.sql.Statement=WARN
log4j.logger.java.sql.PreparedStatement=WARN
log4j.logger.java.sql.ResultSet=WARN
log4j.logger.org.apache.ibatis.logging.jdbc.BaseJdbcLogger=WARN
applicationContext.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- for springmvc & mybatis source scanning -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="***" />
</bean>
<context:component-scan base-package="***" />
<!-- for database -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:config/jdbc.properties" />
<bean id="dbcp2_mysql" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dbcp2_mysql.driverClassName}" />
<property name="url" value="${dbcp2_mysql.url}" />
<property name="username" value="${dbcp2_mysql.username}" />
<property name="password" value="${dbcp2_mysql.password}" />
<!--<property name="initialSize" value="${dbcp2_mysql.initialSize}" />-->
<!--<property name="maxIdle" value="${dbcp2_mysql.maxIdle}" />-->
<!--<property name="minIdle" value="${dbcp2_mysql.minIdle}" />-->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbcp2_mysql" />
<property name="mapperLocations" value="classpath:***" />
<property name="configLocation" value="classpath:config/mybatis.xml"></property>
<property name="typeAliases" value="org.springframework.util.LinkedCaseInsensitiveMap" />
<property name="transactionFactory">
<bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />
</property>
</bean>
</beans>
PS:我用的是intellij IDEA
您忘记了 log4j.properties
中最后一个 属性 前面的 log4j.logger
。
我自己解决了这个问题
我应该将 log4j.logger.my_package.mapper=LOG_LEVEL
添加到配置文件