测试仅显示错误时的log4j配置

问题描述

我有一个Spring应用程序,在这里我使用eclipselink和dbunit进行测试,我在/ src / test / resources /

上创建了这个 log4j.xml 文件
<?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>

但是当我运行测试时,我在控制台中看到很多消息,不仅是错误消息:

 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,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,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,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,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,main])--SELECT COUNT(EXERCICE_ID) FROM EXERCICE

这是我的/src/test/java/meta-inf/persistence.xml文件

<?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>

在我的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>

解决方法

日志级别配置包含在persistence.xml中的持久性单元的定义中,

添加此内容:

<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" />
,

Wiki:wiki.eclipse.org/EclipseLink/Examples/JPA/Logging描述了EclipseLink中的日志记录。在这种情况下,似乎您在Weblogic服务器上(由配置服务器平台WebLogicPlatform日志消息确定),并且如果使用容器管理的持久性,则很可能将其自己的记录器注入持久性单元中-您将需要使用WebLogic控制台以更改日志记录设置。

您可以通过添加以下内容来防止这种情况:

 <property name="eclipselink.logging.logger" value="DefaultLogger"/>

对于persistence.xml,它将使EclipseLink使用其自己的日志记录,该日志记录将使用您已经配置的设置。