Step 1:
- Add log4j dependency in pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Step 2:
- Basic program to store content in log file
package com.cognizant.devops.Log4jProject;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Log4jHelloWorld {
static final Logger logger = Logger.getLogger(Log4jHelloWorld.class);
public static void main(String[] args)
{
//Configure logger
BasicConfigurator.configure();
for(int i=0;i<20000;i++)
{
System.out.println("Hello World!"+i);
logger.info("Hello World!"+i);
}
}
}
Step 3:
Create log4j.properties file in src/main/resources
Add the build path points to src/main/resources