Search code examples
javaspringlogbackunitils

Configure Unitils Log


I want to configure the log level of Unitils because I don't want to show Information traces when I run my tests. The tests run with Unitils through Spring:

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

I configure logs using 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>

When I run the tests, in the file health-safety-web-test.log doesn't appear Info logs, but eclipse and console (mvn install) show those logs.

This is an example of the logs I don't want to show:

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

Any idea?


Solution

  • The problem was that Unitils uses commons-logging to print logs, and I am using SLF4J in my project. I solved the problem changing the dependency of SLF4J

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    

    for

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

    And now Unitils logs (and any other libreries which uses commons-logging) can be configured in the logback.xml file.