How to use different type of Appenders in Log4j properties file

In this tutorial you will learn how you can use different type of appenders in log4j properties file. In this example we will use two type of appender and those are org.apache.log4j.RollingFileAppender and org.apache.log4j.ConsoleAppender. And we will get the different level of logs in different appender.

In this tutorial we will create two appenders ConsoleAppender and RollingFileAppender. You have to set the one file name to log4j.appender.FA.File property, in this example we are setting it application_log.log. it will create the log file if not exists and put all the generated logs in this file. We also added second appender as ConsoleAppender (CA) to the rootLogger.

You can also set the logger level for each appender separately. In this example the RollingFileAppender logger level is set to WARN. So only WARN, ERROR and FATAL level log messages will be logged in the application_log.log file. As we are not setting any log level explicitly for the ConsoleAppender it will inherit the rootLogger level, and that is DEBUG.

Below is the example log4j properties file which is using different type of appender.

#File Appender 
log4j.appender.FA=org.apache.log4j.RollingFileAppender
log4j.appender.FA.File=application_test.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout 
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 
 
#Console Appender 
log4j.appender.CA=org.apache.log4j.ConsoleAppender 
log4j.appender.CA.layout=org.apache.log4j.PatternLayout 
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 
  
# Set the logger level of File Appender to WARN 
log4j.appender.FA.Threshold = WARN
log4j.rootLogger=DEBUG, CA, FA 

Below is the code example which is using different type of appender in log4j properties file.

package com.java.connect.log4j;
 
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
 
public class Log4jDifferentAppenderExample {
    private static Logger LOGGER = Logger.getLogger(Log4jDifferentAppenderExample.class);
 
    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j2.properties");
        LOGGER.debug("Log4j Different Appender Example debug message");
        LOGGER.info("Log4j Different Appender Example info message");
        LOGGER.warn("Log4j Different Appender Example warn message");
        LOGGER.error("Log4j Different Appender Example error message");
        LOGGER.fatal("Log4j Different Appender Example fatal message");
    }
}

The output of the programme on consol will be as:

0    [main] DEBUG com.java.connect.log4j.Log4jDifferentAppenderExample  - Log4j Different Appender Example debug message
3    [main] INFO  com.java.connect.log4j.Log4jDifferentAppenderExample  - Log4j Different Appender Example info message
3    [main] WARN  com.java.connect.log4j.Log4jDifferentAppenderExample  - Log4j Different Appender Example warn message
4    [main] ERROR com.java.connect.log4j.Log4jDifferentAppenderExample  - Log4j Different Appender Example error message
4    [main] FATAL com.java.connect.log4j.Log4jDifferentAppenderExample  - Log4j Different Appender Example fatal message

And the output in the application_log.log file will be as:

3    [main] WARN  com.java.connect.log4j.Log4jDifferentAppenderExample  - Log4j Different Appender Example warn message
4    [main] ERROR com.java.connect.log4j.Log4jDifferentAppenderExample  - Log4j Different Appender Example error message
4    [main] FATAL com.java.connect.log4j.Log4jDifferentAppenderExample  - Log4j Different Appender Example fatal message