Thanks in Advance, I am trying to write logs into a file using log4j, I have tried all log4j.properties but nothing worked. Below is the code.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class Main {
final static Logger logger = LogManager.getLogger(Main.class);
public static void main(String[] args) throws IOException {
logger.info("Hello and welcome!");
// Press Shift+F10 or click the green arrow button in the gutter to run the code.
for (int i = 1; i <= 50; i++) {
logger.warn("This is warn : " + i);
logger.error("This is error : " + i);
logger.fatal("This is fatal : " + i);
}
}
}
Here is the log4j.properties file placed under src/main/resources/log4j.properties
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender log4j.appender.file.File=C:\logs\mylog.log log4j.appender.file.ImmediateFlush=true log4j.appender.file.threshold=INFO log4j.appender.file.Append=true log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=20 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy log4j.appender.file.rollingPolicy.FileNamePattern=C:\logs\mylog%d.log
VM configuration: -Dlog4j2.debug=true-Dlog4j2.configurationFile=C:\Users****\resources\log4j.properties
pom.xml => log4j-api & log4j-core as dependencies.

Use this one and set your log path in it and it will work