How to load log4j2 xml file programmatically ?

By | January 16, 2022

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.

log4j2.xml

I have  created log4j2.xml configuration file in the config folder inside the project

MyUtil.java

ClientTest.java

Now we are going to test our logger. For this, I’ve written below test class.

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?

Leave a Reply

Your email address will not be published.