Search code examples
javaspringlog4jeclipselinkdbunit

log4j config when testing to show only Errors


I have a Spring application, where I use eclipselink and dbunit for my tests I've created this log4j.xml file on /src/test/resources/

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
        xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                    value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="ERROR" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

But when i run the tests I see a lot of messages in the console, not only the error ones:

 T E S T S
-------------------------------------------------------
Running com.pastis.services.config.TestConfig
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Level value for root is  [ERROR].
log4j: root level set to ERROR
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].
[EL Fine]: 2020-09-30 17:08:19.045--Thread(Thread[main,5,main])--Configured server platform: org.eclipse.persistence.platform.server.wls.WebLogicPlatform
[EL Config]: 2020-09-30 17:08:19.298--ServerSession(142103421)--Thread(Thread[main,5,main])--The access type for the persistent class [class com.pastis.FactureCommentType] is set to [FIELD].
[EL Config]: 2020-09-30 17:08:19.335--ServerSession(142103421)--Thread(Thread[main,5,main])--The target entity (reference) class for the one to many mapping element [field factureComments] is being defaulted to: class com.pastis.mode                           
[EL Config]: 2020-09-30 17:08:19.336--ServerSession(142103421)--Thread(Thread[main,5,main])--The access type for the persistent class [class com.pastis.model.inscriptions.Institution] is set to [FIELD].
[EL Config]: 2020-09-30 17:08:19.336--ServerSession(142103421)--Thread(Thread[main,5,main])--The target entity (reference) class for the one to many mapping element [field personneAffectations] is being defaulted to: class eu.europa.ec.oi
[EL Fine]: 2020-09-30 17:08:58.808--ServerSession(142103421)--Connection(1923962747)--Thread(Thread[main,5,main])--SELECT COUNT(EXERCICE_ID) FROM EXERCICE

this is my /src/test/java/META-INF/persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   <persistence-unit name="pastis-server-services">
      <properties>
         <property name="eclipselink.logging.level" value="OFF"/>
         <property name="eclipselink.logging.parameters" value="false"/>
         <property name="eclipselink.weaving" value="static" />
         <property name="eclipselink.logging.level.sql" value="OFF" />
         <property name="eclipselink.logging.level.cache" value="OFF" />
      </properties>
   </persistence-unit>
</persistence>

and in my maven pom.xml:

<properties>
    <eclipselink.showSql>false</eclipselink.showSql>
    <eclipselink.logging.level>OFF</eclipselink.logging.level>
    <eclipselink.logging.level.sql>OFF</eclipselink.logging.level.sql>
</properties>

Solution

  • The log level configuration is included in the definition of the persistence unit in the persistence.xml ,

    add this:

    <property name="eclipselink.logging.level" value="OFF"/>
            <property name="eclipselink.logging.parameters" value="false"/>
            <property name="eclipselink.weaving" value="static" />
            <property name="eclipselink.logging.level.sql" value="OFF" />
            <property name="eclipselink.logging.level.cache" value="OFF" />