{ "html": "
Using Maven:
\n<dependency>\n <groupId>io.sentry</groupId>\n <artifactId>sentry</artifactId>\n <version>1.6.3</version>\n</dependency>\n
Using Gradle:
\ncompile 'io.sentry:sentry:1.6.3'\n
Using SBT:
\nlibraryDependencies += "io.sentry" % "sentry" % "1.6.3"\n
For other dependency managers see the central Maven repository.
\nTo report an event manually you need to initialize a SentryClient
. It is recommended\nthat you use the static API via the Sentry
class, but you can also construct and manage\nyour own SentryClient
instance. An example of each style is shown below:
import io.sentry.context.Context;\nimport io.sentry.event.BreadcrumbBuilder;\nimport io.sentry.event.UserBuilder;\n\npublic class MyClass {\n private static SentryClient sentry;\n\n public static void main(String... args) {\n /*\n It is recommended that you use the DSN detection system, which\n will check the environment variable "SENTRY_DSN", the Java\n System Property "sentry.dsn", or the "sentry.properties" file\n in your classpath. This makes it easier to provide and adjust\n your DSN without needing to change your code. See the configuration\n page for more information.\n */\n Sentry.init();\n\n // You can also manually provide the DSN to the ``init`` method.\n String dsn = args[0];\n Sentry.init(dsn);\n\n /*\n It is possible to go around the static ``Sentry`` API, which means\n you are responsible for making the SentryClient instance available\n to your code.\n */\n sentry = SentryClientFactory.sentryClient();\n\n MyClass myClass = new MyClass();\n myClass.logWithStaticAPI();\n myClass.logWithInstanceAPI();\n }\n\n /**\n * An example method that throws an exception.\n */\n void unsafeMethod() {\n throw new UnsupportedOperationException("You shouldn't call this!");\n }\n\n /**\n * Examples using the (recommended) static API.\n */\n void logWithStaticAPI() {\n // Note that all fields set on the context are optional. Context data is copied onto\n // all future events in the current context (until the context is cleared).\n\n // Record a breadcrumb in the current context. By default the last 100 breadcrumbs are kept.\n Sentry.getContext().recordBreadcrumb(\n new BreadcrumbBuilder().setMessage("User made an action").build()\n );\n\n // Set the user in the current context.\n Sentry.getContext().setUser(\n new UserBuilder().setEmail("hello@sentry.io").build()\n );\n\n // Add extra data to future events in this context.\n Sentry.getContext().addExtra("extra", "thing");\n\n // Add an additional tag to future events in this context.\n Sentry.getContext().addTag("tagName", "tagValue");\n\n /*\n This sends a simple event to Sentry using the statically stored instance\n that was created in the ``main`` method.\n */\n Sentry.capture("This is a test");\n\n try {\n unsafeMethod();\n } catch (Exception e) {\n // This sends an exception event to Sentry using the statically stored instance\n // that was created in the ``main`` method.\n Sentry.capture(e);\n }\n }\n\n /**\n * Examples that use the SentryClient instance directly.\n */\n void logWithInstanceAPI() {\n // Retrieve the current context.\n Context context = sentry.getContext();\n\n // Record a breadcrumb in the current context. By default the last 100 breadcrumbs are kept.\n context.recordBreadcrumb(new BreadcrumbBuilder().setMessage("User made an action").build());\n\n // Set the user in the current context.\n context.setUser(new UserBuilder().setEmail("hello@sentry.io").build());\n\n // This sends a simple event to Sentry.\n sentry.sendMessage("This is a test");\n\n try {\n unsafeMethod();\n } catch (Exception e) {\n // This sends an exception event to Sentry.\n sentry.sendException(e);\n }\n }\n}\n
For more complex messages, you’ll need to build an Event
with the\nEventBuilder
class:
import io.sentry.Sentry;\n import io.sentry.event.Event;\n import io.sentry.event.EventBuilder;\n import io.sentry.event.interfaces.ExceptionInterface;\n\n public class MyClass {\n public static void main(String... args) {\n Sentry.init();\n }\n\n void unsafeMethod() {\n throw new UnsupportedOperationException("You shouldn't call this!");\n }\n\n void logSimpleMessage() {\n // This sends an event to Sentry.\n EventBuilder eventBuilder = new EventBuilder()\n .withMessage("This is a test")\n .withLevel(Event.Level.INFO)\n .withLogger(MyClass.class.getName());\n\n // Note that the *unbuilt* EventBuilder instance is passed in so that\n // EventBuilderHelpers are run to add extra information to your event.\n Sentry.capture(eventBuilder);\n }\n\n void logException() {\n try {\n unsafeMethod();\n } catch (Exception e) {\n // This sends an exception event to Sentry.\n EventBuilder eventBuilder = new EventBuilder()\n .withMessage("Exception caught")\n .withLevel(Event.Level.ERROR)\n .withLogger(MyClass.class.getName())\n .withSentryInterface(new ExceptionInterface(e));\n\n // Note that the *unbuilt* EventBuilder instance is passed in so that\n // EventBuilderHelpers are run to add extra information to your event.\n Sentry.capture(eventBuilder);\n }\n }\n}\n
You can also implement an EventBuilderHelper
that is able to automatically\nenhance outgoing events.
import io.sentry.Sentry;\nimport io.sentry.SentryClient;\nimport io.sentry.event.EventBuilder;\nimport io.sentry.event.helper.EventBuilderHelper;\n\npublic class MyClass {\n public void myMethod() {\n SentryClient client = Sentry.getStoredClient();\n\n EventBuilderHelper myEventBuilderHelper = new EventBuilderHelper() {\n @Override\n public void helpBuildingEvent(EventBuilder eventBuilder) {\n eventBuilder.withMessage("Overwritten by myEventBuilderHelper!");\n }\n };\n\n // Add an ``EventBuilderHelper`` to the current client instance. Note that\n // this helper will process *all* future events.\n client.addBuilderHelper(myEventBuilderHelper);\n\n // Send an event to Sentry. During construction of the event the message\n // body will be overwritten by ``myEventBuilderHelper``.\n Sentry.capture("Hello, world!");\n }\n}\n