我已经配置了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
答案 0 :(得分:6)
log4j.logger.my_package.mapper=LOG_LEVEL
添加到配置文件
答案 1 :(得分:0)
您忘记了log4j.logger
中最后一个属性前面的log4j.properties
。