Search code examples
springexceptionloggingmonitoringsentry

How to add Sentry monitoring to a Spring project


I looked at the Documentation and some Github examples about adding Sentry logging and monitoring to a Spring project.

Does anyone have an example or a link that can help me with that?


Solution

  • I got it to work.

    These are the exact steps you have to follow:

    1. In your pom.xml, add the following dependency for Sentry. (I am using Logback):

      <dependency>
          <groupId>io.sentry</groupId>
          <artifactId>sentry-logback</artifactId>
          <version>1.7.16</version>
      </dependency>
      
    2. For reference (this is just for testing purposes), go in settings, click Client Settings in the SDK setup and get your dsn URL.

    3. After that, make a file in src/resources named sentry.properties and add the following to it:dsn=<YOUR_DSN_URL>.

    4. Make a logback.xml file (if it doesn't already exist) and add the following configuration to it:

    <configuration scan="true" debug="true">
    <property name="app.name" value="MyApp"/>
    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{ISO8601} [%thread] %-5level %logger{40}: %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="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="Sentry"/>
    </root>
    
    </configuration>