一、系统环境
组件 | 版本 |
---|---|
Ubuntu | 20.04 |
SpringBoot | 2.5.8 |
二、Sentry联动
2.1 创建一个新项目
2.2 获取DNS链接
2.3 按照官网教程配置Springboot
三、代码示例
3.1 logback
3.1.1 pom.xml
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring-boot-starter</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-logback</artifactId>
<version>5.5.2</version>
</dependency>
3.2 log4j2
3.2.1 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring-boot-starter</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-log4j2</artifactId>
<version>5.5.2</version>
</dependency>
3.2.2 log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Sentry name="Sentry"/>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Sentry"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
3.3 application.yml
server:
port: 8001
sentry:
dsn: http://项目的dsn
logging:
# 触发event的日志等级,即在Sentry界面上显示问题的最小等级
minimum-event-level: error
# event日志的上下文等级,在Sentry问题页面上下文中的最小等级
minimum-breadcrumb-level: info
3.4 Controller
package com.example.demosentry.controller;
import io.sentry.Sentry;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("api")
@Api(tags = "Sentry测试")
public class DemoController {
@ApiOperation("demo1")
@GetMapping("demo1")
public String demo1 () throws Exception{
//被动发送异常
int a = 0/0;
return "demo1";
}
@ApiOperation("demo2")
@GetMapping("demo2")
public String demo2 (){
//捕获的异常不会发送Sentry
try{
throw new Exception("demo2 Exception");
}catch (Exception e){
}
return "demo2";
}
@ApiOperation("demo3")
@GetMapping("demo3")
public String demo3 (){
//主动推送捕获的异常,会发送Sentry
try{
throw new Exception("demo3 Exception");
}catch (Exception e){
Sentry.captureException(e);
}
return "demo3";
}
@ApiOperation("demo4")
@GetMapping("demo4")
public String demo4 (){
//日志等级小于application.yml配置中 minimum-event-level: error等级的不会发送日志
log.info("demo4 log info");
return "demo4";
}
@ApiOperation("demo5")
@GetMapping("demo5")
public String demo5 (){
//日志等级小于application.yml配置中 minimum-breadcrumb-level: info等级的不会有上下文
log.debug("demo5 log debug");
log.info("demo5 log info");
log.warn("demo5 log warn");
log.error("demo5 log error");
return "demo5";
}
}
3.5 结果查看-demo5接口
3.5.1 触发event的日志等级,即在Sentry界面上显示问题的最小等级
因为配置是minimum-event-level: error,所以没有debug、info、warn的日志
3.5.2 触发问题后,在Sentry问题页面上下文中的最小等级
因为配置是minimum-breadcrumb-level: info,所以上下文中没有debug日志
四、补充说明
页面上没有列出所有文档,更多官方文档需要检索得到,如:springboot