In this post, We will learn How to load log4j2 xml file programmatically using a demo project?
In this example, We have kept our log4j2.xml in the config folder inside the project(instead of keeping log4j2.xml inside src\main\resources).
pom.xml
We have added log4j2 log4j-api & log4j-core dependency in the dependencies section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials.org</groupId> <artifactId>LoadLog4j2xmlFileProgrammaticallyExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.17.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.1</version> </dependency> </dependencies> </project> |
log4j2.xml
I have created log4j2.xml configuration file in the config folder inside the project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn"> <Appenders> <!-- configuration of Console appender --> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </Appenders> <Loggers> <!-- Here Root logger is referring to console appender --> <Root level="debug" additivity="false"> <AppenderRef ref="console" /> </Root> </Loggers> </Configuration> |
MyUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.kkjavatutorials.util; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.Configurator; public class MyUtil { //Utility method to support load log4j2.xml file public static void loadLog4j2Config(String logConfigurationFile) { try { InputStream inputStream = new FileInputStream(logConfigurationFile); ConfigurationSource source = new ConfigurationSource(inputStream); Configurator.initialize(null, source); } catch (IOException e) { e.printStackTrace(); } } } |
ClientTest.java
Now we are going to test our logger. For this, I’ve written below test class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.kkjavatutorials.client; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.kkjavatutorials.util.MyUtil; public class ClientTest { //This static block has to be executed very first static { MyUtil.loadLog4j2Config("config/log4j2.xml"); } private static final Logger logger = LogManager.getLogger(ClientTest.class); public static void main(String[] args) { logger.info("Logging info message"); logger.warn("Logging warn message"); logger.error("Logging error message"); } } |
If you run ClientTest.java as a java application then you’ll find the below output:
16:18:18.290 [main] INFO com.kkjavatutorials.client.ClientTest – Logging info message
16:18:18.290 [main] WARN com.kkjavatutorials.client.ClientTest – Logging warn message
16:18:18.290 [main] ERROR com.kkjavatutorials.client.ClientTest – Logging error message
You May Also Like:
Why Logging?
Log4j 2 simple example
How to Create a Custom Appender in log4j2?
That’s All about How to load log4j2 xml file programmatically?