I am working on logging with log4j, i have sample code which creats certain number of threads , i want to use different log files for each threads. so I have use the PropertyConfigurator.configure() which takes a property object.
I have configured the property object in my code i.e hardcoded , but I want to load it external to code.
Now I want to provide all the configurations externally through a property file i.e not to hardcode them in code and at runtime add the two properties -
props.setProperty("log4j.logger."+"Thread" +
Thread.currentThread().getName(),"DEBUG, file");
and
props.setProperty("log4j.appender.file.File",
"/home/ekhaavi/workspace/TEST_2/ThreadHandler"+
Thread.currentThread().getName()+".log");
since these variables like Thread.currentThread().getName() will get evaluated at runtime.
can anyone suggest me how to do it
I have three class --> MainClass, Parser(Thread class), LoggerClass in package
import java.util.HashMap;
import java.util.Map;
public class MainClass {
private static final org.apache.log4j.Logger log = LoggerClass.getLogger(MainClass.class);
public static void main(String args[]){
Map map = new HashMap();
map.put("Subject", "Math");
log.info("The value of the map is " + map.toString());
for(int ii=0; ii< 3; ii++){
Parser th = new Parser(ii);
}
}
}
package com.test.log4j.conf;
public class Parser implements Runnable{
private org.apache.log4j.Logger log;
Parser(){
Thread th = new Thread(this);
th.start();
}
@Override
public void run() {
log = LoggerClass.getThreadLogger("Thread" + Thread.currentThread().getName());
log.info("------dagdjlagdjgasdjljgljg");
System.out.println("The thread is " + Thread.currentThread().getName());
}
}
and finally my logger class is -->
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public abstract class LoggerClass {
public static Logger getLogger(Class clazz){
org.apache.log4j.Logger log = Logger.getLogger(clazz);
Properties props=new Properties();
props.setProperty("log4j.appender.file","org.apache.log4j.RollingFileAppender");
props.setProperty("log4j.appender.logfile","org.apache.log4j.DailyRollingFileAppender");
props.setProperty("log4j.appender.logfile.DatePattern","'.'yyyy-MM-dd");
props.setProperty("log4j.appender.logfile.layout","org.apache.log4j.PatternLayout");
props.setProperty("log4j.appender.logfile.layout.ConversionPattern","%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n");
props.setProperty("log4j.appender.logfile.File","/home/ekhaavi/workspace/TEST_2/mylogfile.log");
props.setProperty("log4j.logger.com.com.test.log4j.conf","INFO, logfile");
PropertyConfigurator.configure(props);
return log;
}
public static Logger getThreadLogger(String str){
org.apache.log4j.Logger log = Logger.getLogger(str);
Properties props=new Properties();
props.setProperty("log4j.appender.file","org.apache.log4j.DailyRollingFileAppender");
props.setProperty("log4j.appender.file.DatePattern","'.'yyyy-MM-dd");
props.setProperty("log4j.appender.file.layout","org.apache.log4j.PatternLayout");
props.setProperty("log4j.appender.file.File","/home/ekhaavi/workspace/TEST_2/ThreadHandler"+Thread.currentThread().getName()+".log");
props.setProperty("log4j.appender.file.layout.ConversionPattern","%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n");
props.setProperty("log4j.logger."+"Thread" + Thread.currentThread().getName(),"DEBUG, file");
PropertyConfigurator.configure(props);
return log;
}
}