As for instance, we want to log workflow related log messages into a file named workflow.log while services related logs into a file named services.log.
The log4j.properties file will describe two seperate FileAppenders and each of them is associated with individual package.The log4j.properties file as shown below depicts two FileAppenders:
log4j.rootLogger=DEBUG # WorkflowFileAppender - logs messages in the workflow.log file. log4j.appender.WorkflowFileAppender=org.apache.log4j.FileAppender log4j.appender.WorkflowFileAppender.File=workflow.log log4j.appender.WorkflowFileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.WorkflowFileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n # ServicesFileAppender - logs messages in the services.log file. log4j.appender.ServicesFileAppender=org.apache.log4j.FileAppender log4j.appender.ServicesFileAppender.File=services.log log4j.appender.ServicesFileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.ServicesFileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n log4j.logger.com.iqjava.workflow=,WorkflowFileAppender log4j.logger.com.iqjava.services=,ServicesFileAppender
Here the root logger is set as 'DEBUG' which is just for information purpose, and not necessary.We have not set any logger level with individual file appenders. They both have been left blank so that rootLogger level would be inherited.We can surely change that if we want.
The code snippet contains two java files:
1. WorkflowExample.java
package com.iqjava.workflow; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class WorkflowExample { static Logger logger = Logger.getLogger(WorkflowExample.class); public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); logger.debug("Here goes the debug message"); logger.error("Here goes the error message"); logger.fatal("Here goes the fatal message"); logger.info("Here goes the info message"); logger.warn("Here goes the warn message"); } }
2.ServicesExample.java
package com.iqjava.services; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class ServicesExample { static Logger logger = Logger.getLogger(ServicesExample.class); public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); logger.debug("Here goes the debug message"); logger.error("Here goes the error message"); logger.fatal("Here goes the fatal message"); logger.info("Here goes the info message"); logger.warn("Here goes the warn message"); } }
If the individual java appenders are executed then output of workflow.log and services.log will look like as given below:
3.workflow.log
0 [main] DEBUG com.iqjava.workflow.WorkflowExample - Here goes the debug message 33 [main] ERROR com.iqjava.workflow.WorkflowExample - Here goes the error message 33 [main] FATAL com.iqjava.workflow.WorkflowExample - Here goes the fatal message 33 [main] INFO com.iqjava.workflow.WorkflowExample - Here goes the info message 34 [main] WARN com.iqjava.workflow.WorkflowExample - Here goes the warn message
4.services.log
0 [main] DEBUG com.iqjava.services.ServicesExample - Here goes the debug message 1 [main] ERROR com.iqjava.services.ServicesExample - Here goes the error message 1 [main] FATAL com.iqjava.services.ServicesExample - Here goes the fatal message 2 [main] INFO com.iqjava.services.ServicesExample - Here goes the info message 2 [main] WARN com.iqjava.services.ServicesExample - Here goes the warn message
If you want to change the logger level for these file appenders, you can always do that in log4j.properties file.