Hello World Example using Log4j API
In this example, We will show you simple hello world example using Log4j API. The example program has been tested and shared in the same post.
Creating Logger Instance (ApplicationLogger.java)
package com.dineshkrish; import org.apache.log4j.Logger; public class ApplicationLogger { static Logger logger = Logger.getLogger(Logger.class); public static Logger getLogger() { return logger; } }
Adding Log4j Dependency (pom.xml)
<dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
Log4j Configuration (log4j.properties)
# Define the root logger with appender file log = . log4j.rootLogger = DEBUG, FILE # Define the file appender log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=${log}/log.out # Define the layout for file appender log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%m%n
Testing the Logger (Test.java)
package com.dineshkrish; public class Test { public static void main(String[] args) { ApplicationLogger.getLogger().info("Hello World 1"); ApplicationLogger.getLogger().info("Hello World 2"); ApplicationLogger.getLogger().info("Hello World 3"); ApplicationLogger.getLogger().info("Hello World 4"); ApplicationLogger.getLogger().info("Hello World 5"); ApplicationLogger.getLogger().info("Hello World 6"); ApplicationLogger.getLogger().info("Hello World 7"); ApplicationLogger.getLogger().info("Hello World 8"); ApplicationLogger.getLogger().info("Hello World 9"); ApplicationLogger.getLogger().info("Hello World 10"); } }
Output
References
1. Apache Log4J API Distribution
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet