如何使用logback禁用accessExternalDTD和entityExpansionLimit警告

时间:2022-09-06 08:12:11

I'm using logback with groovy and get lots of warnings showing up when parsing xml. I am aware of the bug in JDK1.7_u45 that is causing this.

我正在使用groovy的logback并在解析xml时出现很多警告。我知道JDK1.7_u45中导致此问题的错误。

Warning:  org.apache.xerces.parsers.SAXParser: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized.
Warning:  org.apache.xerces.parsers.SAXParser: Property 'http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit' is not recognized.

Is there any way to turn off this log warnings from showing up in DEBUG? I tried writing a filter using Filter, but didn't help.

有没有办法关闭这个日志警告出现在DEBUG?我尝试使用Filter编写过滤器,但没有帮助。

2 个解决方案

#1


18  

This is a known bug in the JRE that reports this as an warning. See bug reports here and here

这是JRE中的一个已知错误,它将此报告为警告。请参阅此处和此处的错误报告

The issue happens only when you have xerces jar in your classpath, the xerces implementation does not recognize the property and throws an exception on org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.setProperty() which results in a warning log (to System.err) from the com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse()

只有在类路径中有xerces jar时才会出现问题,xerces实现无法识别该属性并在org.apache.xerces.jaxp.SAXParserImpl $ JAXPSAXParser.setProperty()上抛出异常,从而产生警告日志(对系统.err)来自com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse()

The easy (if possible) solution is remove xerces jar from your classpath.

简单(如果可能)解决方案是从类路径中删除xerces jar。

Your log filter does not work since the error is never sent to slf4j. Which kind of suggests a convoluted way of fixing the issue - redirect System.err to slf4j and then use a logging filter on it.

您的日志过滤器不起作用,因为错误永远不会发送到slf4j。哪种方式表明解决问题的复杂方法 - 将System.err重定向到slf4j,然后对其使用日志记录过滤器。

Sample code to reproduce the issue (based on the issue report):

用于重现问题的示例代码(基于问题报告):

import java.io.IOException;
import java.net.URL;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

public class XercesTest {
    public static void main(String[] args) throws IOException, TransformerConfigurationException {
        TransformerFactory tf = TransformerFactory.newInstance();
        URL xsl = MainClass.class.getResource("build.xsl");
        StreamSource stylesheetSource = new StreamSource(
            xsl.openStream(), xsl.toExternalForm());
        tf.newTransformer(stylesheetSource);
    }
}

build.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <!-- TODO: Auto-generated template -->
    </xsl:template>
</xsl:stylesheet>

And maven dependency:

和maven依赖:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.11.0</version>
</dependency>

#2


1  

I also had this error in a project. As I understand it, newer versions of the JRE have the Xerces implementation built into it. More importantly, the JRE version properly supports the accessExternalDTD and entityExpansionLimit properties.

我在项目中也遇到了这个错误。据我了解,新版本的JRE内置了Xerces实现。更重要的是,JRE版本正确支持accessExternalDTD和entityExpansionLimit属性。

Because I had a downstream dependency that was including xercesImpl.jar in my war file, my solution was to just yank it using the code below in my build.gradle and let the JRE implementation of xerces take over in the class path.

因为我的war文件中有一个包含xercesImpl.jar的下游依赖项,我的解决方案就是在build.gradle中使用下面的代码将其拉出来让xerces的JRE实现接管类路径。

warApplication {
    from '/WEB-INF/lib'
        exclude 'xercesImpl*.jar'
}

#1


18  

This is a known bug in the JRE that reports this as an warning. See bug reports here and here

这是JRE中的一个已知错误,它将此报告为警告。请参阅此处和此处的错误报告

The issue happens only when you have xerces jar in your classpath, the xerces implementation does not recognize the property and throws an exception on org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.setProperty() which results in a warning log (to System.err) from the com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse()

只有在类路径中有xerces jar时才会出现问题,xerces实现无法识别该属性并在org.apache.xerces.jaxp.SAXParserImpl $ JAXPSAXParser.setProperty()上抛出异常,从而产生警告日志(对系统.err)来自com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse()

The easy (if possible) solution is remove xerces jar from your classpath.

简单(如果可能)解决方案是从类路径中删除xerces jar。

Your log filter does not work since the error is never sent to slf4j. Which kind of suggests a convoluted way of fixing the issue - redirect System.err to slf4j and then use a logging filter on it.

您的日志过滤器不起作用,因为错误永远不会发送到slf4j。哪种方式表明解决问题的复杂方法 - 将System.err重定向到slf4j,然后对其使用日志记录过滤器。

Sample code to reproduce the issue (based on the issue report):

用于重现问题的示例代码(基于问题报告):

import java.io.IOException;
import java.net.URL;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

public class XercesTest {
    public static void main(String[] args) throws IOException, TransformerConfigurationException {
        TransformerFactory tf = TransformerFactory.newInstance();
        URL xsl = MainClass.class.getResource("build.xsl");
        StreamSource stylesheetSource = new StreamSource(
            xsl.openStream(), xsl.toExternalForm());
        tf.newTransformer(stylesheetSource);
    }
}

build.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <!-- TODO: Auto-generated template -->
    </xsl:template>
</xsl:stylesheet>

And maven dependency:

和maven依赖:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.11.0</version>
</dependency>

#2


1  

I also had this error in a project. As I understand it, newer versions of the JRE have the Xerces implementation built into it. More importantly, the JRE version properly supports the accessExternalDTD and entityExpansionLimit properties.

我在项目中也遇到了这个错误。据我了解,新版本的JRE内置了Xerces实现。更重要的是,JRE版本正确支持accessExternalDTD和entityExpansionLimit属性。

Because I had a downstream dependency that was including xercesImpl.jar in my war file, my solution was to just yank it using the code below in my build.gradle and let the JRE implementation of xerces take over in the class path.

因为我的war文件中有一个包含xercesImpl.jar的下游依赖项,我的解决方案就是在build.gradle中使用下面的代码将其拉出来让xerces的JRE实现接管类路径。

warApplication {
    from '/WEB-INF/lib'
        exclude 'xercesImpl*.jar'
}