How to create separate log file for package or class

Log4j is easy configurable and can customize based on classes and packages, will see how can it be done using an example. Will take two services; one is InternalService which is calling internal service and other service, which is calling external Service. For External service will create seperate log file to track request and response.

Project Design

Log4j Project Design

1. InternalService
package com.javavillage.service;

import org.apache.log4j.Logger;

public class InternalService {
	private final static Logger logger = Logger.getLogger(InternalService.class);
	private String input;
	private String output;
	
	/**
	 * @return the input
	 */
	public String getInput() {
		return input;
	}
	/**
	 * @param input the input to set
	 */
	public void setInput(String input) {
		this.input = input;
	}
	/**
	 * @return the output
	 */
	public String getOutput() {
		return output;
	}
	/**
	 * @param output the output to set
	 */
	public void setOutput(String output) {
		this.output = output;
	}
	public void service(){
		logger.info("Internal Service Request: "+input);
		setOutput("Hi "+input); // Here your Service call to Internal Service
		logger.info("Internal Service Response: "+input);
	}
}



2. ExternalService
  package com.javavillage.service;

import org.apache.log4j.Logger;

public class ExternalService {
	private final static Logger logger = Logger.getLogger(ExternalService.class);
	private String input;
	private String output;
	
	/**
	 * @return the input
	 */
	public String getInput() {
		return input;
	}
	/**
	 * @param input the input to set
	 */
	public void setInput(String input) {
		this.input = input;
	}
	/**
	 * @return the output
	 */
	public String getOutput() {
		return output;
	}
	/**
	 * @param output the output to set
	 */
	public void setOutput(String output) {
		this.output = output;
	}
	
	public void service(){
		logger.info("External Service Request: "+input);
		setOutput("Hi "+input); // Here your SErvice call to External Service
		logger.info("External Service Response: "+output);
	}
}  



3. Declares the following dependencies
     <dependency>
	  <groupId>log4j</groupId>
	  <artifactId>log4j</artifactId>
	  <version>1.2.17</version>
     </dependency>



4. Log4jMain.java
 package com.javavillage;

import org.apache.log4j.Logger;

import com.javavillage.service.ExternalService;
import com.javavillage.service.InternalService;

public class Log4jMain {
	private final static Logger logger = Logger.getLogger(Log4jMain.class);

	public static void main(String args[]) {
		ExternalService extService = new ExternalService();
		extService.setInput("JavaVillage");
		extService.service();
		logger.info("Response from External Service: "+extService.getOutput());
		
		InternalService intService = new InternalService();
		intService.setInput("Soha");
		intService.service();
		logger.info("Response from Internal Service: "+intService.getOutput());
	}
}


5. Execute the class (Log4jMain.java) with below log4j.property changes
# Root logger option
log4j.rootLogger=INFO, file, stdout
	 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p:: %m%n

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender 
log4j.appender.file.File=F:\\App\\javavaillage.log
log4j.appender.file.MaxFileSize=10000KB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p:: %m%n

# Direct log messages for external service to a log file
log4j.logger.com.javavillage.service.ExternalService=DEBUG, service
log4j.additivity.com.javavillage.service.ExternalService=true
log4j.appender.service=org.apache.log4j.RollingFileAppender 
log4j.appender.service.File=F:\\App\\externalService.log
log4j.appender.service.MaxFileSize=10000KB
log4j.appender.service.MaxBackupIndex=10
log4j.appender.service.layout=org.apache.log4j.PatternLayout
log4j.appender.service.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p:: %m%n


Project Structure

Log4j package Structure

javavaillage.log after execution of Log4jMain

2014-10-28 14:23:32 [main] INFO :: External Service Request: JavaVillage
2014-10-28 14:23:32 [main] INFO :: External Service Response: Hi JavaVillage
2014-10-28 14:23:32 [main] INFO :: Response from External Service: Hi JavaVillage
2014-10-28 14:23:32 [main] INFO :: Internal Service Request: Soha
2014-10-28 14:23:32 [main] INFO :: Internal Service Response: Soha
2014-10-28 14:23:32 [main] INFO :: Response from Internal Service: Hi Soha


externalService.log after execution of Log4jMain

2014-10-28 14:23:32 [main] INFO :: External Service Request: JavaVillage
2014-10-28 14:23:32 [main] INFO :: External Service Response: Hi JavaVillage


Note: If we want apply seperate log file for package, create service entry only for package instead of class in log4j.property and this file creation will be enabled and disabled based on flag for log4j.additivity.com.javavillage.service

log4j.logger.com.javavillage.service=DEBUG, service
log4j.additivity.com.javavillage.service=true