aspose file tools*
The moose likes Threads and Synchronization and the fly likes logging in threads by using log4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "logging in threads by using log4j" Watch "logging in threads by using log4j" New topic
Author

logging in threads by using log4j

suresh reddy reddy
Greenhorn

Joined: Jan 19, 2010
Posts: 12
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

Joined: Jun 21, 2008
Posts: 423
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

Joined: Jan 19, 2010
Posts: 12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: logging in threads by using log4j