4

Tell me please, how to write path to file inside the maven dependency with my custom configuration in <xi:include href="" in log4j2 configuration?

In my application I've added following maven dependency:

<dependency>
    <groupId>logging</groupId>
    <artifactId>logging-libs</artifactId>
    <version>1.0.1</version>
</dependency>

this library contains file with custom configuration: log4j2-include.xml.

My config library

How can i write the path to my configuration file in log4j2.xml using log4j2 include feature.

I tried this but it doesn't work:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" xmlns:xi="http://www.w3.org/2001/XInclude">
    <Properties>
        <Property name="logger.app" value="${spring:appName:-UNDEFINED}"/>
        <Property name="logger.appVersion" value="${spring:appVersion:-UNDEFINED}"/>
    </Properties>
    <xi:include href="log4j2-include.xml"/>
</configuration>

ERROR:

[Warning] log4j2.xml:7:44: Include operation failed, reverting to fallback. Resource error reading file as XML (href='log4j2-include.xml'). Reason: C:\Users\...\IdeaProjects\my-logging\target\classes\log4j2-include.xml (�� ������� ����� ��������� ����)
[Fatal Error] log4j2.xml:7:44: An include with href 'log4j2-include.xml'failed, and no fallback element was found.
ERROR StatusLogger Error parsing C:\Users\...\IdeaProjects\my-logging\target\classes\log4j2.xml
 org.xml.sax.SAXParseException; systemId: file:///C:/Users/.../IdeaProjects/my-logging/target/classes/log4j2.xml; lineNumber: 7; columnNumber: 44; An include with href 'log4j2-include.xml'failed, and no fallback element was found.
    at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:261)
    at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
    at org.apache.logging.log4j.core.config.xml.XmlConfiguration.<init>(XmlConfiguration.java:94)
    at org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory.getConfiguration(XmlConfigurationFactory.java:46)
    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:533)
    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:457)
    at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:318)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:690)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:711)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:253)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)
    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
    at org.apache.commons.logging.LogFactoryService.getInstance(LogFactoryService.java:46)
    at org.apache.commons.logging.LogFactoryService.getInstance(LogFactoryService.java:41)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:655)
    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:203)
    at my.pack.DemoLoggingApplication.main(DemoLoggingApplication.java:10)
ERROR StatusLogger No logging configuration
Olivier
  • 13,283
  • 1
  • 8
  • 24

1 Answers1

3

The XInclude support in Log4j2 configuration was introduced in LOG4J2-341.

The feature is very dependent on the underlying XML parser and has different limitations.

Among others, AFAIK, it does not allow to use a classpath resource for inclusion.

Please, consider read the James Hutton comment in the aforementioned issue:

when using xinclude the path is interpreted as relative to the base directory, not the file including the additional configuration. Classpath protocol is not usuable either.

One thing you could try instead is combining the different configurations provided across the files in your library.

The process is described in the Composite Configuration section of the Log4j2 manual.

I never tested it, but it seems you can provide a comma separated list of configuration resources using the configuration property log4j.configurationFile.

Probably defining the log4j.configurationFile similar to the following could work in your use case:

log4j.configurationFile=log4j2.xml,log4j2-include.xml

Please, consider read this related SO question.

jccampanero
  • 50,989
  • 3
  • 20
  • 49