Sentry not capturing exceptions automatically in a spring boot application

荒凉一梦 提交于 2021-01-28 06:10:32

问题


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

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

回答1:


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>

You will also need to specify sentry dsn property.



来源:https://stackoverflow.com/questions/62797517/sentry-not-capturing-exceptions-automatically-in-a-spring-boot-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!