在实际的业务开发中我们会遇到以下场景:我们为服务开启监控线程,然后按照业务性质可以将线程分为业务线程和监控线程,我们想要将业务线程的日子全部打印到业务日志中,而监控线程的日志全部打印到监控日志中。一种方法是我们根据log4j2日志进行配置,通过指定Logger的方式实现;本文将提供另一种实现方式,根据线程名称打印不同的日志文件。插件说明可以参考上一章日志框架LOG4J2系列四——log4j2的plugins(插件)使用
<properties>
<log4j.version>2.14.1log4j.version>
properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>${log4j.version}version>
dependency>
dependencies>
这里需要使用@Plugin和StrLookup,log4j-slf4j-impl会引入log4j2-core,但是限定了scope是runtime,我们必须手动引入log4j2-core组件。
首先我们先要拓展一个StrLookUp插件,使用@Plugin表明此时一个log4j2的插件,定义插件的名称和属性
package com.demo.log.plugins;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;
// 表明是一个log4j2的插件,且是拓展了StrLookup 的插件
@Plugin(name = "threadName", category = StrLookup.CATEGORY)
public class ThreadName implements StrLookup {
// 插件的功能即根据key值获取相应结果,这里我们直接返回线程名字
@Override
public String lookup(String key) {
return Thread.currentThread().getName();
}
@Override
public String lookup(LogEvent event, String key) {
return event.getThreadName() == null ? Thread.currentThread().getName()
: event.getThreadName();
}
}
使用Routing,根据插件lookup的结果即线程名称选择路由routes,routes下面每个route都是一个RollingFile,这样就能将线程打印到不同的地方
<Configuration name="log-demo-config" status="error" monitorInterval="10">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{yyyy-MM-dd;HH:mm:ss.SSS Z}] [%-5p] [%t] [%c] %m%n">PatternLayout>
Console>
<Routing name="Routing">
<Routes pattern="$${threadName:threadName}">
<Route>
<RollingFile name="RollingFile-${threadName:threadName}"
fileName="J:\JavaWorkSpace\SpringLearning\src\main\resources\log\thread\thread-${threadName:threadName}.log"
filePattern="J:\JavaWorkSpace\SpringLearning\src\main\resources\log\thread\thread-${threadName:threadName}-%i.log">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss,SSS Z}] [%-5p] [%t] [%c %L] %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
Policies>
<DefaultRolloverStrategy max="10"/>
RollingFile>
Route>
Routes>
Routing>
Appenders>
<Loggers>
<Logger name="com.demo.log" level="INFO" additivity="false">
<AppenderRef ref="Routing">AppenderRef>
Logger>
Loggers>
Configuration>
package com.demo.log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class LogDemo {
private static final Logger LOGGER = LoggerFactory.getLogger(LogDemo.class);
private static ThreadPoolExecutor threadPoolExecutor;
public static void main(String[] args) {
threadPoolExecutor = new ThreadPoolExecutor(10, 20, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>());
for (int i = 0; i < 10; i++) {
int threadNum = i;
threadPoolExecutor.execute(() -> {
while (true) {
LOGGER.info("thread-" + threadNum);
}
});
}
}
}
运行logdemo查看效果,将在log\thread下面按线程保存不同日志文件:
[2021-09-22 22:42:30,174 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,241 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,274 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,274 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,275 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,275 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,276 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,289 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,289 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,290 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,290 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,290 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,292 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,293 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,293 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,293 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,301 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0
[2021-09-22 22:42:30,323 +0800] [INFO ] [pool-2-thread-1] [com.demo.log.LogDemo 22] thread-0