spring boot 添加sentry
官方spring 方案
1
https://docs.sentry.io/clients/java/modules/spring/
配置如下:
pom.xml
1
2
3
4
5<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring</artifactId>
<version>1.6.3</version>
</dependency>1
2
3
4
5
6
7
8
public HandlerExceptionResolver sentryExceptionResolver() {
return new io.sentry.spring.SentryExceptionResolver();
}
public ServletContextInitializer sentryServletContextInitializer() {
return new io.sentry.spring.SentryServletContextInitializer();
}1
dsn=http://cbbc57cc8f7975750:2d9@sentry.io/24
raven-logback 亲测可用
pom.xml
1
2
3
4
5<dependency>
<groupId>com.getsentry.raven</groupId>
<artifactId>raven-logback</artifactId>
<version>8.0.3</version>
</dependency>1
2
3
4
5
6
7
8
9
10
11
12
13
14
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="SENTRY" class="com.getsentry.raven.logback.SentryAppender">
<dsn>http://cbbc57cc8f7975750:41a8850708e5dc1159@sentry.io/24</dsn>
<tags>tag1:cpa,tag2:admin</tags>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender></p><p> <root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="SENTRY"/>
</root>
</configuration>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32import com.getsentry.raven.DefaultRavenFactory;
import com.getsentry.raven.Raven;
import com.getsentry.raven.dsn.Dsn;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;</p><p>import javax.servlet.http.HttpServletRequest;</p><p>
public class GlobalExceptionHandler {
String dsnUrl;</p><p>
public Result defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
sendSentry(e);
// 统计结果类
Result result = new Result();
return result.setStatus("error").setMessage(e.getMessage());
}
/**
* 异常发送至sentry
* @param e
*/
private void sendSentry(Exception e) {
Dsn dsn = new Dsn(dsnUrl);
Raven raven = (new DefaultRavenFactory()).createRavenInstance(dsn);
Throwable throwable = new Throwable(e.getMessage(), e.getCause());
throwable.setStackTrace(e.getStackTrace());
raven.sendException(throwable);
}
}
应用
1
2
3
4
5
6
7import lombok.extern.log4j.Log4j;
public class Test{
public void test(){
log.error("pp");
}
}