自定义starter处理异常和请求响应日志

GitHub: https://github.com/linweiyuan/logger-spring-boot-starter

准备

git clone https://github.com/linweiyuan/logger-spring-boot-starter.git

./gradlew publishToMavenLocal(其实如果没有特殊的自定义构建脚本需求,感觉 maven 好很多,没有奇奇怪怪的问题)

依赖处理

1
2
3
4
5
<dependency>
<groupId>com.linweiyuan</groupId>
<artifactId>logger-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>

异常日志处理

启动类(或者其他叫法)添加 @EnableExceptionLog 注解

如果使用 @EnableExceptionLog(allowOverride = true),注解上的配置就会失效,以配置文件为准,可以方便的进行修改而无需重新编译

支持如下属性

1
2
3
4
5
6
7
8
9
linweiyuan.logger.exception.border-color=#ffffff
linweiyuan.logger.exception.border-background-color=#ffffff
linweiyuan.logger.exception.border-blink=true
linweiyuan.logger.exception.text-color=#ffffff
linweiyuan.logger.exception.text-background-color=#ffffff
linweiyuan.logger.exception.text-bold=false
linweiyuan.logger.exception.text-italic=false
linweiyuan.logger.exception.text-blink=true
linweiyuan.logger.exception.empty-space-background-color=#ffffff
1
2
3
4
5
6
7
8
9
10
11
12
linweiyuan:
logger:
exception:
border-color: "#ffffff"
border-background-color: "#ffffff"
border-blink: true
text-color: "#ffffff"
text-background-color: "#ffffff"
text-bold: false
text-italic: false
text-blink: true
empty-space-background-color: "#ffffff"

如果使用 @EnableExceptionLog(showAll = true),则会打印全部异常堆栈(默认是 false,也就是只打印注解所在类的包相关,Spring 一堆业务无关的不打印)

注解支持属性

1
2
3
4
5
6
7
8
9
10
11
12
13
@EnableExceptionLog(
showAll = true,
borderColor = "#ffffff",
borderBackgroundColor = "#ffffff",
borderBlink = true,
textColor = "#ffffff",
textBackgroundColor = "#ffffff",
textBold = true,
textItalic = true,
textBlink = true,
emptySpaceBackgroundColor = "#ffffff",
allowOverride = true
)

请求响应日志

这个不支持配置文件配置,只能在注解上进行

注解支持属性 q

1
2
3
4
5
6
7
8
9
10
11
12
@EnableExceptionLog(
borderColor = "#ffffff",
borderBackgroundColor = "#ffffff",
borderBlink = true,
textColor = "#ffffff",
textBackgroundColor = "#ffffff",
textBold = true,
textItalic = true,
textBlink = true,
emptySpaceBackgroundColor = "#ffffff",
useDefaultColor = true
)

如果使用 @EnableExceptionLog(useDefaultColor = true),则以下默认配置会生效

响应码颜色
2xx绿
4xx
5xx

但是只有 API 返回的是 ResponseEntity 才会生效,试过用 HttpServletResponse,获取的响应码全是 200,可能还要再看看