I have created the below basic program in JAVA to display the log message in console using log4j,
import org.apache.logging.log4j.*;
import org.apache.logging.*;
public class Test {
public final static Logger log=LogManager.getLogger(Test.class.getName());
public static void main(String[] args) {
log.log(Level.TRACE,"Testing");
if(log.isTraceEnabled())
System.out.println("Trace Enabled");
else
System.out.println("Trace not Enabled");
log.trace("Entering Log4j Example.");
log.info("Started");
log.fatal("Error occured");
log.error("Dkjdsfh");
log.debug("Debugging");
log.info("Success");
log.warn("Warning");
}
}
But, the above code displaying only ERROR/FATAL message, other log messages not displayed in console. Below is my log4j.xml configuration,
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<!-- Define custom levels before using them for filtering below. -->
<CustomLevels>
<CustomLevel name="DIAG" intLevel="350" />
<CustomLevel name="NOTICE" intLevel="450" />
<CustomLevel name="VERBOSE" intLevel="550" />
</CustomLevels>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-7level %logger{36} - %msg%n"/>
</Console>
<File name="MyFile" fileName="logs/app.log">
<PatternLayout pattern="%d %-7level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="trace">
<!-- Only events at DIAG level or more specific are sent to the console. -->
<AppenderRef ref="Console" level="diag" />
<AppenderRef ref="MyFile" level="trace" />
</Root>
</Loggers>
</Configuration>
Also, I have the below jar files to the build path,
log4j-api-2.13.3.jar
log4j-core-2.13.3.
Output of my program:
Trace not Enabled
16:55:40.104 [main] FATAL Test - Error occured
16:55:40.107 [main] ERROR Test - Dkjdsfh
Can someone please help me where I am doing mistake? Thanks in advance.