SpringBoot整合sentry

首先,springboot配置sentry有两种方式,方式一支持spring2.x版本,方式二支持spring4.x版本

方式一:

首先在linux使用docker部署安装sentry,下面是springboot配置:

一。引入pom.xml文件

<dependency>
   <groupId>io.sentry</groupId>
   <artifactId>sentry-spring</artifactId>
   <version>1.7.6</version>
</dependency>
   <dependency>
   <groupId>io.sentry</groupId>
   <artifactId>sentry-logback</artifactId>
   <version>1.7.30</version>
</dependency>

二。新建sentry.properties文件,内容如下:

SpringBoot整合sentry

三。在logback-spring.xml加入下面代码:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <!--Console 日志文件-->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d - %msg%n
            </pattern>
        </layout>
    </appender>
    <!--Info 文件日志-->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <!--<fileNamePattern>/var/log/tomcat/sell/info.%d.log</fileNamePattern>-->
            <fileNamePattern>log/info.%d.log</fileNamePattern>
          <!--  <fileNamePattern>D://tomcat/info.%d.log</fileNamePattern>-->
        </rollingPolicy>
    </appender>
    <!--Error 文件日志-->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>log/error.%d.log</fileNamePattern>
            <!--<fileNamePattern>D://tomcat/error.%d.log</fileNamePattern>-->
        </rollingPolicy>
    </appender>

    <appender name="SentryAppender" class="io.sentry.logback.SentryAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
    </appender>
    <!-- console only if local active -->
    <springProfile name="local">
        <root level="INFO">
            <appender-ref ref="ConsoleAppender"/>
        </root>
    </springProfile>
    <!-- file,sentry only if test or online active -->
    <springProfile name="test,online">
        <root level="INFO">
            <appender-ref ref="ServerFileAppender"/>
            <appender-ref ref="SentryAppender"/>
        </root>
    </springProfile>

    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration>

在配置文件下建Config配置:


import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;

@Configuration
public class SentryConfig {
    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new io.sentry.spring.SentryExceptionResolver();
    }

    /*
     * SentryServletContextInitializer installs the SentryServletRequestListener so that
     * Sentry events contain HTTP request information.
     */
    @Bean
    public ServletContextInitializer sentryServletContextInitializer() {
        return new io.sentry.spring.SentryServletContextInitializer();
    }
}

OK,刷新maven,启动项目即可;

方式二:

一:在pom.xml文件添加依赖

<dependency>
      <groupId>io.sentry</groupId>
      <artifactId>sentry-spring-boot-starter</artifactId>
      <version>3.2.0</version>
    </dependency>
    <dependency>
      <groupId>io.sentry</groupId>
      <artifactId>sentry-logback</artifactId>
      <version>3.2.0</version>
    </dependency>

二:在application.yml添加配置即可

SpringBoot整合sentry

 

上一篇:.netcore word/excel转换pdf


下一篇:多人开发代码格式化配置, 使用eslint+prettier