Log4j configuration with different log files

In this tutorial you will how you can configure different log file for different layers or modules of your java application, Most of java application made of different type of layers like client layer, service layer and DAO layer. In this case you may want to log your messages in different log files for different layer.

In this example we are using three layers for my application. And we are going to logs different layer’s log messages in different log files.

To configure log4j properties file for this requirement we have to create three RollingFileAppender and associate them with each package.

You can see the below log properties file that instead of associating the appenders to the rootLogger we associate it with different packages. The DaoFileAppender is linked with the dao package and the ServiceFileAppender is linked with the service package and as same ClientFileAppender is linked with client package. It means all the log entries in the dao package will be logged in the dao_layer.log file and all the log entries in the service package will be logged in the service_layer.log file and same for client_layer.log.

You can also set different logger levels for each package, in this example for the dao package it is set to WARN and for the for service package it is set to DEBUG and for client package it is set to INFO. The rootLogger will be by default set to the logger level DEBUG.

The full log4j properties file looks like as:

log4j.rootLogger=DEBUG
  
# DaoFileAppender - we have to log messages in the dao_layer.log file. 
log4j.appender.DaoFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DaoFileAppender.File=dao_layer.log 
log4j.appender.DaoFileAppender.layout=org.apache.log4j.PatternLayout 
log4j.appender.DaoFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n 
  
# ServiceFileAppender - we have to log messages in the service_layer.log file. 
log4j.appender.ServiceFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ServiceFileAppender.File=service_layer.log 
log4j.appender.ServiceFileAppender.layout=org.apache.log4j.PatternLayout 
log4j.appender.ServiceFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n 
  
# ClientFileAppender - we have to log messages in the client_layer.log file. 
log4j.appender.ClientFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ClientFileAppender.File=client_layer.log 
log4j.appender.ClientFileAppender.layout=org.apache.log4j.PatternLayout 
log4j.appender.ClientFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n 
 
log4j.logger.com.java.connect.log4j.dao=WARN,DaoFileAppender 
log4j.logger.com.java.connect.log4j.service=DEBUG,ServiceFileAppender
log4j.logger.com.java.connect.log4j.client=INFO,ClientFileAppender

To understand the whole things we will use three classes to put the logs, and each classes in different package. So in this example we will create three classes named as Log4jDAOExample in dao package, Log4jServiceExample in service package and Log4JClientExample in client package. And let see how the logs are being generated in different log files.

The examples of different type of classes in different package are shown below.

Log4jClientExample in client package

package com.java.connect.log4j.client;
 
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
 
public class Log4JClientExample {
    private static Logger LOGGER = Logger.getLogger(Log4JClientExample.class);
 
    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j2.properties");
        LOGGER.debug("Client layer debug logger ");
        LOGGER.info("Client layer info logger ");
        LOGGER.warn("Client layer warn logger ");
        LOGGER.error("Client layer error logger ");
        LOGGER.fatal("Client layer fatal logger ");
    }
 
}

Log4jServiceExample in service package

package com.java.connect.log4j.service;
 
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
 
public class Log4jServiceExample {
    private static Logger LOGGER = Logger.getLogger(Log4jServiceExample.class);
 
    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j2.properties");
        LOGGER.debug("Service layer debug logger ");
        LOGGER.info("Service layer info logger ");
        LOGGER.warn("Service layer warn logger ");
        LOGGER.error("Service layer error logger ");
        LOGGER.fatal("Service layer fatal logger ");
    }
 
}

Log4jDaoExample in dao package

package com.java.connect.log4j.dao;
 
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
 
public class Log4jDAOExample {
    private static Logger LOGGER = Logger.getLogger(Log4jDAOExample.class);
 
    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j2.properties");
        LOGGER.debug("Dao layer debug logger ");
        LOGGER.info("Dao layer info logger ");
        LOGGER.warn("Dao layer warn logger ");
        LOGGER.error("Dao layer error logger ");
        LOGGER.fatal("Dao layer fatal logger ");
    }
}

When you run these above examples it will create three different logs files and these files looks like as:

The client_layer.log file

0    [main] INFO  com.java.connect.log4j.client.Log4JClientExample  - Client layer info logger 
 3    [main] WARN  com.java.connect.log4j.client.Log4JClientExample  - Client layer warn logger 
 3    [main] ERROR com.java.connect.log4j.client.Log4JClientExample  - Client layer error logger 
 4    [main] FATAL com.java.connect.log4j.client.Log4JClientExample  - Client layer fatal logger 

The service_layer.log file

0    [main] DEBUG com.java.connect.log4j.service.Log4jServiceExample  - Service layer debug logger 
 3    [main] INFO  com.java.connect.log4j.service.Log4jServiceExample  - Service layer info logger 
 3    [main] WARN  com.java.connect.log4j.service.Log4jServiceExample  - Service layer warn logger 
 3    [main] ERROR com.java.connect.log4j.service.Log4jServiceExample  - Service layer error logger 
 4    [main] FATAL com.java.connect.log4j.service.Log4jServiceExample  - Service layer fatal logger 

The dao_layer.log file

0    [main] WARN  com.java.connect.log4j.dao.Log4jDAOExample  - Dao layer warn logger 
 3    [main] ERROR com.java.connect.log4j.dao.Log4jDAOExample  - Dao layer error logger 
 3    [main] FATAL com.java.connect.log4j.dao.Log4jDAOExample  - Dao layer fatal logger 

If you want the log all the log messages from all the packages in one file to be logged then add the corresponding appender to the rootLogger, if you want the appender to be associated with a specific package then apply only to that package. But take care if you do it for both than the log entries can be duplicated.