Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

logging in threads by using log4j

 
suresh reddy reddy
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can any one tell me how to generate loger files for each thread and update the logs for each thread

my code is like this



import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
*
* @author Suresh Reddy
*
*/
public class LoadHandler extends Thread{
public static void main(String[] args) {
for(int i=0;i<10;i++){
new LoadHandler().start();
}
}

public void run() {
org.apache.log4j.Logger log = Logger.getLogger(LoadHandler.class);
Properties props=new Properties();
props.setProperty("log4j.appender.file","org.apache.log4j.RollingFileAppender");
props.setProperty("log4j.appender.file.maxFileSize","100MB");
props.setProperty("log4j.appender.file.maxBackupIndex","100");
props.setProperty("log4j.appender.file.File","D:/log4jtesting/LoadHandler_"+Thread.currentThread().getName()+".log");
props.setProperty("log4j.appender.file.threshold","debug");
props.setProperty("log4j.appender.file.layout","org.apache.log4j.PatternLayout");
props.setProperty("log4j.appender.file.layout.ConversionPattern","%d [%t] %-5p [%-35F : %-25M : %-6L] %-C -%m%n");
props.setProperty("log4j.rootLogger","DEBUG, file");
PropertyConfigurator.configure(props);
log.info("thread started :"+Thread.currentThread().getName());
log.debug("run method :"+Thread.currentThread().getName());

}
}


if we run above code it will generate 10 log files in d:/log4jtesting directory but all threads updated their logs in one log file only
my assumption for this problem is like that

which ever thread start at last will have the log refference and all the threads updating the logs in to that log file only ,


 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create separate logger for each thread


You must also create separate file appender for each logger in the log4j.properties configuration,
if you want to save logs of each thread in different files.
 
suresh reddy reddy
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,the below code is working fine,mean to say ,separate log files are created and messages are got updated successfully .




import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
*
* @author Suresh Reddy
*
*/
public class LoadHandler extends Thread{
public static void main(String[] args) {
for(int i=0;i<10;i++){
new LoadHandler().start();
}
}

public void run() {
// org.apache.log4j.Logger log = Logger.getLogger(LoadHandler.class);
org.apache.log4j.Logger log = Logger.getLogger("Thread" + Thread.currentThread().getName());
Properties props=new Properties();
props.setProperty("log4j.appender.file","org.apache.log4j.RollingFileAppender");

props.setProperty("log4j.appender.file.maxFileSize","100MB");
props.setProperty("log4j.appender.file.maxBackupIndex","100");
props.setProperty("log4j.appender.file.File","D:/log4jtesting/LoadHandler"+Thread.currentThread().getName()+".log");
props.setProperty("log4j.appender.file.threshold","debug");
props.setProperty("log4j.appender.file.layout","org.apache.log4j.PatternLayout");
props.setProperty("log4j.appender.file.layout.ConversionPattern","%d [%t] %-5p [%-35F : %-25M : %-6L] %-C -%m%n");
props.setProperty("log4j.appender.stdout","org.apache.log4j.ConsoleAppender");
//props.setProperty("log4j.appender.stdout.Target","System.out");
props.setProperty("log4j.logger."+"Thread" + Thread.currentThread().getName(),"DEBUG, file");

// props.setProperty("log4j.logger.LoadHandler","DEBUG, file");
PropertyConfigurator.configure(props);
log.info("thread started :"+Thread.currentThread().getName());
log.debug("run method :"+Thread.currentThread().getName());

}
}





Thanks a lot.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic