java 注入日志_GitHub - xiaoyu2xu/autologging: 一个非常强大的监控日志输出框架,支持 SpringAOP 和动态字节码注入两种方式输出方法执行的监控日志,而且包含链...

AutoLogging 监控日志框架

一个非常强大的监控日志输出框架,主要功能包含

方法监控日志输出

应用内调用链路追踪

可以通过

SpringAOP

动态字节码运行时注入

两种方式引入,只要一个注解即可开启所有功能,而且具有强大的可扩展性

背景

在开发过程中,我们常常会看到这样的代码:

@GetMapping("{id}")

public ReturnDTO getById(int id) {

log.info("根据主键id查询用户, id: {}", id);

long startTime = System.currentTimeMillis();

try {

ReturnDTO rs = ResultUtil.successResult(userService.getById(id));

log.debug("根据主键id查询用户成功, id: {}, 耗时: {}", id, (System.currentTimeMills() - startTime));

return rs;

} catch (Exception e) {

log.error("据主键id查询用户发生异常, id: {}, 耗时: {}", id, (System.currentTimeMills() - startTime), e);

return ResultUtil.errorResult("据主键id查询用户发生异常, id: " + id);

}

}

这种写法有以下几个问题:

代码冗余,不易维护

日志格式不统一,难以自动化分析

容易遗漏或者写错日志信息,问题排查艰难

这些问题都给开发、测试和线上排查问题造成困扰

想象一下,整个项目当中,我们有多少个像这样的方法?DRY!

特点

只需要添加 maven 依赖,然后给全局打上一个注解,就能开启所有功能,我们就可以去掉所有监控日志,由框架统一输出:

@GetMapping("{id}")

public ReturnDTO getById(int id) {

return ResultUtil.successResult(userService.getById(id));

}

自动日志框架会自动输出方法的监控日志

效果展示

2020-03-01 13:38:46.483 INFO 54635 --- [ main] c.g.d.a.c.l.LocalLogTraceListener : 121361440260956160 | 2 | Repository | | com.github.dadiyang.autologging.test.user.UserMapperFakeImpl | getById | [830293] | {"id":830293,"username":"张三"} | 4

2020-03-01 13:38:46.484 INFO 54635 --- [ main] c.g.d.a.c.l.LocalLogTraceListener : 121361440260956160 | 1 | Service | | com.github.dadiyang.autologging.test.user.UserServiceImpl | getById | [830293] | {"id":830293,"username":"张三"} | 27

2020-03-01 13:38:46.485 INFO 54635 --- [ main] c.g.d.a.c.l.LocalLogTraceListener : 121361440260956160 | 0 | Controller | GET 127.0.0.1 http://localhost/user/getById | com.github.dadiyang.autologging.test.user.UserController | getById | [830293] | {"id":830293,"username":"张三"} | 39

2020-03-01 13:38:46.528 ERROR 54635 --- [ main] c.g.d.a.c.l.LocalLogTraceListener : 121361440592306176 | 2 | Repository | | com.github.dadiyang.autologging.test.user.UserMapperFakeImpl | updateById | [{"id":830293,"username":"张三"}] | 0 | java.lang.UnsupportedOperationException: 模拟抛出异常

at com.github.dadiyang.autologging.test.user.UserMapperFakeImpl.updateById(UserMapperFakeImpl.java:14)

at com.github.dadiyang.autologging.test.user.UserMapperFakeImpl$$FastClassBySpringCGLIB$$8b95b1d3.invoke()

at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

项目结构

本框架分为 core、aop、aop-jvm-sandbox、test 四个项目,其中

core 核心模块,包含配置和上报相关的日志监听器的定义;

aop 通过 SpringAOP 打印和上报方法执行的监控信息,包括对 Controller、 Service 和 Mapper 层的监控日志,以及统一异常处理;

aop-jvm-sandbox 基于 jvm-sandbox 框架实现的无侵入的 AOP 日志实现,它提供了一个能力,让应用完全无感的情况下,在运行时动态将监控日志相关代码织入到目标应用中,使之拥有输入监控日志的能力

test 用于测试

快速开始

基于 aop 的方法级监控日志

以 SpringBoot 环境使用为例

maven 依赖

org.springframework.boot

spring-boot-starter-aop

com.github.dadiyang

autologging-aop

1.0.0

注解

在 Application 类上添加 @EnableServiceLog 注解开启 Service 层的监控

各个切面有预置的注解,可以根据需要挑选添加:

注解

释义

备注

@EnableMarkLog

启用所有带 @MarkLog 注解的类和方法

在需要监控的类或方法上添加 @MarkLog

@AutoLogAll

开启所有切面的日志监控功能,打印到本地,如果有引入 Kafka 相关配置则进行上报

@AutoLogAllLocal

开启所有切面的日志监控功能,打印到本地

@EnableControllerLog

开启 Controller 日志

@EnableServiceLog

开启 Service 的日志

带有 @Service 的类

@EnableMapperLog

开启 Mapper 层的日志

带有 @Mapper 的类

@RepositoryLogAspect

开启 Repository 的日志

带有 @Repository 的类

配置

autolog:

# 请填写应用名称,必填!

app-name:

OK,搞定!启动之后应用中的各个切面方法执行的时候就会打印相关的日志了。

交个朋友

donation.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值