You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Gary Gregory (JIRA)" <ji...@apache.org> on 2018/03/20 15:15:00 UTC

[jira] [Comment Edited] (LOG4J2-2283) ParserConfigurationException when using log4j with junit

    [ https://issues.apache.org/jira/browse/LOG4J2-2283?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16406480#comment-16406480 ] 

Gary Gregory edited comment on LOG4J2-2283 at 3/20/18 3:14 PM:
---------------------------------------------------------------

Are you using an embedded version of Java in an Oracle database? That is the only place I see a class named {{JXDocumentBuilderFactory}}.

Can you please add a full description of your environment?

Our code blows up on:
{code:java}
setFeature(factory, "http://xml.org/sax/features/external-general-entities", false);
{code}
We could further guard our code against {{ParserConfigurationException}} by allowing processing to continue even when we catch a {{ParserConfigurationException}}.

Thoughts from other devs?

You could try this buy building our sources with this patch:
{noformat}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
index 7efb6ab..07d5740 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
@@ -199,7 +199,7 @@
         return factory.newDocumentBuilder();
     }
 
-    private static void disableDtdProcessing(final DocumentBuilderFactory factory) throws ParserConfigurationException {
+    private static void disableDtdProcessing(final DocumentBuilderFactory factory) {
         factory.setValidating(false);
         factory.setExpandEntityReferences(false);
         setFeature(factory, "http://xml.org/sax/features/external-general-entities", false);
@@ -207,12 +207,9 @@
         setFeature(factory, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     }
     
-    private static void setFeature(final DocumentBuilderFactory factory, final String featureName, final boolean value)
-            throws ParserConfigurationException {
+    private static void setFeature(final DocumentBuilderFactory factory, final String featureName, final boolean value) {
         try {
             factory.setFeature(featureName, value);
-        } catch (ParserConfigurationException e) {
-            throw e;
         } catch (Exception | LinkageError e) {
             getStatusLogger().error("Caught {} setting feature {} to {} on DocumentBuilderFactory {}: {}",
                     e.getClass().getCanonicalName(), featureName, value, factory, e, e);
{noformat}


was (Author: garydgregory):
Are you using an embedded version of Java in an Oracle database? That is the only place I see a class named {{JXDocumentBuilderFactory}}.

Can you please add a full description of your environment?

Our code blows up on:

{code:java}
setFeature(factory, "http://xml.org/sax/features/external-general-entities", false);
{code}

We could further guard our code against {{ParserConfigurationException}} by allowing processing to continue even when we catch a {{ParserConfigurationException}}.

Thoughts?

You could try this buy building our sources with this patch:
{noformat}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
index 7efb6ab..07d5740 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
@@ -199,7 +199,7 @@
         return factory.newDocumentBuilder();
     }
 
-    private static void disableDtdProcessing(final DocumentBuilderFactory factory) throws ParserConfigurationException {
+    private static void disableDtdProcessing(final DocumentBuilderFactory factory) {
         factory.setValidating(false);
         factory.setExpandEntityReferences(false);
         setFeature(factory, "http://xml.org/sax/features/external-general-entities", false);
@@ -207,12 +207,9 @@
         setFeature(factory, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     }
     
-    private static void setFeature(final DocumentBuilderFactory factory, final String featureName, final boolean value)
-            throws ParserConfigurationException {
+    private static void setFeature(final DocumentBuilderFactory factory, final String featureName, final boolean value) {
         try {
             factory.setFeature(featureName, value);
-        } catch (ParserConfigurationException e) {
-            throw e;
         } catch (Exception | LinkageError e) {
             getStatusLogger().error("Caught {} setting feature {} to {} on DocumentBuilderFactory {}: {}",
                     e.getClass().getCanonicalName(), featureName, value, factory, e, e);
{noformat}


> ParserConfigurationException when using log4j with junit
> --------------------------------------------------------
>
>                 Key: LOG4J2-2283
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-2283
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.10.0
>            Reporter: Vishnu Priya Matha
>            Priority: Major
>
> I am using Junit 4.10 and log4j 2.10 versions. When I run the junit test, I am unable to get the logs and following is the error when I turn on the log4j debugging:
> ERROR StatusLogger Error parsing /scratch/log4j.xml
>  javax.xml.parsers.ParserConfigurationException
>  at oracle.xml.jaxp.JXDocumentBuilderFactory.setFeature(JXDocumentBuilderFactory.java:374)
>  at org.apache.logging.log4j.core.config.xml.XmlConfiguration.setFeature(XmlConfiguration.java:213)
>  at org.apache.logging.log4j.core.config.xml.XmlConfiguration.disableDtdProcessing(XmlConfiguration.java:205)
>  at org.apache.logging.log4j.core.config.xml.XmlConfiguration.newDocumentBuilder(XmlConfiguration.java:194)
>  at org.apache.logging.log4j.core.config.xml.XmlConfiguration.<init>(XmlConfiguration.java:92)
>  at org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory.getConfiguration(XmlConfigurationFactory.java:46)
>  at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:239)
>  at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:369)
>  at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:260)
>  at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:613)
>  at org.apache.logging.log4j.core.LoggerContext.setConfigLocation(LoggerContext.java:603)
>  at oracle.oki.service.SampleBaseTest.setUpClass(SampleBaseTest.java:113)
>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>  at java.lang.reflect.Method.invoke(Method.java:497)
>  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
>  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
>  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
>  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
>  at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
>  at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
>  at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
>  at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:422)
>  at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:931)
>  at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:785)
> Following is how I setup my logging configuration:
> LoggerContext context = (LoggerContext) LogManager.getContext(false);
>  File file = new File("/scratch/log4j.xml");
>  
>  // this will force a reconfiguration
>  context.setConfigLocation(file.toURI());
> And the log4j.xml has following contents:
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration monitorinterval="30" status="info" strict="true">
>  <Properties>
>  <Property name="filename">/scratch/CelsiusConverter.log</Property>
>  </Properties>
>  <Appenders>
>  <Appender type="Console" name="Console">
>  <target>System.err</target>
>  <Layout type="PatternLayout" pattern="%d %p [%t] %m%n" />
>  </Appender>
>  <Appender type="Console" name="FLOW">
>  <target>System.err</target>
>  <Layout type="PatternLayout" pattern="%C\{1}.%M %m %ex%n" />
>  </Appender>
>  <Appender type="File" name="File" fileName="${filename}">
>  <Layout type="PatternLayout" pattern="%d %p %C\{1.} [%t] %m%n" />
>  </Appender>
>  </Appenders>
>  <Loggers>
>  <Root level="trace">
>  <AppenderRef ref="File" />
>  </Root>
>  </Loggers>
> </Configuration>



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)