log4j配置,在测试时仅显示错误消息。

huangapple go评论70阅读模式
英文:

log4j config when testing to show only Errors

问题

我有一个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,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

这是我的/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>
英文:

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/

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;!DOCTYPE log4j:configuration SYSTEM &quot;log4j.dtd&quot;&gt;
&lt;log4j:configuration debug=&quot;true&quot;
xmlns:log4j=&#39;http://jakarta.apache.org/log4j/&#39;&gt;
&lt;appender name=&quot;console&quot; class=&quot;org.apache.log4j.ConsoleAppender&quot;&gt;
&lt;layout class=&quot;org.apache.log4j.PatternLayout&quot;&gt;
&lt;param name=&quot;ConversionPattern&quot;
value=&quot;%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n&quot; /&gt;
&lt;/layout&gt;
&lt;/appender&gt;
&lt;root&gt;
&lt;level value=&quot;ERROR&quot; /&gt;
&lt;appender-ref ref=&quot;console&quot; /&gt;
&lt;/root&gt;
&lt;/log4j:configuration&gt;

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 &quot;org.slf4j.impl.StaticLoggerBinder&quot;.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
log4j: reset attribute= &quot;false&quot;.
log4j: Threshold =&quot;null&quot;.
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: &quot;org.apache.log4j.PatternLayout&quot;
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:

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

and in my maven pom.xml:

&lt;properties&gt;
&lt;eclipselink.showSql&gt;false&lt;/eclipselink.showSql&gt;
&lt;eclipselink.logging.level&gt;OFF&lt;/eclipselink.logging.level&gt;
&lt;eclipselink.logging.level.sql&gt;OFF&lt;/eclipselink.logging.level.sql&gt;
&lt;/properties&gt;

答案1

得分: 1

这个维基页面:wiki.eclipse.org/EclipseLink/Examples/JPA/Logging 描述了在 EclipseLink 中的日志记录。在这种情况下,根据配置的服务器平台 WebLogicPlatform 的日志消息,看起来您正在使用 Weblogic 服务器;如果使用容器管理的持久性,很可能会将其自己的记录器注入到持久性单元中 - 您需要使用 WebLogic 控制台来更改日志记录设置。

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

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

将其添加到 persistence.xml 中,这将使 EclipseLink 使用其自己的日志记录,它将使用您已经配置的设置。

英文:

The wiki: wiki.eclipse.org/EclipseLink/Examples/JPA/Logging describes logging in EclipseLink. I this case, it seems you are on Weblogic server (as determined by the Configured server platform WebLogicPlatform log message), and if using container managed persistence, it is likely injecting its own logger into the persistence unit - you would need to use the WebLogic console to change the logging settings.

You can prevent this by adding:

 &lt;property name=&quot;eclipselink.logging.logger&quot; value=&quot;DefaultLogger&quot;/&gt;

To the persistence.xml, which will have EclipseLink use its own logging, which will use the settings you have already configured.

答案2

得分: 0

以下是翻译好的部分:

日志级别配置包含在持久性单元的定义中,位于 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" />
英文:

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

add this:

&lt;property name=&quot;eclipselink.logging.level&quot; value=&quot;OFF&quot;/&gt;
&lt;property name=&quot;eclipselink.logging.parameters&quot; value=&quot;false&quot;/&gt;
&lt;property name=&quot;eclipselink.weaving&quot; value=&quot;static&quot; /&gt;
&lt;property name=&quot;eclipselink.logging.level.sql&quot; value=&quot;OFF&quot; /&gt;
&lt;property name=&quot;eclipselink.logging.level.cache&quot; value=&quot;OFF&quot; /&gt;

huangapple
  • 本文由 发表于 2020年9月30日 23:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64140663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定