There are very less info on the web for log4j2 with XMLLayout.
I have not found any links with simple examples (log4j2 + XMLLayout);
Can somebody post a simple example of the same ....
with regards Karthik
There are very less info on the web for log4j2 with XMLLayout.
I have not found any links with simple examples (log4j2 + XMLLayout);
Can somebody post a simple example of the same ....
with regards Karthik
Here is a minimal sample application that demonstrates use of XmlLayout
.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test-log4j2</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Test Log4J 2</name>
<description>Test Log4J 2</description>
<properties>
<jackson.version>2.5.0</jackson.version>
<log4j.version>2.5</log4j.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<mainClass>Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</project>
import org.apache.logging.log4j.LogManager;
public class Main {
public static void main(String[] args) {
LogManager.getLogger(Main.class).info("This is a test.");
}
}
status = ERROR
name = PropertiesConfig
rootLogger.level = INFO
rootLogger.appenderRefs = CONSOLE
rootLogger.appenderRef.CONSOLE.ref = CONSOLE
appenders = CONSOLE
appender.CONSOLE.type = Console
appender.CONSOLE.name = CONSOLE
appender.CONSOLE.layout.type = XmlLayout
appender.CONSOLE.layout.complete = true
appender.CONSOLE.layout.compact = true
> mvn clean package
> java -jar target/test-log4j2.jar
<?xml version="1.0" encoding="UTF-8"?><Events xmlns="http://logging.apache.org/log4j/2.0/events"><Event xmlns="" xmlns="http://logging.apache.org/log4j/2.0/events" timeMillis="1451539702378" thread="main" level="INFO" loggerName="Main" endOfBatch="false" loggerFqcn="org.apache.logging.log4j.spi.AbstractLogger"><Message>This is a test.</Message></Event></Events>
The trickiest part was figuring out the Jackson dependency required. Without that, Log4J 2 experiences internal ClassNotFoundException
errors and fails to initialize XmlLayout
.
If you prefer XML-based configuration, then here is the equivalent XML configuration.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" name="XMLConfig">
<Loggers>
<Root level="INFO">
<AppenderRef ref="CONSOLE"/>
</Root>
</Loggers>
<Appenders>
<Console name="CONSOLE">
<XMLLayout complete="true" compact="true"/>
</Console>
</Appenders>
</Configuration>