07 - log4j Levels

7.1 Overview

In previous chapters we have touched a bit on log levels and in this chapter we will discuss the log levels in detail.

7.2 Log Levels

Log4j supports several log levelsand they are defined in class Level under org.apache.log4j package. Below are the details of supported log levels.

  1. ALL- This log level is used to log messages of all levels. This level does not have any corresponding printing method.
  2. OFF- This log level is used to turn off all the log messages. This level does not have any corresponding printing method.
  3. DEBUG- This log level is used to log the debug statements and messages those are intended to debug the application should be logged with debug level. Ideally in production environments, debug logs are turned off. 
  4. ERROR- This log level is used to log the error statements and which still allows application to run.
  5. FATAL -This log level is used to log the fatal statements and we should write messages with fatal severity only when such type of error occurred, application will not be able to continue.
  6. INFO-This log level is used to log the info statements and messages those are intended for the information purpose should be logged with info level.
  7. TRACE-This log level is used to log the trace statements
  8. WARN-This log level is used to log the warning statements

7.3 Effective Level of a Logger

If a logger has not defined its level then, its effective log level will be inherited from its parent whose log level is not null proceeding upwards.

For example if a logger “xyz” has not defined its level and Root logger is defined as DEBUG level, then the effective level of logger “xyz” will become DEBUG.

7.4 Log Level Filter

All the loge level discussed above follows below relative order- 

ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

This means that DEBUG log level is relatively low in order then INFO whereas WARN is relative above than INFO.
Setting a log level will enable all the log levels which are “equal to or above in order” which means setting a log level of ERRO will allow only ERROR and FATAL logs to be printed.

Threshold Filter–Threshold filter is like a central switch which is applied to overall logger hierarchy and gets precedence over individual logger level. If individual logger is configured for DEBUG level and hierarchy threshold is set to ERROR then all loggers will be log only ERROR or above.

To set the threshold filter, we need to get the instance of LoggerRepository and apply on it like below 

       LoggerRepository repository = logger.getLoggerRepository();

       repository.setThreshold(Level.ERROR);

7.4 Configure Log Level

Log level can be configured either programmatically or in configuration file.

  1. Configure Log Level in configuration file- Log level is configured under logger object with tag <level> like below

          <logger name="com.log4j.examples.Test"  additivity="false">
              <level value="ERROR"/>
              <appender-ref ref="console" />
         </logger>

 

  b. Configure Log Level programmatically-  Log Level can be set programmatically by calling setLevel() method on logger instance.

7.5 Example

7.5.1 Configure log level programmatically

package com.log4j.examples;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
publicclass Test {

static Logger logger = Logger.getLogger(Test.class);
publicstaticvoid main(String[] args) {

logger.setLevel(Level.ERROR);
        logger.debug("Debug message ...");
        logger.error("Error message ... ");
        logger.fatal("fatal message ... ");
        logger.info(" Info message ... ");
        logger.trace(" trace message...");    
}}

The above program will produce below output.

2015-04-27 22:40:07 ERROR Test:22 - Error message ... 
2015-04-27 22:40:07 FATAL Test:23 - fatal message ...

7.5.2 Set Threshold Filter

In below example though the logger is set to ERROR level but as we set the threshold filter as FATAL , only fatal message will be displayed.

package com.log4j.examples;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
publicclass Test {

static Logger logger = Logger.getLogger(Test.class);
publicstaticvoid main(String[] args) {

    logger.setLevel(Level.ERROR);
    LoggerRepository  repository = logger.getLoggerRepository();
    repository.setThreshold(Level.FATAL);

        logger.debug("Debug message ...");
        logger.error("Error message ... ");
        logger.fatal("fatal message ... ");
        logger.info(" Info message ... ");
        logger.trace(" trace message...");    
   }
}

The above program will produce below output.

     2015-04-27 22:40:07 FATAL Test:23 - fatal message ...

7.5.3 Configure log level in configuration file

package com.log4j.examples;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
publicclass Test {

   static Logger logger = Logger.getLogger(Test.class);
   publicstaticvoid main(String[] args) {

         logger.debug("Debug message ...");
        logger.error("Error message ... ");
        logger.fatal("fatal message ... ");
        logger.info(" Info message ... ");
        logger.trace(" trace message...");    
        }
}

Define the below log4j.xml in src folder

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
    xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
        value="%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
    </appender>

    <logger name="com.log4j.examples.Test"  additivity="false">
          <level value="ERROR"/>
          <appender-ref ref="console" />
          <appender-ref ref="file" />
    </logger>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="console" />
    </root>
</log4j:configuration>

The above program will produce below output.

   2015-04-27 22:40:07 ERROR Test:22 - Error message ... 
   2015-04-27 22:40:07 FATAL Test:23 - fatal message ...

 

 

Like us on Facebook