If an error occurs in the web application (Spring this time), use Sentry to collect information on the client OS and browser.
OSS that collects error information in various languages such as Java, JavaScript, Ruby, and C #. It is convenient because you can pick up the browser and OS of the operated client. In addition, you can easily browse and check errors on a GUI-based (Slack-like screen). You can also notify Slack and register as an issue in Jira.
https://www.getsentry.com/signup/
Select Java from the screen below and create a project
Select Client Keys in the project settings and copy the following DSN URL
Sentry-logback is a library that sends the contents of log output to Sentry
pom.xml
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-logback</artifactId>
<version>1.2.0</version>
</dependency>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerExceptionResolver;
import io.sentry.Sentry;
@SpringBootApplication
public class SentryApplication {
public static void main(String[] args) {
SpringApplication.run(SentryApplication.class, args);
Sentry.init("https://[email protected]/1202285");
}
@Bean
public HandlerExceptionResolver sentryExceptionResolver() {
return new io.sentry.spring.SentryExceptionResolver();
}
@Bean
public ServletContextInitializer sentryServletContextInitializer() {
return new io.sentry.spring.SentryServletContextInitializer();
}
}
Install the following files under resource
logback.xml
<configuration>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="Sentry" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
</appender>
<root level="DEBUG">
<appender-ref ref="Console" />
<appender-ref ref="Sentry"/>
</root>
</configuration>
The contents at the time of logger.error are sent to Sentry
@RequestMapping("/")
public String list(Model model) {
logger.error("sentry error");
return "Greetings from Spring Boot!";
}
Send an error email to the email address registered by Sentry. Also, errors accumulate in Sentry projects
It is possible to link with Slack from the setting screen of Sentry. Works with many other apps.
Recommended Posts