【问题标题】:Sentry not capturing exceptions automatically in a spring boot applicationSentry 未在 Spring Boot 应用程序中自动捕获异常
【发布时间】: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


【解决方案1】:

我发现了问题:MyApplication 正在实现CommandLineRunner。而不是sentry-spring,我使用sentry-logback替代并更新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>

您还需要指定sentry dsn 属性。

【讨论】:

    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 2016-10-17
    • 2023-03-30
    • 2018-05-30
    • 1970-01-01
    • 2021-09-20
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多