SpringBoot整合Sentry监控项目日志

Sentry Java版使用简介

基本概念

Sentry是什么

Sentry 是一个开源的实时错误报告工具,支持 web 前后端、移动应用以及游戏,支持 Python、OC、Java、Go、Node、Django、RoR 等主流编程语言和框架 ,还提供了 GitHub、Slack、Trello 等常见开发工具的集成。

DSN(Data Source Name)

Sentry 服务支持多用户、多团队、多应用管理,每个应用都对应一个 PROJECT_ID,以及用于身份认证的 PUBLIC_KEY 和 SECRET_KEY。由此组成一个这样的 DSN:

‘{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}’

PROTOCOL 通常会是 http 或者 https,HOST 为 Sentry 服务的主机名和端口,PATH 通常为空。
为方便管理,每个应用生成一个 DSN,具体可咨询虎大师 或者 我。

使用 Sentry SDK

Sentry 的 SDK 通常在各语言的包管理器中成为 Raven,使用起来也非常简单。以 Java版本为例(使用SpringBoot框架):

  • 1.首先项目中pom文件安装 Raven

    
    <dependency>
       <groupId>com.getsentry.ravengroupId>
       <artifactId>raven-logbackartifactId>
       <version>8.0.2version>
   dependency>
  • 2.在项目配置文件中建立logback.xml

<configuration>
    
    
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex"
                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx"
                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
    
    <property name="CONSOLE_LOG_PATTERN"
              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(--){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>/>

    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
        encoder>
    appender>


    <appender name="Sentry" class="com.getsentry.raven.logback.SentryAppender">
        
        <dsn>http://d73b23c481654b9ca0e4e8a9db310169:[email protected]/7dsn>
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARNlevel>
        filter>
    appender>

    <logger name="logback.SentryAppenderIT" level="INFO">
        <appender-ref ref="Sentry"/>
    logger>

    <root level="INFO"> 
        <appender-ref ref="Console"/>
        <appender-ref ref="Sentry"/>
    root>
configuration>
  • 3.application.propertes中添加导入日志配置【主要要提前在pom文件中添加包含*.xml,否则会报错,not found file】

logging.config=classpath:logback.xml

  • 4.接口层,正常使用logger等方法。Raven会自动把warn级别通过tcp发送到日志中心,并通知相关人员查看信息
   @RequestMapping(value = "/testlog", method = RequestMethod.GET)
    public void testLog() {
        logger.info("test接口");//最低拦截级别为warn,所以info不会输出发送到日志中心
        logger.error("error"); //会显示在日志中心,并邮件通知相关联系人
    }

这样就可以使用 Raven 对象向 Sentry 服务器中提交日志信息了。

使用 Sentry web 服务

主要是登录后台查看,后续有时间更新此部分文档。开通账号可找虎大师。

附录

sentry 官方文档






<configuration scan="true" scanPeriod="30 seconds">
    
    <contextName>testcontextName>

    
    <property name="Log_Home" value="mylogs/test"/>


    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
    appender>

    
    <appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>DEBUGlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
        <Encoding>UTF-8Encoding>
        <File>${Log_Home}/debug/debug.logFile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ${Log_Home}/debug/debug.%d{yyyy-MM-dd}.%i.log
            FileNamePattern>
            
            <MaxHistory>1MaxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                
                <MaxFileSize>2MBMaxFileSize>
            TimeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>

        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%nPattern>
        encoder>
    appender>

    
    <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>INFOlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
        <Encoding>UTF-8Encoding>
        <File>${Log_Home}/info/info.logFile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ${Log_Home}/info/info.%d{yyyy-MM-dd}.%i.log
            FileNamePattern>
            
            <MaxHistory>1MaxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                
                <MaxFileSize>2MBMaxFileSize>
            TimeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>

        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%nPattern>
        encoder>
    appender>


    
    <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>WARNlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
        <Encoding>UTF-8Encoding>
        <File>${Log_Home}/warn/warn.logFile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ${Log_Home}/warn/warn.%d{yyyy-MM-dd}.%i.log
            FileNamePattern>
            
            <MaxHistory>1MaxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                
                <MaxFileSize>2MBMaxFileSize>
            TimeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>

        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%nPattern>
        encoder>
    appender>

    
    <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>ERRORlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
        <Encoding>UTF-8Encoding>
        <File>${Log_Home}/error/error.logFile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ${Log_Home}/error/error.%d{yyyy-MM-dd}.%i.log
            FileNamePattern>
            
            <MaxHistory>1MaxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                
                <MaxFileSize>2MBMaxFileSize>
            TimeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>

        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%nPattern>
        encoder>
    appender>


    
    <logger name="rattlesnake.callback"/>

    
    
    <root level="DEBUG">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE_DEBUG"/>
        <appender-ref ref="FILE_INFO"/>
        <appender-ref ref="FILE_WARN"/>
        <appender-ref ref="FILE_ERROR"/>
    root>
configuration>

你可能感兴趣的:(SpringBoot整合Sentry监控项目日志)