项目集成sentry

本文章将介绍基于Sentry官方提供的在线服务集成sentry,本地搭建sentry将在下篇文章中介绍。

Sentry是个什么东西,自行百度了解。

1、注册,登录

网址:https://sentry.io/
注册一个账号

2、创建一个project

3、获取project的DSN

4、maven配置依赖

<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-log4j</artifactId>
    <version>1.7.10</version>
</dependency>

普通java项目需要引入的jar包包括:sentry-1.7.16.jar、sentry-log4j-1.7.17-SNAPSHOT.jar、jackson-annotations-2.1.0.jar、jackson-core-2.1.2.jar、jackson-databind-2.1.0.jar等,根据需要引入。

5、log4j集成sentry时需要在log4j中增加下列配置:

# Enable the Console and Sentry appenders
log4j.rootLogger=INFO, Console, Sentry

# Configure the Console appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5p: %m%n

# Configure the Sentry appender, overriding the logging threshold to the WARN level
log4j.appender.Sentry=io.sentry.log4j.SentryAppender
log4j.appender.Sentry.threshold=WARN

如果是log4j.xml,则

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
    xmlns:log4j=‘http://jakarta.apache.org/log4j/‘>

    <!-- Configure the Console appender -->
    <appender name="Console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <!-- Configure the Sentry appender, overriding the logging threshold to the WARN level -->
    <appender name="Sentry" class="io.sentry.log4j.SentryAppender">
        <!-- Override the Sentry handler log level to WARN -->
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="levelMin" value="WARN" />
        </filter>
    </appender>

    <!-- Enable the Console and Sentry appenders, Console is provided as an example
 of a non-Sentry logger that is set to a different logging threshold -->
    <root level="INFO">
        <appender-ref ref="Console" />
        <appender-ref ref="Sentry" />
    </root>
</log4j:configuration>

如果指定sentrylogger形式的日志才需要发送到sentry,其他日志不需要发送时,则按照下面的格式配置:

log4j.logger.sentrylogger=INFO,Sentry

## Enable the Console and Sentry appenders
#log4j.rootLogger=INFO, Console, Sentry
#
## Configure the Console appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5p: %m%n
##
## Configure the Sentry appender, overriding the logging threshold to the INFO level log4j.appender.Sentry=io.sentry.log4j.SentryAppender log4j.appender.Sentry.threshold=INFO

6、设置DSN

在文件系统或类路径上的属性文件中(默认为sentry.properties):

dsn=https://public:[email protected]:port/1

通过Java系统属性(在Android上不可用):

-Dsentry.dsn=https://public:[email protected]:port/1 -jar app.jar

通过系统环境变量(在Android上不可用):

SENTRY_DSN=https://public:[email protected]:port/1 java -jar app.jar

代码中:

import io.sentry.Sentry;

Sentry.init("https://public:[email protected]:port/1");

7、demo代码:

package io.sentry.example.basic;

import io.sentry.Sentry;
import io.sentry.SentryClient;
import io.sentry.SentryClientFactory;
import io.sentry.context.Context;
import io.sentry.event.BreadcrumbBuilder;
import io.sentry.event.UserBuilder;

public class MyClass {
    private static SentryClient sentry;

    public static void main(String... args) {
        /*
        It is recommended that you use the DSN detection system, which
        will check the environment variable "SENTRY_DSN", the Java
        System Property "sentry.dsn", or the "sentry.properties" file
        in your classpath. This makes it easier to provide and adjust
        your DSN without needing to change your code. See the configuration
        page for more information.
        */
        Sentry.init("https://8219291bc5224fa8ab7188ffbf4aa025:[email protected]/1361356");

        // You can also manually provide the DSN to the ``init`` method.
        // String dsn = "https://<SENTRY_PUBLIC_KEY>:<SENTRY_PRIVATE_KEY>@sentry.io/<PROJECT_ID>";
        // Sentry.init(dsn);

        /*
        It is possible to go around the static ``Sentry`` API, which means
        you are responsible for making the SentryClient instance available
        to your code.
        */
        sentry = SentryClientFactory.sentryClient();

        MyClass myClass = new MyClass();
        myClass.logWithStaticAPI();
    }

    /**
     * An example method that throws an exception.
     */
    void unsafeMethod() {
        throw new UnsupportedOperationException("You shouldn‘t call this!");
    }

