log4j2.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import Link from 'sentry/components/links/link';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import type {
  6. BasePlatformOptions,
  7. Docs,
  8. DocsParams,
  9. OnboardingConfig,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {getJavaMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  12. import {feedbackOnboardingCrashApiJava} from 'sentry/gettingStartedDocs/java/java';
  13. import {t, tct} from 'sentry/locale';
  14. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  15. export enum PackageManager {
  16. GRADLE = 'gradle',
  17. MAVEN = 'maven',
  18. }
  19. const platformOptions = {
  20. packageManager: {
  21. label: t('Package Manager'),
  22. items: [
  23. {
  24. label: t('Gradle'),
  25. value: PackageManager.GRADLE,
  26. },
  27. {
  28. label: t('Maven'),
  29. value: PackageManager.MAVEN,
  30. },
  31. ],
  32. },
  33. } satisfies BasePlatformOptions;
  34. type PlatformOptions = typeof platformOptions;
  35. type Params = DocsParams<PlatformOptions>;
  36. const getGradleInstallSnippet = (params: Params) => `
  37. buildscript {
  38. repositories {
  39. mavenCentral()
  40. }
  41. }
  42. plugins {
  43. id "io.sentry.jvm.gradle" version "${getPackageVersion(
  44. params,
  45. 'sentry.java.android.gradle-plugin',
  46. '3.12.0'
  47. )}"
  48. }
  49. sentry {
  50. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  51. // This enables source context, allowing you to see your source
  52. // code as part of your stack traces in Sentry.
  53. includeSourceContext = true
  54. org = "${params.organization.slug}"
  55. projectName = "${params.projectSlug}"
  56. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  57. }`;
  58. const getMavenInstallSnippet = (params: Params) => `
  59. <build>
  60. <plugins>
  61. <plugin>
  62. <groupId>io.sentry</groupId>
  63. <artifactId>sentry-maven-plugin</artifactId>
  64. <version>${getPackageVersion(params, 'sentry.java.maven-plugin', '0.0.4')}</version>
  65. <extensions>true</extensions>
  66. <configuration>
  67. <!-- for showing output of sentry-cli -->
  68. <debugSentryCli>true</debugSentryCli>
  69. <org>${params.organization.slug}</org>
  70. <project>${params.projectSlug}</project>
  71. <!-- in case you're self hosting, provide the URL here -->
  72. <!--<url>http://localhost:8000/</url>-->
  73. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  74. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  75. </configuration>
  76. <executions>
  77. <execution>
  78. <goals>
  79. <!--
  80. Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  81. This enables source context, allowing you to see your source
  82. code as part of your stack traces in Sentry.
  83. -->
  84. <goal>uploadSourceBundle</goal>
  85. </goals>
  86. </execution>
  87. </executions>
  88. </plugin>
  89. </plugins>
  90. ...
  91. </build>`;
  92. const getConsoleAppenderSnippet = (params: Params) => `
  93. <?xml version="1.0" encoding="UTF-8"?>
  94. <Configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
  95. <Appenders>
  96. <Console name="Console" target="SYSTEM_OUT">
  97. <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  98. </Console>
  99. <Sentry name="Sentry"
  100. dsn=${params.dsn.public}>
  101. </Appenders>
  102. <Loggers>
  103. <Root level="info">
  104. <AppenderRef ref="Sentry"/>
  105. <AppenderRef ref="Console"/>
  106. </Root>
  107. </Loggers>
  108. </Configuration>`;
  109. const getLogLevelSnippet = (params: Params) => `
  110. <!-- Setting minimumBreadcrumbLevel modifies the default minimum level to add breadcrumbs from INFO to DEBUG -->
  111. <!-- Setting minimumEventLevel the default minimum level to capture an event from ERROR to WARN -->
  112. <Sentry name="Sentry"
  113. dsn="${params.dsn.public}"
  114. minimumBreadcrumbLevel="DEBUG"
  115. minimumEventLevel="WARN"
  116. />`;
  117. const getVerifyJavaSnippet = () => `
  118. import java.lang.Exception;
  119. import io.sentry.Sentry;
  120. try {
  121. throw new Exception("This is a test.");
  122. } catch (Exception e) {
  123. Sentry.captureException(e);
  124. }`;
  125. const getVerifyKotlinSnippet = () => `
  126. import java.lang.Exception
  127. import io.sentry.Sentry
  128. try {
  129. throw Exception("This is a test.")
  130. } catch (e: Exception) {
  131. Sentry.captureException(e)
  132. }`;
  133. const introduction = (
  134. <p>
  135. {tct(
  136. 'The [code:sentry-log4j2] library provides [log4jLink:Log4j 2.x] support for Sentry via an [appenderLink:Appender] that sends logged exceptions to Sentry.',
  137. {
  138. log4jLink: <ExternalLink href="https://logging.apache.org/log4j/2.x//" />,
  139. appenderLink: (
  140. <ExternalLink href="https://logging.apache.org/log4j/2.x/manual/appenders.html" />
  141. ),
  142. code: <code />,
  143. }
  144. )}
  145. </p>
  146. );
  147. const onboarding: OnboardingConfig<PlatformOptions> = {
  148. introduction: () => introduction,
  149. install: params => [
  150. {
  151. type: StepType.INSTALL,
  152. description: t(
  153. "Install Sentry's integration with Log4j 2.x using %s:",
  154. params.platformOptions.packageManager === PackageManager.GRADLE
  155. ? 'Gradle'
  156. : 'Maven'
  157. ),
  158. configurations: [
  159. {
  160. description: tct(
  161. 'To see source context in Sentry, you have to generate an auth token by visiting the [link:Organization Auth Tokens] settings. You can then set the token as an environment variable that is used by the build plugins.',
  162. {
  163. link: <Link to="/settings/auth-tokens/" />,
  164. }
  165. ),
  166. language: 'bash',
  167. code: 'SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___',
  168. },
  169. ...(params.platformOptions.packageManager === PackageManager.GRADLE
  170. ? [
  171. {
  172. description: tct(
  173. 'The [link:Sentry Gradle Plugin] automatically installs the Sentry SDK as well as available integrations for your dependencies. Add the following to your [code:build.gradle] file:',
  174. {
  175. code: <code />,
  176. link: (
  177. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  178. ),
  179. }
  180. ),
  181. language: 'groovy',
  182. code: getGradleInstallSnippet(params),
  183. },
  184. ]
  185. : []),
  186. ...(params.platformOptions.packageManager === PackageManager.MAVEN
  187. ? [
  188. {
  189. language: 'xml',
  190. partialLoading: params.sourcePackageRegistries?.isLoading,
  191. description: tct(
  192. 'The [link:Sentry Maven Plugin] automatically installs the Sentry SDK as well as available integrations for your dependencies. Add the following to your [code:pom.xml] file:',
  193. {
  194. code: <code />,
  195. link: (
  196. <ExternalLink href="https://github.com/getsentry/sentry-maven-plugin" />
  197. ),
  198. }
  199. ),
  200. code: getMavenInstallSnippet(params),
  201. },
  202. ]
  203. : []),
  204. ],
  205. additionalInfo: tct(
  206. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  207. {
  208. link: (
  209. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  210. ),
  211. }
  212. ),
  213. },
  214. ],
  215. configure: params => [
  216. {
  217. type: StepType.CONFIGURE,
  218. description: t(
  219. "Configure Sentry as soon as possible in your application's lifecycle:"
  220. ),
  221. configurations: [
  222. {
  223. language: 'xml',
  224. description: tct(
  225. 'The following example using the [code:log4j2.xml] format to configure a [code:ConsoleAppender] that logs to standard out at the INFO level, and a [code:SentryAppender] that logs to the Sentry server at the ERROR level.',
  226. {
  227. code: <code />,
  228. }
  229. ),
  230. code: getConsoleAppenderSnippet(params),
  231. additionalInfo: tct(
  232. "You'll also need to configure your DSN (client key) if it's not already in the [code:log4j2.xml] configuration. Learn more in [link:our documentation for DSN configuration].",
  233. {
  234. code: <code />,
  235. link: (
  236. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/log4j2/#dsn-configuration" />
  237. ),
  238. }
  239. ),
  240. },
  241. {
  242. description: tct(
  243. "Next, you'll need to set your log levels, as illustrated here. You can learn more about [link:configuring log levels] in our documentation.",
  244. {
  245. link: (
  246. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/log4j2/#configure" />
  247. ),
  248. }
  249. ),
  250. configurations: [
  251. {
  252. language: 'xml',
  253. code: getLogLevelSnippet(params),
  254. },
  255. ],
  256. },
  257. ],
  258. },
  259. ],
  260. verify: () => [
  261. {
  262. type: StepType.VERIFY,
  263. description: t(
  264. 'Last, create an intentional error, so you can test that everything is working:'
  265. ),
  266. configurations: [
  267. {
  268. language: 'java',
  269. code: [
  270. {
  271. language: 'java',
  272. label: 'Java',
  273. value: 'java',
  274. code: getVerifyJavaSnippet(),
  275. },
  276. {
  277. language: 'kotlin',
  278. label: 'Kotlin',
  279. value: 'kotlin',
  280. code: getVerifyKotlinSnippet(),
  281. },
  282. ],
  283. },
  284. ],
  285. additionalInfo: (
  286. <Fragment>
  287. <p>
  288. {t(
  289. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  290. )}
  291. </p>
  292. <p>
  293. {t(
  294. "If you're an existing user and have disabled alerts, you won't receive this email."
  295. )}
  296. </p>
  297. </Fragment>
  298. ),
  299. },
  300. ],
  301. nextSteps: () => [
  302. {
  303. id: 'examples',
  304. name: t('Examples'),
  305. description: t('Check out our sample applications.'),
  306. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  307. },
  308. ],
  309. };
  310. const docs: Docs<PlatformOptions> = {
  311. platformOptions,
  312. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiJava,
  313. crashReportOnboarding: feedbackOnboardingCrashApiJava,
  314. customMetricsOnboarding: getJavaMetricsOnboarding(),
  315. onboarding,
  316. };
  317. export default docs;