【发布时间】:2020-10-29 00:35:45
【问题描述】:
我想使用 sentry 跟踪我的 Spring Boot 应用程序的异常。 Sentry 不会自动捕获异常。但是当我在 catch 块中使用Sentry.capture(e); 时,Sentry
正在捕获错误。这是配置和一些代码sn-ps。提前感谢您的帮助
@SpringBootApplication
@EnableAutoConfiguration
public class MyApplication implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args).close();
}
@Override
public void run(String... args) throws IOException {
try {
int example = 1 / 0;
} catch (Exception e) {
//Sentry.capture(e);
logger.error("Caught exception erwqerer!", e);
}
}
}
@Configuration
public class SentryConfig {
@Bean
public HandlerExceptionResolver sentryExceptionResolver() {
return new io.sentry.spring.SentryExceptionResolver();
}
@Bean
public ServletContextInitializer sentryServletContextInitializer() {
return new io.sentry.spring.SentryServletContextInitializer();
}
@Value("${com.zzzz.sentry.dsn:#{null}}")
private String sentryDsn;
@PostConstruct
private void initializeSentry() {
if (sentryDsn != null) {
Sentry.init(sentryDsn);
}
}
}
POM.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
...
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring</artifactId>
<version>1.7.5</version>
</dependency>
application.properties
com.zzzz.sentry.dsn=https://akey@sentry.zzzz.com/20?environment=dev&stacktrace.app.packages=com.zzzz.mypackage
【问题讨论】:
-
那么需要手动做
Sentry.init()吗?出于某些原因,我认为哨兵本身会为我做这件事......
标签: java spring spring-boot logging sentry