[JAVA] Collecting client information when an error occurs in a web application

This goal

If an error occurs in the web application (Spring this time), use Sentry to collect information on the client OS and browser.

What is Sentry?

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.

Sentry setting procedure

Create an account on Sentry's official page

https://www.getsentry.com/signup/

Sentry team and project creation

Select Java from the screen below and create a project

sentry1.png

Get the project DSN

Select Client Keys in the project settings and copy the following DSN URL

sentry1.png

Web application "Java (Spring)" settings

Package installation on Maven

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>

Describe Sentry settings in Java and add Bean

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();
	}
}

Added Sentry to LogBack

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>

Log output described in Controller class

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!";
	}

result

Send an error email to the email address registered by Sentry. Also, errors accumulate in Sentry projects

sentry1.png

[Supplement]: Sentry and Slack integration

It is possible to link with Slack from the setting screen of Sentry. Works with many other apps.

Slack連携.png

Recommended Posts

Collecting client information when an error occurs in a web application
A reminder when an aapt.exe error occurs
When using a list in Java, java.awt.List comes out and an error occurs
An error occurs when codedeploy-agent is installed in Ubuntu Server 20.04
Let's create a TODO application in Java 12 Processing when a request comes in with an unused HttpMethod ・ Processing when an error occurs in the server
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
What to do and how to install when an error occurs in DXRuby 1.4.7
I get an error when adding a dependency
[Rails] Solution when migration error occurs in acts-as-taggable-on
Error 400 occurs when executing Query by specifying Japanese as a condition in SOQL
What to do when a null byte error occurs
I have a question. I get an error when playing a video in Listview on android.
[Review] When creating a web application with Rails, syntax error, unexpected')', expecting => ...]}% ","% # {params [: content]}% "]) ...
Customize the display when an error such as 404 Not Found occurs in Spring Boot
[Unresolved] An exception occurs when an SSH connection is executed using JSch from a Java 6 application.
Resolve CreateProcess error = 206 when running Java in a Windows environment
When an error occurs even though validation is not set
I tried to implement a buggy web application in Kotlin
A memo of the update command when npm install causes an error in GitHub Action etc.
What to do if an error occurs when doing npm install axios in React + Typescript project
Swift: A trap when setting multiple initial elements in an array
Automatically deploy a Web application developed in Java using Jenkins [Preparation]
After switching between Java 8 and 11 versions, an error occurs in confirmation
Solution notes when an error occurs when downloading docker gpg using curl
What is the difference between a web server and an application server?
I got a Permission Denied error when I put Laravel in Docker
What to do if an error occurs in VS Code when importing a django module or your own module installed by pip install
Simplified version of number calculation by collecting information in an instance
It should be the same as the sample when logging in to Twitter, but an error occurs ~ resolution
Build a web application with Javalin
Implement a gRPC client in Ruby
[Unauthorized Operation] A memorandum because an error occurred when creating an EC2 instance.
Automatically deploy a web application developed in Java using Jenkins [Spring Boot application]
What to do if an error occurs when nokogiri enters when bundle install
I want to display an error message when registering in the database
How to migrate a web application created in a local docker environment to AWS
Make the context root a project folder in an Eclipse dynamic web project
Notes on what to do when a WebView ClassNotFoundException occurs in JavaFX 12
What did I prepare for when I entered an Android application development project while developing the Web in Java?
An error occurred when executing a function from MyBatis with the OUT parameter set to CURSOR in PostgreSQL.