Test whether the springboot log4j2 asynchronous configuration takes effect

Use the following code to test, simple changes are needed

//导入包
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


//定义日志变量
 private static final Logger LOGGER = LogManager.getLogger(A.class);


//多线程测试异步
 Thread currentThread = Thread.currentThread();
        ExecutorService executorService = Executors.newFixedThreadPool(32);
        for (int i = 0; i < 10000; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    LOGGER.info("exec  " + Thread.currentThread().getName());

                    if (LOGGER instanceof AsyncLogger) {
                        // 异步记录日志消息
                        LOGGER.info("This is an async log message");

                        // 让主线程等待异步线程执行完毕
                        // log4j2测试过程,出现报错:WARN [AsyncDefault] Ignoring log event after log4j was shut down: 就把这行注释掉
                        ((AsyncLogger) LOGGER).getContext().stop();
                    } else {
                        System.out.println("Logger is not an AsyncLogger");
                    }
                }
            });
        }
LockSupport.park(currentThread);

Guess you like

Origin blog.csdn.net/Mint6/article/details/130576346