All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.monitor.sentry.SentryExceptionHandler Maven / Gradle / Ivy

package org.zodiac.monitor.sentry;

import io.sentry.Sentry;
import io.sentry.event.Event.Level;
import io.sentry.event.EventBuilder;
import io.sentry.event.interfaces.ExceptionInterface;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

/**
 * 统一Sentry异常处理。
 */
public class SentryExceptionHandler {

    private static Logger log = LoggerFactory.getLogger(SentryExceptionHandler.class);

    private static Environment env;
    private static boolean enabled=false;

    public static boolean isEnabled() {
        return enabled;
    }

    public static void setEnabled(boolean enabled) {
        SentryExceptionHandler.enabled = enabled;
    }

    public static void setEnv(Environment env) {
        SentryExceptionHandler.env = env;
    }

    private static boolean record(Object o) {
        if(!isEnabled()){
            return false;
        }
        if (env == null) {
            return false;
        }
        if (o == null) {
            return false;
        }
        return true;
    }

    private static boolean checkThrowable(Throwable t) {
        if (t == null) {
            return false;
        }
        return true;
    }

    public static void onException(Thread thread, Throwable t) {
        if (checkThrowable(t)) {
            sendEventBuilder(
                new EventBuilder().withMessage(thread.toString() + " " + t.getMessage()).withLevel(Level.ERROR)
                    .withSentryInterface(new ExceptionInterface(t)));

        } else {
            log.debug("ignored exception", t);
        }
    }

    public static void onException(String logger, Throwable t) {
        if (checkThrowable(t)) {

            sendEventBuilder(new EventBuilder().withMessage(t.getMessage()).withLevel(Level.ERROR).withLogger(logger)
                .withSentryInterface(new ExceptionInterface(t)));

        } else {
            log.debug("ignored exception", t);
        }
    }

    public static void sendErrorMessage(String message) {
        sendEventBuilder(new EventBuilder().withMessage(message).withLevel(Level.ERROR));
    }

    public static void sendWarningMessage(String message) {
        sendEventBuilder(new EventBuilder().withMessage(message).withLevel(Level.WARNING));
    }

    public static void sendInfoMessage(String message) {
        sendEventBuilder(new EventBuilder().withMessage(message).withLevel(Level.INFO));
    }

    public static void sendEventBuilder(EventBuilder eventBuilder) {
        if (record(eventBuilder)) {
            // TODO 自动获取关注此应用的邮件列表
            eventBuilder.withRelease(release);
            eventBuilder.withEnvironment(environment);
            Sentry.capture(eventBuilder);
            return;
        }
        log.debug("ignored event {}", eventBuilder);
    }

    private static String release="none@none";
    private static String environment="none";

    public static String getRelease() {
        return release;
    }

    public static void setRelease(String release) {
        SentryExceptionHandler.release = release;
    }

    public static String getEnvironment() {
        return environment;
    }

    public static void setEnvironment(String environment) {
        SentryExceptionHandler.environment = environment;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy