首页 > 编程语言 >nicelog--强大的Java日志组件,排查问题的利器

nicelog--强大的Java日志组件,排查问题的利器

时间:2024-03-19 19:24:51浏览次数:20  
标签:Java 自定义 nicelog 手动 -- suchtool 日志 null

 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,就会自动收集其日志。

当前支持的组件有:

  1. Controller
  2. XXL-JOB
  3. Bean的方法或者类上加@NiceLog注解
  4. Feign
  5. RabbitMQ
  6. RocketMQ
  7. Kafka
  8. 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.feignTraceIdHeader

feign的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

错误详细信息

手动时可自定义

throwable

Throwable异常类

手动时可自定义。栈追踪字符串会自动保存到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

手动时可自定义

标签:Java,自定义,nicelog,手动,--,suchtool,日志,null
From: https://www.cnblogs.com/knifeblade/p/18083734

相关文章

  • shell执行sh
    假设我们有一个名为“shell_script”的脚本文件,文件内容如下#!/bin/bash然后我们准备执行这个文件$chmodu+xshell_script$./shell_script当我们执行./shell_script这行命令的时候由于脚本添加了shebang,相当于在命令行这样执行:/bin/bashshell_script  #!/usr/bin/ba......
  • C++ static和const
    const定义的常量在超出其作用域之后其空间会被释放;static定义的静态常量在函数执行后不会释放其存储空间;1.staticstatic表示的是静态的。类的静态成员函数、静态成员变量是和类相关的,而不是和类的具体对象相关的。即使没有具体对象,也能调用类的静态成员函数和成员......
  • RGBLCD基本知识
    学习资料来源于网络https://www.bilibili.com/video/BV1yE411h7uQ?p=55&spm_id_from=pageDriver&vd_source=432ba293ecfc949a4174ab91ccc526d6https://doc.embedfire.com/linux/imx6/driver/zh/latest/bare_metal/elcdif.html# 基础概念:LCD:LiquidCrystalDisplay液晶是一......
  • 传输层协议
    传输层协议TCP/IP协议TCP/IP是一个ProtocolStack,包括TCP、IP、UDP、ICMP、RIP、TELNET、FTP、SMTP、ARP等许多协议TCP/IP和OSI模型的比较相同点两者都是以协议栈的概念为基础协议栈中的协议彼此相互独立下层对上层提供服务不同点OSI是先有模型;TCP/IP是先有协议,后有......
  • Ubuntu搭建Samba服务
    Ubuntu搭建Samba服务使用samba​服务可以跨系统的进行文件共享,安装samba​服务的步骤如下:安装步骤安装基础软件sudoapt-getinstallsambasudoapt-getinstallsmbclient#验证安装samba-V目录配置使用编辑器打开配置文件,以vim​​为例sudovim/etc/samba......
  • 邮件客户端的配置使用
    软件介绍:winmail收到新邮件不会提示用户,这样用户就容易错过消息下载foxmail可以解决这个问题一:下载foxmail1.实体机进入官网https://www.foxmail.com/,点击下载版本随意下载好后拖到虚拟机上二:配置foxmail1.开始创建账号因为这几种邮箱都不是我们上个实验的邮箱选择其他邮......
  • Prufer序列
    基本信息定义:prufer序列是无根树和序列的双向映射,并且描述了节点读书以及父节点的信息。使用场景:将构造树的问题转化为构造序列,将统计树的问题转化为统计序列,将树的dp转化为序列的dp。如何得到prufer序列?统计树上的所有节点的度数\(d_i\)。找到所有度数为\(1\)的节点......
  • 在Excel中如何获取汉字拼音首字母?
    在工作中,为了方便查询、排序或者编码,需要生成汉字内容信息的首字母,如果信息很多的话,一个个录入是一件很麻烦的事情,下面给大家介绍一个便捷的方法,轻松获取汉字拼音首字母信息。最终效果如下:一、新建Excel文件新建一个Excel文件,输入示例汉字内容。二、设置【开发工具】菜单【文件】-......
  • GTC 2024 开幕,英伟达发布新一代 GPU 架构;Apple ID 或将淘汰丨 RTE 开发者日报 Vol.168
       开发者朋友们大家好: 这里是「RTE开发者日报」,每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享RTE(RealTimeEngagement)领域内「有话题的新闻」、「有态度的观点」、「有意思的数据」、「有思考的文章」、「有看点的会议」,但内容仅代表编辑的个人......
  • HTML学习记录(2)(HTML常用标签)
    一,p与hrp段落可以将HTML文段分割为若干段落。段落常用<p>标签来定义,用align来定义段落的对齐方式实列<p>我是第一个段落</p><p>我是第二个段落</p><palign="left">我在左边</p><palign="center">我在中间</p><palign="right">我在右边</p>......