Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why mybatis force prints log4j DEBUG info to the console

I've already configured mybatis to use log4j for logging. And I configured log4j to only print warning info. But when I run it, mybatis prints lots of sql debug messages to the console:

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

<?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 file:

# 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 file:

<?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: I am using intellij IDEA

like image 658
Chip Zhang Avatar asked Jan 05 '15 02:01

Chip Zhang


People also ask

How do I enable debug mode in log4j?

You need to set the logger level to the lowest you want to display. For example, if you want to display DEBUG messages, you need to set the logger level to DEBUG. The Apache log4j manual has a section on Configuration. Show activity on this post.

Which log4j component is responsible for logging messages?

log4j has three main components: loggers: Responsible for capturing logging information. appenders: Responsible for publishing logging information to various preferred destinations. layouts: Responsible for formatting logging information in different styles.


1 Answers

I solved the problem myself I should add log4j.logger.my_package.mapper=LOG_LEVEL to the config file

like image 50
Chip Zhang Avatar answered Sep 20 '22 12:09

Chip Zhang