配置 Unitils 日志

Configure Unitils Log

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

使用 Unitils 进行测试 运行
@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 中没有出现信息日志,但是 eclipse 和控制台(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

有什么想法吗?

问题是 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>

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