采用spring boot enable的方式重构日志中心代码
代码:https://gitee.com/owenwangwen/open-capacity-platform
日志中心预览
log-spring-boot-starter
pom依赖
AOP标准日志格式
springboot项目中是否使用过EnableCaching
下面咱们按照springboot的方式 编写EnableLogging
@Import(LogImportSelector.class)
LogImportSelector
总结
1.@Import用来导入一个或多个类(会被spring容器托管),或者配置类(配置类里面的bean都会被spring容器托管)
- ImportSelector 该接口的方法的返回值都会被纳入到spring容器管理中
import可以采用importselecor的返回值加入到spring容器
import还可以自定义ImportBeanDefinitionRegistrar接口,通过BeanDefinitionRegistry纳入到spring容器
链路跟踪
spring-cloud-sleuth-core
HP@owen /cygdrive/d/open-capacity-platform
$ grep -rn a26003208271fd94 *
logs/auth-gateway/auth-gateway-info.log:740:[auth-gateway:130.75.131.208:9200] [a26003208271fd94] [a26003208271fd94] 2019-09-06 09:38:33.898 INFO 6364 [XNIO-2 task-3] com.open.capacity.client.filter.ResponseFilter request url = http://127.0.0.1:9200/api-auth/validata/code/1263967C-FED7-4732-86D7-8CC6BDF752F5, traceId = a26003208271fd94
logs/auth-gateway/auth-gateway-info.log:741:[auth-gateway:130.75.131.208:9200] [a26003208271fd94] [a26003208271fd94] 2019-09-06 09:38:36.703 INFO 6364 [XNIO-2 task-3] com.open.capacity.client.filter.ResponseFilter response url http://127.0.0.1:9200/api-auth/validata/code/1263967C-FED7-4732-86D7-8CC6BDF752F5, traceId = a26003208271fd94
oauth-center/logs/auth-server/auth-server-info.log:722:[auth-server:130.75.131.208:8000] [a26003208271fd94] [b26ed6e6877dd47a] 2019-09-06 09:38:34.795 INFO 15168 [http-nio-8000-exec-1] com.open.capacity.log.aop.LogAnnotationAOP 开始请求,transid=a26003208271fd94, url=com.open.capacity.uaa.server.controller.ValidateCodeController/createCode , httpMethod=null, reqData=["1263967C-FED7-4732-86D7-8CC6BDF752F5"]
oauth-center/logs/auth-server/auth-server-info.log:723:[auth-server:130.75.131.208:8000] [a26003208271fd94] [b26ed6e6877dd47a] 2019-09-06 09:38:36.671 INFO 15168 [http-nio-8000-exec-1] com.open.capacity.log.aop.LogAnnotationAOP 请求完成, transid=a26003208271fd94, 耗时=1898, resp=null:
CompletableFuture.runAsync 与链路跟踪
[test-log-center:130.75.131.208:8080] [,d1d8bf482db0af10] 2019-09-16 13:40:25.181 INFO 18088 [http-nio-8080-exec-10] com.open.capacity.log.test.controller.TestController ok
[test-log-center:130.75.131.208:8080] [,d1d8bf482db0af10] 2019-09-16 13:40:25.220 INFO 18088 [pool-1-thread-1] com.open.capacity.log.test.controller.TestController oook
配置bean
package com.open.capacity.log.test.config;
import java.util.concurrent.Executor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@EnableAutoConfiguration
@EnableAsync
// add the infrastructure role to ensure that the bean gets auto-proxied
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class CustomExecutorConfig extends AsyncConfigurerSupport {
@Autowired
BeanFactory beanFactory;
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// CUSTOMIZE HERE
executor.setCorePoolSize(7);
executor.setMaxPoolSize(42);
executor.setQueueCapacity(11);
executor.setThreadNamePrefix("MyExecutor-");
// DON'T FORGET TO INITIALIZE
executor.initialize();
return new LazyTraceExecutor(this.beanFactory, executor);
}
}
异步调用
@Slf4j
@RestController
public class TestController {
@Autowired
BeanFactory beanFactory;
@GetMapping("/test")
public String opt(){
log.info("ok");
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(10000);
log.info("oook");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, new TraceableExecutorService(beanFactory, Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(),
// 'calculateTax' explicitly names the span - this param is optional
"calculateTax"));
log.info("ok");
return "ok" ;
}
}
logback 日志标准格式定义
logback-spring.xml配置
${APP_NAME}
true
${CONSOLE_LOG_PATTERN}
UTF-8
${LOG_FILE}/${APP_NAME}-error.log
${LOG_FILE}/${APP_NAME}-error.%d{yyyy-MM-dd}.%i.log
100MB
60
${CONSOLE_LOG_PATTERN_NO_COLOR}
UTF-8
100MB
ERROR
ACCEPT
DENY
${LOG_FILE}/${APP_NAME}-info.log
${LOG_FILE}/${APP_NAME}-info.%d{yyyy-MM-dd}.%i.log
100MB
60
${CONSOLE_LOG_PATTERN_NO_COLOR}
UTF-8
INFO
ACCEPT
DENY