项⽬集成sentry
本⽂章将介绍基于Sentry官⽅提供的在线服务集成sentry,本地搭建sentry将在下篇⽂章中介绍。Sentry是个什么东西,⾃⾏百度了解。
1、注册,登录
⽹址:
注册⼀个账号
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
# 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
如果是l,则
<?xml version="1.0" encoding="UTF-8"?>
<configuration package="org.apache.,io.sentry.log4j2">
<properties>
<property name="PATTERN">[%-5p]%d{yyyy-MM-dd HH:mm:ss,SSS} %l%n%m%nlog4j2 appender
</property>
</properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout charset="UTF-8" pattern="${PATTERN}" />
</Console>
<RollingFile name="crawler_logger_appender"
fileName="logs/crawler.log"
filePattern="logs/crawler-%d{yyyyMMdd-HH}.log">
<PatternLayout charset="UTF-8">
<Pattern>"${PATTERN}"</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
<Sentry name="Sentry" />
</appenders>
<loggers>
<logger name="" level="ERROR">
<AppenderRef ref="Console" />
</logger>
<root level="ERROR">
<AppenderRef ref="Console" />
<AppenderRef ref="crawler_logger_appender"></AppenderRef>
</root>
<AsyncLogger name="sentrylogger" additivity="false"
level="ERROR" includeLocation="true">
<AppenderRef ref="Sentry" />
</AsyncLogger>
<AsyncLogger name="crawlerlogger" additivity="false"
level="ERROR" includeLocation="true">
<AppenderRef ref="crawler_logger_appender" />
</AsyncLogger>
</loggers>
</configuration>
如果指定sentrylogger形式的⽇志才需要发送到sentry,其他⽇志不需要发送时,则按照下⾯的格式配置:log4j.logger.sentrylogger=INFO,Sentry
## Enable the Console and Sentry appenders
#Logger=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=public:private@host:port/1
通过Java系统属性(在Android上不可⽤):
-Dsentry.dsn=public:private@host:port/1 -jar app.jar
通过系统环境变量(在Android上不可⽤):
SENTRY_DSN=public:private@host:port/1 java -jar app.jar
代码中:
import io.sentry.Sentry;
Sentry.init("public:private@host:port/1");
7、demo代码:
package ample.basic;
import io.sentry.Sentry;
import io.sentry.SentryClient;
import io.sentry.SentryClientFactory;
import t.Context;
import io.sentry.event.BreadcrumbBuilder;
import io.sentry.event.UserBuilder;
public class MyClass {
private static SentryClient sentry;
public static void 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("8219291bc5224fa8ab7188ffbf4aa025:6ce60ab509d046cfbbada4862cbd2154@sentry.io/1361356");
// You can also manually provide the DSN to the ``init`` method.
// String dsn = "<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.
new BreadcrumbBuilder().setMessage("User made an action").build()
);
// Set the user in the current context.
new UserBuilder().setEmail("1416459094@qq").build()
);
// Add extra data to future events in this context.
// Add an additional tag to future events in this context.
/
*
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);
}
}
}
⾃测成功!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论