42019

Log4j2 File Inclusion : and similar to Logback

Does Log4j2 support file inclusion mechanism like Logback does ? This is for including parts of a configuration file from another file (containing appenders, loggers etc.)

As an FYI - Here is how it works in Logback:

Joran supports including parts of a configuration file from another file. This is done by declaring a element, as shown below:

Example: File include (logback-examples/src/main/java/chapters/configuration/containingConfig.xml)

<configuration> <include file="src/main/java/chapters/configuration/includedConfig.xml"/> <root level="DEBUG"> <appender-ref ref="includedConsole" /> </root>

The target file MUST have its elements nested inside an element. For example, a ConsoleAppender could be declared as:

Example: File include (logback-examples/src/main/java/chapters/configuration/includedConfig.xml)

<included> <appender name="includedConsole" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>"%d - %m%n"</pattern> </encoder> </appender> </included>

Answer1:

XInclude can be used, but isn't an ideal solution. When using XInclude the include files themselves have to define a single top level element such as Appenders/Loggers/Properties.

Here's an example of how you could use it:

<?xml version="1.0" encoding="UTF-8"?> <Configuration status="error" xmlns:xi="http://www.w3.org/2001/XInclude"> <!-- this will override the log pattern defined in the included log4j-properties.xml --> <Properties> <Property name="log-pattern">jit %d %-5p [%t] %C{1.} - %m%n</Property> </Properties> <xi:include href="log4j2-properties.xml" /> <xi:include href="log4j2-appenders.xml" /> <xi:include href="log4j2-loggers.xml" /> </Configuration>

As an example include the log4j2-properties.xml could look like:

<?xml version="1.0" encoding="UTF-8"?> <Properties> <!-- define the log pattern as a property so that it can be overridden --> <Property name="log-pattern">%d %-5p [%t] %C{1.} - %m%n</Property> </Properties>

You can make XIncludes optional by using an empty "fallback" block. However in the latest version of log4j2 that is available this results in a warning message coming out of Xerces (since the DefaultErrorHandler is being used by log4j2).

<xi:include href="log4j2-optional.xml"> <xi:fallback /> </xi:include>

Answer2:

A slightly different but similar mechanism exists in Log4j2, it supports XInclude. See https://issues.apache.org/jira/browse/LOG4J2-341 for details. (This needs to be documented better...)

Recommend

  • Log4j RollingFileAppender not adding mapper and reducer logs to file
  • Log4net - delete any logs that is over 14 days old [duplicate]
  • cf.net exception and other logging
  • How do I configure log4j to send log events to java.util.logging using JULAppender?
  • Monotouch crashes with NullReferenceException on non nullable object
  • Access object instance inside an event handler
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • What causes the runtime difference in this trivial fortran code?
  • Setting the run time properties on SpringApplicationBuilder()
  • How to get links to open in the native browser in iOS Meteor apps?
  • Enabling DTD support in Sql Server
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Laravel: Getting Session ID oddly truncates when using foreach
  • How to get current document uri in XSLT?
  • How to disable all widgets inside Panel or inside Composite?
  • Reduction and collapse clauses in OMP have some confusing points
  • Xcode 4 NSLog Macro link in Xcode 3
  • Play WS (2.2.1): post/put large request
  • Can I check if a recipient has an automatic reply before I send an email?
  • Getting last autonumber in access
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Shallow update not allowed (git > 1.9)
  • Deserializing XML into class C#
  • When should I choose bucket sort over other sorting algorithms?
  • jquery mobile loadPage not working
  • Unanticipated behavior
  • Is there a mandatory requirement to switch app.yaml?
  • How to include full .NET prerequisite for Wix Burn installer
  • -fvisibility=hidden not passed by compiler for Debug builds
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Qt: Run a script BEFORE make
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal