如何在java中使用多线程中的log4j? [英] how to use log4j in Multithread using java?
本文介绍了如何在java中使用多线程中的log4j?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用log4j为每个线程写日志,日志文件名为workthread..log,第一个线程打印日志文件为workthread-1.log,第二个线程为workthread-2.log等等。
I want to write log for each thread using log4j and log file name will be "workthread..log",first thread print log on file "workthread-1.log" and second thread on "workthread-2.log" and so on.
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
请帮帮我?
问候
推荐答案
也许你可以试试这样的东西:
perhaps you could try somezthing like this:
logger = Logger.getLogger(classname);
logger.removeAllAppenders();
logger.addAppender(new FileAppender(layout, "your-thread-name.log", true));
你可以用thread.getName()这样的东西来获取你的线程的名称。 )
you could get the name of your thread with something like "thread.getName()" i think ;)
这篇关于如何在java中使用多线程中的log4j?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文