nicelog是一个功能强大的Java日志组件,它可以自动、手动收集日志,并通过traceId将日志关联起来,可极大提高排查问题的速度。
官网:https://www.yuque.com/knifeblade/opensource/nicelog
gitee:https://gitee.com/knifeedge/nicelog
github:https://github.com/knife-blade/nicelog
1.介绍
nicelog:功能强大的Java日志组件。
2.功能
1. 手动打印
手动打印日志。
2. 自动收集日志
在三处收集日志:进入时、返回时、报异常时。
默认情况下,只要你的项目里有相关的组件,比如XXL-JOB,就会自动收集其日志。
当前支持的组件有:
Controller
XXL-JOB
Bean的方法或者类上加@NiceLog注解
Feign
RabbitMQ
RocketMQ
Kafka
Scheduled
3. 更多功能
准备支持:我自己暂时想不到了。如果有需求请提issue
3.快速开始
1. 引入依赖
<dependency>
<groupId>com.suchtool</groupId>
<artifactId>nicelog-spring-boot-starter</artifactId>
<version>{newest-version}</version>
</dependency>
2.使用示例
@Api(tags = "测试类")
@RequestMapping("test")
@RestController
public class HelloController {
@ApiOperation("测试1")
@PostMapping("test1")
public String test(User user, String email) {
NiceLogUtil.createBuilder()
.mark("我的打印")
.info();
return "success";
}
}
日志输出结果:
2024-02-29 19:15:20.607 INFO 5840 --- [nio-8080-exec-1] c.s.n.p.impl.NiceLogProcessDefaultImpl : nicelog日志:{"param":"{\"userName\":\"Tony\"}","returnValue":null,"mark":null,"businessNo":null,"errorInfo":null,"throwable":null,"other1":null,"other2":null,"other3":null,"other4":null,"other5":null,"other6":null,"other7":null,"other8":null,"other9":null,"other10":null,"appName":"","entryType":"CONTROLLER","entry":"/test/test1","entryClassTag":"测试","entryMethodTag":"测试1","className":"com.knife.example.controller.HelloController","classTag":"测试","methodName":"test","methodTag":"测试1","methodDetail":"com.knife.example.controller.HelloController.test(java.lang.String)","codeLineNumber":null,"level":"INFO","directionType":"IN","traceId":"3d250d34d8914de5b847fea3ba93d7a7","logTime":"2024-02-29 19:15:20.591","clientIp":"10.0.10.110","ip":"10.0.10.110"}
2024-02-29 19:15:20.619 INFO 5840 --- [nio-8080-exec-1] c.s.n.p.impl.NiceLogProcessDefaultImpl : nicelog日志:{"param":null,"returnValue":null,"mark":"我的打印","businessNo":null,"errorInfo":null,"throwable":null,"other1":null,"other2":null,"other3":null,"other4":null,"other5":null,"other6":null,"other7":null,"other8":null,"other9":null,"other10":null,"appName":"","entryType":"MANUAL","entry":"/test/test1","entryClassTag":"测试","entryMethodTag":"测试1","className":"com.knife.example.controller.HelloController","classTag":null,"methodName":"test","methodTag":null,"methodDetail":null,"codeLineNumber":"23","level":"INFO","directionType":"INNER","traceId":"3d250d34d8914de5b847fea3ba93d7a7","logTime":"2024-02-29 19:15:20.619","clientIp":"10.0.10.110","ip":"10.0.10.110"}
2024-02-29 19:15:20.620 INFO 5840 --- [nio-8080-exec-1] c.s.n.p.impl.NiceLogProcessDefaultImpl : nicelog日志:{"param":null,"returnValue":"\"success\"","mark":null,"businessNo":null,"errorInfo":null,"throwable":null,"other1":null,"other2":null,"other3":null,"other4":null,"other5":null,"other6":null,"other7":null,"other8":null,"other9":null,"other10":null,"appName":"","entryType":"CONTROLLER","entry":"/test/test1","entryClassTag":"测试","entryMethodTag":"测试1","className":"com.knife.example.controller.HelloController","classTag":"测试","methodName":"test","methodTag":"测试1","methodDetail":"com.knife.example.controller.HelloController.test(java.lang.String)","codeLineNumber":null,"level":"INFO","directionType":"OUT","traceId":"3d250d34d8914de5b847fea3ba93d7a7","logTime":"2024-02-29 19:15:20.620","clientIp":"10.0.10.110","ip":"10.0.10.110"}
4 使用说明
1. 打印日志
默认情况下,会通过logback输出。
支持自定义处理日志:提供一个Bean,实现com.suchtool.nicelog.process.NiceLogProcess的void process(NiceLogInnerBO niceLogInnerBO)方法即可。 例如:
@Component
public class CustomLogProcessor implements NiceLogProcess {
@Override
public void process(NiceLogInnerBO logInnerBO) {
// 这里可以这么做:
// 1.取出logInnerBO的字段值,赋值到项目本身的日志实体类
// 2.打印到控制台或者上传到ES等
}
}
2. 自动收集日志
自动收集相关组件的日志。原理:使用AOP。
3. 手动打印日志
支持手动打印日志:
NiceLogUtil.createBuilder()
.mark("创建订单")
.info();
此工具支持通过lambda的形式构造参数并打印,每次输入.都会有代码提示,类似于MyBatis-Plus的lambdaQuery。
4. 手动收集日志
在方法或者是类上加@NiceLog,即可收集出入参、返回值、异常信息。
注意:此类必须注入Spring,方法必须是public。
5.详细配置
5.1 yml配置
支持SpringBoot的配置文件进行配置,比如:application.yml。
配置描述默认值
suchtool.nicelog.enabled启用日志true
suchtool.nicelog.collectAll收集所有true
suchtool.nicelog.enableControllerLog启用Controller日志true
suchtool.nicelog.enableXxlJobLog启用XXL-JOB日志true
suchtool.nicelog.enableRabbitMQLog启用RabbitMQ日志true
suchtool.nicelog.enableRocketMQLog启用RocketMQ日志true
suchtool.nicelog.enableKafkaLog启用Kafka日志true
suchtool.nicelog.enableNiceLogAnnotationLog启用@NiceLog日志true
suchtool.nicelog.enableFeignLog启用Feign日志true
suchtool.nicelog.enableScheduledLog启用@Scheduled日志true
suchtool.nicelog.ignoreFeignLogPackageName不收集Feign日志的包名,多个用逗号隔开空
suchtool.nicelog.feignTraceIdHeaderfeign的traceId的header名字nice-log-trace-id
5.2 日志开关
默认会自动收集所有支持组件的日志。可以自由的开关:
场景1:不收集某个组件假如不收集Kafka的日志,就这样配置yml:suchtool.nicelog.enableKafkaLog=false
场景2:关闭所有组件,只收集标有@NiceLog的类或方法配置yml:suchtool.nicelog.collectAll=false
场景3:不收集某个类或方法在类或者方法上加注解:@NiceLogIgnore
5.3 设置优先级
日志自动收集功能是通过AOP实现的。你可以手动指定它们的优先级:在SpringBoot的启动类上加如下注解即可:
@EnableNiceLog(controllerLogOrder = 1, rabbitMQLogOrder = 2, xxlJobLogOrder = 3,
niceLogAnnotationLogOrder = 4, rocketMQLogOrder = 6, kafkaLogOrder = 3,
feignLogOrder = 5, feignRequestInterceptorOrder = 6, scheduledLogOrder = 5)
比如:
package com.knife.example;
import com.suchtool.nicelog.annotation.EnableNiceLog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableNiceLog(controllerLogOrder = 1, rabbitMQLogOrder = 2, xxlJobLogOrder = 3)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5.4 收集Feign原始响应body
如果你没有自定义Feign的Decoder,是能够收集到原始响应body的。 如果你自定义了Feign的Decoder,则需要手动保存一下body,这样后边就能打印出来,例如:
@Configuration
public class FeignLogResponseDecoder extends SpringDecoder {
public FeignLogResponseDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
super(messageConverters);
}
@Override
public Object decode(final Response response, Type type) throws IOException, FeignException {
Response.Body body = response.body();
String bodyString = StreamUtils.copyToString(body.asInputStream(), StandardCharsets.UTF_8);
// 这里将body保存下来
NiceLogFeignContextThreadLocal.saveOriginFeignResponseBody(bodyString);
// body流只能读一次,必须重新封装一下
Response newResponse = response.toBuilder().body(bodyString, StandardCharsets.UTF_8).build();
return super.decode(newResponse, type);
}
}
6. 字段的含义
Key含义备注
param入参手动时可自定义
returnValue返回值手动时可自定义
originReturnValue原始返回值手动时可自定义
mark标记手动时可自定义
businessNo业务单号手动时可自定义
errorInfo错误信息手动时可自定义
errorDetailInfo错误详细信息手动时可自定义
throwableThrowable异常类手动时可自定义。栈追踪字符串会自动保存到NiceLogInnerBO.stackTrace
printStackTrace打印栈追踪手动时可自定义。用于非异常时主动获得栈追踪,会将栈追踪字符串会保存到NiceLogInnerBO.stackTrace。若throwable不为空,则使用throwable的栈数据
appName应用名字取的是spring.application.name配置
entryType入口类型MANUAL:手动;CONTROLLER:接口;RABBIT_MQ:RabbitMQ;XXL_JOB:XXL-JOB;NICE_LOG_ANNOTATION:NiceLog注解;FEIGN:Feign; ROCKETMQ:RocketMQ;KAFKA:Kafka
entry入口对于Controller,是URL;对于RabbitMQ,是@RabbitListener的queues;对于XXL-JOB,是@XxlJob的value;对于Feign,是URL;对于RocketMQ,是@RocketMQMessageListener的topic字段;对于Kafka,是@KafkaListener的topics字段。作为上下文传递。
entryClassTag入口类的tag取值优先级为:先取@NiceLog的value,若为空则取:对于Controller:Controller类上的@Api的tags > Controller类上的@Api的value;对于Feign:@FeignClient的value字段。作为上下文传递。
entryMethodTag入口方法的tag取值优先级为:@NiceLog的value > Controller方法上的@ApiOperation的value。作为上下文传递。
className类名
classTag当前类的tag取值同entryClassTag,但不作为上下文传递。
methodName方法名
methodTag当前方法的tag取值同entryMethodTag,但不作为上下文传递。
methodDetail方法详情全限定类名+方法名+全限定参数
codeLineNumber代码所在的行数只在手动输出时有值。
level级别INFO、WARNING、ERROR
directionType方向IN:方法进入;OUT:方法退出;INNER:方法内部执行
traceId链路id作为上下文传递
stackTrace栈追踪字符串
logTime日志时间
clientIp客户端IP
ip调用方IP
other1备用字段1手动时可自定义
other2备用字段2手动时可自定义
other3备用字段3手动时可自定义
other4备用字段4手动时可自定义
other5备用字段5手动时可自定义
other6备用字段6手动时可自定义
other7备用字段7手动时可自定义
other8备用字段8手动时可自定义
other9备用字段9手动时可自定义
other10备用字段10手动时可自定义