【问题标题】:How do I turn off spring logging in my JUnit tests?如何在我的 JUnit 测试中关闭 spring 日志记录?
【发布时间】:2013-06-21 16:19:21
【问题描述】:

我正在使用 Maven 3.0.3、JUnit 4.8.1、Spring 3.1.1.RELEASE 和 Hibernate 4.1.0.Final。运行 JUnit 测试时如何关闭 Spring 日志记录消息?我收到此类消息作为

Jun 21, 2013 10:54:34 AM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/admin/standards/import],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void org.mainco.subco.standards.mvc.StandardsImportController.standardsImportForm()

我的 src/test/resources 目录中有这个 log4j.properties 文件……

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=ERROR, stdout

# Hibernate logging options (INFO only shows startup messages)
log4j.logger.org.hibernate=ERROR

# Log JDBC bind parameter runtime arguments
log4j.logger.org.hibernate.type=ERROR

log4j.category.org.springframework=ERROR
log4j.category.net.sf.hibernate=ERROR

日志消息仍然大量涌现。这是我在 JUnit 测试中使用的应用程序上下文文件。知道如何消除噪音吗?

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                    http://www.springframework.org/schema/jdbc 
                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">

    <import resource="classpath:/META-INF/spring/applicationContext-user.xml"/>
    <import resource="classpath:/META-INF/spring/applicationContext-session.xml"/> 
    <import resource="classpath:/META-INF/spring/applicationContext-session-cache.xml"/>
    <import resource="classpath:/META-INF/spring/applicationContext-sbadmin-security.xml"/>
    <import resource="classpath:/META-INF/spring/applicationContext-sbadmin-mvc.xml"/> 

    <!-- Define test properties -->
    <util:properties id="testProps" location="classpath:test.properties" />
    <context:property-placeholder location="classpath:session.properties,classpath:ebook.properties,classpath:mysql_datasource.properties"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${test.mysql.dataSource.driverClassName}" />
        <property name="url" value="${test.mysql.dataSource.url}" />
        <property name="username" value="${test.mysql.db.user}" />
        <property name="password" value="${test.mysql.db.password}" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="org.mainco.subco" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaPropertyMap" ref="jpaPropertyMap" />
    </bean>

    <util:map id="jpaPropertyMap">
        <entry key="hibernate.show_sql" value="false" />
        <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <entry key="hibernate.hbm2ddl.auto" value="validate" /> 
        <entry key="hibernate.cache.region.factory_class"   value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
        <entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
        <entry key="hibernate.cache.use_second_level_cache" value="true" />
        <entry key="hibernate.cache.use_query_cache" value="false" />
        <entry key="hibernate.generate_statistics" value="true" />
    </util:map>

    <bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
       <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven />

    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath:truncate_tables.sql"/> 
        <jdbc:script location="classpath:db-test-data.sql"/>    
    </jdbc:initialize-database>

</beans>

【问题讨论】:

    标签: spring hibernate logging junit log4j


    【解决方案1】:

    尝试将log4j.category.org.springframework=ERROR 更改为log4j.logger.org.springframework=ERROR

    【讨论】:

    • 没有骰子,仍然从 org.springframework 获取 INFO 消息。我还验证了这是我的类路径中唯一的 log4j.properties 文件。
    【解决方案2】:

    有趣的是,我的 pom.xml 文件中有这两个依赖项...

                <dependency>
                        <groupId>org.apache.openejb</groupId>
                        <artifactId>commons-dbcp-all</artifactId>
                        <version>1.3</version>
                        <scope>test</scope>
                </dependency>
                <dependency>
                        <groupId>org.apache.openejb</groupId>
                        <artifactId>openejb-core</artifactId>
                        <version>4.0.0</version>
                        <scope>test</scope>
                </dependency>
    

    删除那些依赖项(反正我不需要),解决了我的日志记录问题。

    【讨论】:

      【解决方案3】:

      其实我发现Logback很简单:

      只需添加/更改这些:

      <logger name="org.hibernate" level="OFF" additivity="false">
          <appender-ref ref="CONSOLE"/>
      </logger>
      
      <logger name="org.springframework" level="OFF" additivity="false">
          <appender-ref ref="CONSOLE"/>
      </logger>
      

      虽然我没有测试过,但我认为在 log4j 中将级别设置为 OFF 也应该可以。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-26
        • 2017-09-17
        • 2018-03-28
        • 1970-01-01
        • 1970-01-01
        • 2017-08-03
        • 1970-01-01
        相关资源
        最近更新 更多