Skip to content

Latest commit

 

History

History
 
 

raven-logback

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Raven-logback

logback support for Raven. It provides an Appender for logback to send the logged events to Sentry.

Installation

Maven

<dependency>
    <groupId>net.kencochrane.raven</groupId>
    <artifactId>raven-logback</artifactId>
    <version>5.0</version>
</dependency>

Other dependency managers

Details in the central Maven repository.

Manual dependency management

Relies on:

Usage

Configuration

In the logback.xml file set:

<configuration>
    <appender name="Sentry" class="net.kencochrane.raven.logback.SentryAppender">
        <dsn>https://publicKey:secretKey@host:port/1?options</dsn>
        <tags>tag1:value1,tag2:value2</tags>
        <!-- Optional, allows to select the ravenFactory -->
        <!--<ravenFactory>net.kencochrane.raven.DefaultRavenFactory</ravenFactory>-->
    </appender>
    <root level="warn">
        <appender-ref ref="Sentry"/>
    </root>
</configuration>

Additional data and information

It's possible to add extra details to events captured by the logback module thanks to the marker system which will add a tag logback-Marker. The MDC system provided by logback allows to add extra information to the event.

Mapped Tags

By default all MDC parameters are sent under the Additional Data Tab. By specify the mappedTags parameter in your configuration file. You can specify MDC keys to send as tags instead of including them in Additional Data. This allows them to be filtered within Sentry.

<mappedTags>User,OS</mappedTags>
    void logWithExtras() {
        // MDC extras
        MDC.put("User", "test user");
        MDC.put("OS", "Linux");

        // This adds a message with extras and MDC keys declared in mappedTags as tags to Sentry
        logger.info("This is a test");
    }

In practice

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.MarkerFactory;

public class MyClass {
    private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
    private static final Marker MARKER = MarkerFactory.getMarker("myMarker");

    void logSimpleMessage() {
        // This adds a simple message to the logs
        logger.info("This is a test");
    }

    void logWithTag() {
        // This adds a message with a tag to the logs named 'logback-Marker'
        logger.info(MARKER, "This is a test");
    }

    void logWithExtras() {
        // MDC extras
        MDC.put("extra_key", "extra_value");
        // This adds a message with extras to the logs
        logger.info("This is a test");
    }

    void logException() {
        try {
            unsafeMethod();
        } catch (Exception e) {
            // This adds an exception to the logs
            logger.error("Exception caught", e);
        }
    }

    void unsafeMethod() {
        throw new UnsupportedOperationException("You shouldn't call that");
    }
}