我想用log4j爲每個線程寫日誌,日誌文件名爲「workthread..log」,第一個線程打印日誌文件「workthread-1.log」,第二個線程在「workthread-2.log」上等等。如何在使用java的多線程中使用log4j?
class MyRunnable implements Runnable
{
private Logger logger=null;
public MyRunnable()
{
DOMConfigurator.configure(this.getClass().getClassLoader().getResource(LOG4J_FILEPATH));
logger =Logger.getLogger(classname);
}
public void run()
{
logger.info("Important job running in MyRunnable"+Thread.currentThread().getName());
}
}
public class TestThreads
{
public static void main (String [] args)
{
Thread[] worker=new Thread[3];
MyRunnable r = new MyRunnable();
for(int i=0;i<3;i++) {
worker[i]=new Thread(r);
worker[i].start();
}
}
}//class
請幫幫我嗎?
問候
和你的問題是什麼?代碼在做什麼?有沒有錯誤? –