Main module of the Raven project in java. It provides a client to send messages to a Sentry server as well as an implementation of an Handler for java.util.logging.
<dependency> <groupId>net.kencochrane.raven</groupId> <artifactId>raven</artifactId> <version>6.0.0</version> </dependency>Details in the central Maven repository.
Relies on:
- guava-18.0.jar
- jackson-core-2.5.0.jar
- slf4j-api-1.7.9.jar
- slf4j-jdk14-1.7.9.jar is recommended as the implementation of slf4j to capture the log events generated by Raven (connection errors, ...) if
java.util.loggingis used.
In the logging.properties file set:
.level=WARN handlers=net.kencochrane.raven.jul.SentryHandler net.kencochrane.raven.jul.SentryHandler.dsn=https://publicKey:secretKey@host:port/1?options net.kencochrane.raven.jul.SentryHandler.tags=tag1:value1,tag2:value2 # Optional, allows to select the ravenFactory #net.kencochrane.raven.jul.SentryHandler.ravenFactory=net.kencochrane.raven.DefaultRavenFactoryWhen starting your application, add the java.util.logging.config.file to the system properties, with the full path to the logging.properties as its value.
$ java -Djava.util.logging.config.file=/path/to/app.properties MyClass import java.util.logging.Level; import java.util.logging.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class.getName()); void logSimpleMessage() { // This adds a simple message to the logs logger.log(Level.INFO, "This is a test"); } void logException() { try { unsafeMethod(); } catch (Exception e) { // This adds an exception to the logs logger.log(Level.SEVERE, "Exception caught", e); } } void unsafeMethod() { throw new UnsupportedOperationException("You shouldn't call that"); } }java.util.logging does not support either MDC nor NDC, meaning that it is not possible to attach additional/custom context values to the logs. In other terms, it is not possible to use the "extra" field supported by Sentry.
It is possible to use the client manually rather than using a logging framework in order to send messages to Sentry. It is not recommended to use this solution as the API is more verbose and requires the developer to specify the value of each field sent to Sentry.
import net.kencochrane.raven.Raven; import net.kencochrane.raven.RavenFactory; public class MyClass { private static Raven raven; public static void main(String... args) { // Creation of the client with a specific DSN String dsn = args[0]; raven = RavenFactory.ravenInstance(dsn); // It is also possible to use the DSN detection system like this raven = RavenFactory.ravenInstance(); } void logSimpleMessage() { // This adds a simple message to the logs raven.sendMessage("This is a test"); } void logException() { try { unsafeMethod(); } catch (Exception e) { // This adds an exception to the logs raven.sendException(e); } } void unsafeMethod() { throw new UnsupportedOperationException("You shouldn't call that"); } }For more complex messages, it will be necessary to build an Event with the EventBuilder class.
import net.kencochrane.raven.Raven; import net.kencochrane.raven.RavenFactory; import net.kencochrane.raven.event.Event; import net.kencochrane.raven.event.EventBuilder; import net.kencochrane.raven.event.interfaces.ExceptionInterface; import net.kencochrane.raven.event.interfaces.MessageInterface; public class MyClass { private static Raven raven; public static void main(String... args) { // Creation of the client with a specific DSN String dsn = args[0]; raven = RavenFactory.ravenInstance(dsn); // It is also possible to use the DSN detection system like this raven = RavenFactory.ravenInstance(); // Advanced: To specify the ravenFactory used raven = RavenFactory.ravenInstance(new Dsn(dsn), "net.kencochrane.raven.DefaultRavenFactory"); } void logSimpleMessage() { // This adds a simple message to the logs EventBuilder eventBuilder = new EventBuilder() .withMessage("This is a test") .withLevel(Event.Level.INFO) .withLogger(MyClass.class.getName()); raven.runBuilderHelpers(eventBuilder); // Optional raven.sendEvent(eventBuilder.build()); } void logException() { try { unsafeMethod(); } catch (Exception e) { // This adds an exception to the logs EventBuilder eventBuilder = new EventBuilder() .withMessage("Exception caught") .withLevel(Event.Level.ERROR) .withLogger(MyClass.class.getName()) .withSentryInterface(new ExceptionInterface(e)); raven.runBuilderHelpers(eventBuilder); // Optional raven.sendEvent(eventBuilder.build()); } } void unsafeMethod() { throw new UnsupportedOperationException("You shouldn't call that"); } }This gives more control over the content of the Event and gives access to the complete API supported by Sentry.