log4j printing only FATAL and ERROR message in console, but not displaying INFO/TRACE

Viewed 1186

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.

1 Answers

You set your custom "DIAG" level to 350. 350 is lower than INFO (400), so INFO, DEBUG and TRACE levels are not shown on the console.

If you check your file output, it should contain everything.

OFF     0
FATAL   100
ERROR   200
WARN    300
INFO    400
DEBUG   500
TRACE   600

Either change the intLevel or pass a different level to the appender.

See custom log levels

Related