kok202
센트리 자바 샘플 코드

2019. 3. 27. 18:49[정리] 기능별 개념 정리/Sentry

raven : sentry 샘플 프로젝트

raven-java

raven-python 이런 식으로 나뉜다.

그런데 이건 이제 만료된 샘플 프로젝트이다.

현재 sentry-java 로 바뀌었다.

 

 

 

Sentry 8 

1. pom.xml 에 sentry dependency 추가

<dependencies> 
	<dependency> 
		<groupId>io.sentry</groupId> 
		<artifactId>sentry</artifactId> 
		<version>1.6.7</version> 
	</dependency> 
</dependencies>

2. maven - reimport

3. 

 

package sentry;
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){
        System.out.println("Place your dsn in init() Run this class");
        Sentry.init("센트리 클라이언드 DSN");

        MyClass myClassSentry8 = new MyClass();
        myClassSentry8.createArithmaticError();
        //myClassSentry8.logWithStaticAPI();
        //myClassSentry8.logWithInstanceAPI();
    }

    void unsafeMethod() {
        throw new UnsupportedOperationException("You shouldn't call this!");
    }

    void createArithmaticError(){
        try{
            int x = 1/0;
        }catch(Exception e){
            Sentry.capture(e);
        }
    }

    void logWithStaticAPI() {
        Sentry.getContext().recordBreadcrumb( new BreadcrumbBuilder().setMessage("User made an action").build() );
        Sentry.getContext().setUser( new UserBuilder().setEmail("hello@sentry.io").build() );
        Sentry.getContext().addExtra("extra", "thing");
        Sentry.getContext().addTag("tagName", "tagValue");
        Sentry.capture("This is a test 1");

        try {
            unsafeMethod();
        } catch (Exception e) {
            Sentry.capture(e);
        }
    }

    void logWithInstanceAPI() {
        sentry = SentryClientFactory.sentryClient();
        Context context = sentry.getContext();
        context.recordBreadcrumb(new BreadcrumbBuilder().setMessage("User made an action").build());
        context.setUser(new UserBuilder().setEmail("hello@sentry.io").build());
        sentry.sendMessage("This is a test 2");

        try {
            unsafeMethod();
        } catch (Exception e) {
            sentry.sendException(e);
        }
    }
}

 

 

 

 

 


Sentry 9

※ io.sentry의 버전이 1.6.7 일 경우 Sentry 9.0.0 페이지로 전송시 에러남 

1. pom.xml 에 sentry dependency 추가

<dependencies> 
	<dependency> 
		<groupId>io.sentry</groupId> 
		<artifactId>sentry</artifactId> 
		<version>1.7.16</version> 
	</dependency> 
</dependencies>

2. maven - reimport

3. 샘플 코드는 위 Sentry 8 코드와 동일

 

package sentry;
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){
        System.out.println("Place your dsn in init() Run this class");
        Sentry.init("센트리 클라이언드 DSN");

        MyClass myClassSentry8 = new MyClass();
        myClassSentry8.createArithmaticError();
        //myClassSentry8.logWithStaticAPI();
        //myClassSentry8.logWithInstanceAPI();
    }

    void unsafeMethod() {
        throw new UnsupportedOperationException("You shouldn't call this!");
    }

    void createArithmaticError(){
        try{
            int x = 1/0;
        }catch(Exception e){
            Sentry.capture(e);
        }
    }

    void logWithStaticAPI() {
        Sentry.getContext().recordBreadcrumb( new BreadcrumbBuilder().setMessage("User made an action").build() );
        Sentry.getContext().setUser( new UserBuilder().setEmail("hello@sentry.io").build() );
        Sentry.getContext().addExtra("extra", "thing");
        Sentry.getContext().addTag("tagName", "tagValue");
        Sentry.capture("This is a test 1");

        try {
            unsafeMethod();
        } catch (Exception e) {
            Sentry.capture(e);
        }
    }

    void logWithInstanceAPI() {
        sentry = SentryClientFactory.sentryClient();
        Context context = sentry.getContext();
        context.recordBreadcrumb(new BreadcrumbBuilder().setMessage("User made an action").build());
        context.setUser(new UserBuilder().setEmail("hello@sentry.io").build());
        sentry.sendMessage("This is a test 2");

        try {
            unsafeMethod();
        } catch (Exception e) {
            sentry.sendException(e);
        }
    }
}