配置Unitils日志

时间:2017-02-27 08:24:18

标签: java spring logback unitils

我想配置Unitils的日志级别,因为我不想在运行测试时显示信息跟踪。 Unitils通过Spring运行测试:

@RunWith(UnitilsJUnit4TestClassRunner.class)
@SpringApplicationContext("testConfig.xml")
@Transactional(value = TransactionMode.ROLLBACK)
public class ContractorServiceIT

我使用logback配置日志:

<property name="pathfile" value="${catalina.base}/logs" />

<appender name="FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>${pathfile}/health-safety-web-test.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>${pathfile}/archived/health-safety-web-test.%d{yyyy-MM-dd}.log</FileNamePattern>
    </rollingPolicy>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{100} - %msg%n</pattern>
    </encoder>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{100} - %msg%n</pattern>
    </encoder>
</appender>

<logger name="org.springframework" level="WARN"/>
<logger name="org.hibernate" level="WARN" />
<logger name="org.hibernate.cache" level="ERROR" />
<logger name="org.hibernate.SQL" level="WARN"/>
<logger name="net.sf.ehcache" level="WARN"/>
<logger name="com.vaadin.spring" level="WARN"/>
<logger name="org.vaadin.spring" level="WARN"/> 
<logger name="org.atmosphere" level="WARN"/>
<logger name="org.dbunit" level="ERROR"/>
<logger name="org.unitils" level="ERROR"/>
<logger name="es.cic" level="INFO"/>

<root level="WARN">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
</root>

当我运行测试时,在文件health-safety-web-test.log中没有显示Info日志,但eclipse和console(mvn install)显示这些日志。

这是我不想显示的日志示例:

feb 27, 2017 9:07:38 AM org.unitils.core.ConfigurationLoader loadCustomConfiguration
ADVERTENCIA: No custom configuration file unitils.properties found.
feb 27, 2017 9:07:38 AM es.cic.viesgo.core.commons.testing.PropertiesDataSourceFactory createDataSource
INFORMACIÓN: You are running with Spring Security Core 4.0.4.RELEASE
feb 27, 2017 9:07:39 AM org.springframework.security.config.SecurityNamespaceHandler <init>
INFORMACIÓN: Spring Security 'config' module version is 4.0.4.RELEASE
feb 27, 2017 9:07:39 AM org.springframework.security.config.method.GlobalMethodSecurityBeanDefinitionParser parse

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题在于Unitils使用commons-logging打印日志,我在项目中使用SLF4J。我解决了改变SLF4J

的依赖性的问题
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
</dependency>

现在可以在commons-logging文件中配置Unitils日志(以及使用logback.xml的任何其他libreries)。