1、sentry
Sentry一個開源錯誤跟蹤工具,能夠讓開發者實時監控和修復崩潰程序,持續迭代,提高效率。程序代碼中集成Sentry之後,能夠將異常信息發送到Sentry服務,並且可以通過配置Sentry插件,能夠實現通過郵件、釘釘等告警通知。
Sentry官網:https://sentry.io/welcome/
2、log4j/logback on sentry
Sentry中提供log4j的Appender,可以將log中特定等級日誌發送到Sentry中
-
代碼中集成Sentry
使用Maven:
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-logback</artifactId>
<version>1.7.5</version>
</dependency>使用SBT:
libraryDependencies += "io.sentry" % "sentry-logback" % "1.7.5" -
logback.xml的配置
<configuration>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n WARN
3、配置Sentry的DSN(Data Source Name)
有以下集中實現的方式。
- 配置在classpath下的sentry.properties
dsn=https://public:private@host:port/1 - 配置在Java系統屬性中(Java System Properties)
java -Dsentry.dsn=https://public:private@host:port/1 -jar app.jar - 配置在系統環境變量中
SENTRY_DSN=https://public:private@host:port/1 java -jar app.jar - 在代碼中實現
import io.sentry.Sentry;
Sentry.init("https://public:private@host:port/1");
我採用的是第二個方式,配置啓動程序的JVM參數
4、代碼實現
具體工程代碼可以參考 https://github.com/chocolateBlack/loghub-logback-sentry