英文:
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/
<?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>
答案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:
<property name="eclipselink.logging.logger" value="DefaultLogger"/>
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:
<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" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论