Sentry不会在Spring Boot应用程序中自动捕获异常 [英] Sentry not capturing exceptions automatically in a spring boot application

查看:105
本文介绍了Sentry不会在Spring Boot应用程序中自动捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用哨兵跟踪我的Spring Boot应用程序的异常.Sentry不会自动捕获异常.但是,当我在catch块中使用 Sentry.capture(e); 时,则使用Sentry正在捕获错误.这是配置和一些代码片段.谢谢您的提前帮助

I want to track exceptions of my spring boot application using sentry. Sentry is not capturing exceptions automatically. But when I use Sentry.capture(e); in catch block then Sentry is capturing error. Here are configurations and some code snippets. Thank you for your help in advance

@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

application.properties

com.zzzz.sentry.dsn=https://akey@sentry.zzzz.com/20?environment=dev&stacktrace.app.packages=com.zzzz.mypackage

推荐答案

我发现了问题: MyApplication 正在实现 CommandLineRunner .我使用 sentry-logback 替代了 sentry-spring ,并更新了 logback.xml 如下:

I found the problem: MyApplication is implementing CommandLineRunner. Instead of sentry-spring I used sentry-logback alternative and updated logback.xml as following:

    <!-- Configure the Sentry appender, overriding the logging threshold to the WARN level -->
    <appender name="Sentry" class="io.sentry.logback.SentryAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
<!-- LOG everything at INFO level -->
    <root level="WARN">
        <appender-ref ref="Console" />
        <appender-ref ref="Sentry" />
    </root>

您还需要指定 sentry dsn 属性.

这篇关于Sentry不会在Spring Boot应用程序中自动捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