记录不同日志文件中的线程

时间:2014-01-27 07:21:47

标签: java multithreading logging log4j

我有java类在java中使用线程。目前我使用两个线程,我希望每个线程都将它的日志写入自己的日志文件中。我正在使用log4j。但是我的java程序向我展示了一种异常行为。我能够创建两个不同的日志文件,但我可以看到,在我的第一个日志文件中,有来自一个或两个线程的日志消息,而在第二个日志文件中只有第二个线程消息,但我想要单独记录两个线程。

这是我的帖子文件

public class MainController extends Thread{   
 public static synchronized void main(String args[])
 {  
  ServerAccess t1=new ServerAccess("IP","root","zoomcallrec","cat /var/log/callrec/core1.log","com.ef.ThreadOne","Thread One");  
  ServerAccess t2=new ServerAccess("IP","root","zoomcallrec","cat /var/log/callrec/core1.log","com.ef.ThreadTwo","Thread Two");  
  t1.start();  

  t2.start();  
 }  
} 

这是我的log4j文件

log4j.logger.com.ef.ThreadOne=DEBUG, ThreadFirst

log4j.appender.ThreadFirst=org.apache.log4j.RollingFileAppender
log4j.appender.ThreadFirst.File=C:/AnalyzerLog/ThreadOne/Analyzer.log
log4j.appender.ThreadFirst.MaxFileSize=1000KB
log4j.appender.ThreadFirst.MaxBackupIndex=10
log4j.appender.ThreadFirst.layout=org.apache.log4j.PatternLayout
log4j.appender.ThreadFirst.layout.ConversionPattern=%d [%t] (%F:%L) %-5p - %m%n

log4j.logger.com.ef.ThreadTwo=DEBUG, ThreadTwo

log4j.appender.ThreadTwo=org.apache.log4j.RollingFileAppender
log4j.appender.ThreadTwo.File=C:/AnalyzerLog/ThreadTwo/Analyzer.log
log4j.appender.ThreadTwo.MaxFileSize=1000KB
log4j.appender.ThreadTwo.MaxBackupIndex=10
log4j.appender.ThreadTwo.layout=org.apache.log4j.PatternLayout
log4j.appender.ThreadTwo.layout.ConversionPattern=%d [%t] (%F:%L) %-5p - %m%n

这是我的程序正在生成的日志文件。这是线程一个日志文件,它为线程1和线程2生成日志

2014-01-24 15:24:13,688 [Thread-1] (ServerAccess.java:76) DEBUG - Server is connect
2014-01-24 15:24:13,693 [Thread-0] (ServerAccess.java:76) DEBUG - Server is connect
2014-01-24 15:24:14,329 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter0
2014-01-24 15:24:14,414 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter1
2014-01-24 15:24:14,474 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter2
2014-01-24 15:24:14,534 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter3
2014-01-24 15:24:14,545 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter0
2014-01-24 15:24:14,589 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter4
2014-01-24 15:24:14,635 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter1
2014-01-24 15:24:14,639 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter5
2014-01-24 15:24:14,695 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter2
2014-01-24 15:24:14,724 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter6
2014-01-24 15:24:14,755 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter3
2014-01-24 15:24:14,809 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter7
2014-01-24 15:24:14,811 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter4
2014-01-24 15:24:14,861 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter5
2014-01-24 15:24:14,895 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter8
2014-01-24 15:24:14,947 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter6
2014-01-24 15:24:14,957 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter9
2014-01-24 15:24:15,019 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter10
2014-01-24 15:24:15,032 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter7
2014-01-24 15:24:15,079 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter11
2014-01-24 15:24:15,120 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter8
2014-01-24 15:24:15,139 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter12
2014-01-24 15:24:15,180 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter9
2014-01-24 15:24:15,194 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter13
2014-01-24 15:24:15,241 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter10
2014-01-24 15:24:15,281 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter14
2014-01-24 15:24:15,301 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter11
2014-01-24 15:24:15,362 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter12
2014-01-24 15:24:15,367 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter15
2014-01-24 15:24:15,417 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter16
2014-01-24 15:24:15,417 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter13
2014-01-24 15:24:15,503 [Thread-1] (ServerAccess.java:97) DEBUG - Thread isThread TwoCounter17
2014-01-24 15:24:15,504 [Thread-0] (ServerAccess.java:97) DEBUG - Thread isThread OneCounter14

这是线程2日志文件,它仅为自己的线程生成日志

2014-01-27 10:56:56,582 [Thread-1] (ServerAccess.java:77) DEBUG - Server is connect
2014-01-27 10:56:56,774 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter0
2014-01-27 10:56:56,897 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter1
2014-01-27 10:56:56,957 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter2
2014-01-27 10:56:57,017 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter3
2014-01-27 10:56:57,072 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter4
2014-01-27 10:56:57,122 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter5
2014-01-27 10:56:57,207 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter6
2014-01-27 10:56:57,292 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter7
2014-01-27 10:56:57,380 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter8
2014-01-27 10:56:57,440 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter9
2014-01-27 10:56:57,500 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter10
2014-01-27 10:56:57,560 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter11
2014-01-27 10:56:57,620 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter12
2014-01-27 10:56:57,686 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter13
2014-01-27 10:56:57,771 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter14
2014-01-27 10:56:57,856 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter15
2014-01-27 10:56:57,906 [Thread-1] (ServerAccess.java:98) DEBUG - Thread isThread TwoCounter16

2 个答案:

答案 0 :(得分:0)

我认为log4j不支持这种分离。您应该考虑使用logback框架(无论如何都不推荐使用log4j)。 Logback附带一个SiftingAppender用于日志分离。有关详细信息:http://logback.qos.ch/manual/loggingSeparation.html

使用此SiftingAppender并将您的线程ID作为鉴别器应该不会那么困难。

答案 1 :(得分:0)

在实例化ServerAccess个对象时,请为它们提供您希望它们使用的记录器,如下所示:

public class MainController { static void main (String[] args){ Logger l1 = Logger.getLogger("log4j.logger.com.ef.ThreadOne"); Logger l2 = Logger.getLogger("log4j.logger.com.ef.ThreadTwo"); ServerAccess t1 = new ServerAccess(l1); ServerAccess t2 = new ServerAccess(l2); t1.start(); t2.start(); }

然后,在ServerAccess内,使用该记录器:

public class ServerAccess extends Thread { private final Logger logger; public ServerAccess(Logger logger){ this.logger = logger; } public void Start() { logger.debug("Debug Statement"); } }