    /**
     * Examples using the (recommended) static API.
     */
    void logWithStaticAPI() {
        // Note that all fields set on the context are optional. Context data is copied onto
        // all future events in the current context (until the context is cleared).

        // Record a breadcrumb in the current context. By default the last 100 breadcrumbs are kept.
        Sentry.getContext().recordBreadcrumb(
                new BreadcrumbBuilder().setMessage("User made an action").build()
        );

        // Set the user in the current context.
        Sentry.getContext().setUser(
                new UserBuilder().setEmail("[email protected]").build()
        );

        // Add extra data to future events in this context.
        Sentry.getContext().addExtra("extra", "thing");

        // Add an additional tag to future events in this context.
        Sentry.getContext().addTag("tagName", "tagValue");

        /*
        This sends a simple event to Sentry using the statically stored instance
        that was created in the ``main`` method.
        */
        Sentry.capture("This is a test by weichangjin");

        try {
            unsafeMethod();
        } catch (Exception e) {
            // This sends an exception event to Sentry using the statically stored instance
            // that was created in the ``main`` method.
            Sentry.capture(e);
        }
    }

}

自测成功!

原文地址:https://www.cnblogs.com/promonkeys/p/10209642.html

时间: 2024-08-30 18:03:53

项目集成sentry的相关文章

flask项目集成Sentry收集线上错误日志

flask集成sentry分为4个步骤: 首先在sentry官网注册1个账号 然后创建1个新的项目,这里我选择的是flask,这会得到一些关于sdk的使用说明 接下来创建一个简单的flask项目使用sdk测试一下 实时在sentry的dashboard的project页面上看到提交过来的异常信息 Sentry官网地址 在flask项目中配置使用sentry,步骤如下: step1: 通过pip 安装sentry-sdk pip3 install --upgrade 'sentry-sdk[fla

【总结】HUE集成Sentry,通过HUE界面管理Sentry权限

将Sentry集成到HUE中能够有许多好处: 1.通过HUE界面,创建role 2.在HUE的Sentry管理界面,可以Grant privileges给用户 3.在Hue中创建与Sentry中role同名的group 4.将role赋予一个user 5.保证HUE用户,在所有节点上的group与HUE中的group相同 HUE集成Sentry操作 1.将登录HUE管理用户和权限的用户所属组添加到sentry-site.xml配置文件中 <property>   <name>sen

Spring学习(一)tomcat加载web.xml、以及项目集成Spring支持

tomcat容器加载web.xml 一. 1 .启动一个 WEB 项目的时候, WEB 容器会去读取它的配置文件 web.xml ,读取 <listener> 和 <context-param> 两个结点. 2 .紧急着,容创建一个 ServletContext ( servlet 上下文),这个 web 项目的所有部分都将共享这个上下文. 3 .容器将 <context-param> 转换为键值对,并交给 servletContext . 4 .容器创建 <li

SpringMVC将一个项目集成在另一个项目中

将KissfloveUtil项目集成在自己项目中.在pom.xml中加入以下配置: <dependencies> <dependency> <groupId>com.kissflove</groupId> <artifactId>kissfloveUtil</artifactId> <version>0.0.1</version> </dependency> </dependencies>

Vue.js项目集成ElementUI

Vuejs实例-02Vue.js项目集成ElementUI Vuejs实例-02Vue.js项目集成ElementUI 0:前言 vue.js的UI组件库,在git上有多个项目,我见的使用者比较多的是iView和Element.两个组件库,组件都很丰富. 官网的介绍 iView: 一套基于 Vue.js 的高质量 UI 组件库 Element,一套为开发者.设计师和产品经理准备的基于 Vue 2.0 的组件库,提供了配套设计资源,帮助你的网站快速成型. 两者各有优缺点,不多评论,根据自己的需求,

项目集成koala业务日志子系统

Koala平台的业务日志子系统是基于Maven的项目模块,最方便的集成是项目也使用Maven,war项目集成请自行下载jar包 前提 依赖spring 添加仓库 <repositories> <repository> <id>koala-releases</id> <url>http://nexus.openkoala.org/content/repositories/public-releases/</url> <releas

Appium&#183;项目集成

date:2018610 day14 一.项目集成 1.项目分层(以搜索下单为例) ①.测试数据 ②.关键字 ③.测试用例 将关键字组合起来,组合成一个搜索-消费测试用例 ④.用例模板 2.Tips ①.想要关键字.变量能被调用,要在资源下写关键字.变量 ②.在测试用例模板中,tearDown中要加Close All Applications ③.所有从网页上得到的内容,都是要Unicode形式获取,要将Unicode形式装换成Int型,直接  int(${a})即可 ④.RIDE不能直接计算,

MyBatis&amp;&amp;ssm项目集成

MyBatis&&ssm项目集成 一.mapper映射    1.准备接口 EmployeeMapper    2.准备xml EmployeeMapper.xml    3.测试的完成二.高级查询三.关系配置(重难点)    1.多对一        1.1 domain准备        1.2 关系映射 - 嵌套结果        1.3 关系映射 - 嵌套查询    2.一对多的配置        2.1 相应的domain准备        2.2 嵌套结果        2.3

springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】

项目结构: 1.maven项目的pom中引入shiro所需的jar包依赖关系 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <dependency>     <groupid>javax.servlet</groupid>     javax.servlet-api</artifactid>     <version>3.0.1</version>