前段时间项目里因为多线程的问题对log4j进行了一下学习,今天有空汇总一下。

需求是有一个多线程程序,需要对每个线程的日志单独按级别存储。如下图所示。

log4j多线程以及分文件输出日志

 

 项目代码不方便发出,简单写一个demo。

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;

public class TestLog {
    private static Logger log = LogManager.getLogger(TestLog.class);
    public static void main(String[] args) {

        Thread[] threads = new Thread[10];
        for(int i=0;i<10;i++){
            threads[i] = new TestThread("-"+i);
        }
        for(Thread thread : threads) {
            thread.start();
        }
    }
}
TestLog

相关文章:

  • 2021-11-27
  • 2021-11-27
  • 2021-11-27
  • 2021-04-05
  • 2021-11-27
  • 2021-11-27
  • 2021-11-27
  • 2021-11-27
猜你喜欢
  • 2021-11-27
  • 2021-08-21
  • 2021-11-27
  • 2021-12-27
  • 2021-10-04
  • 2021-11-27
  • 2021-11-27
相关资源
相似解决方案