简体   繁体   English

Sentry 未在 spring 引导应用程序中自动捕获异常

[英]Sentry not capturing exceptions automatically in a spring boot application

I want to track exceptions of my spring boot application using sentry.我想使用哨兵跟踪我的 spring 引导应用程序的异常。 Sentry is not capturing exceptions automatically. Sentry 不会自动捕获异常。 But when I use Sentry.capture(e);但是当我使用Sentry.capture(e); in catch block then Sentry is capturing error.在 catch 块中,Sentry 正在捕获错误。 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 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

I found the problem: MyApplication is implementing CommandLineRunner .我发现了问题: MyApplication正在实现CommandLineRunner Instead of sentry-spring I used sentry-logback alternative and updated logback.xml as following:我使用sentry-logback替代品代替sentry-spring并更新logback.xml ,如下所示:

    <!-- 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>

You will also need to specify sentry dsn property.您还需要指定sentry dsn属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM