log4j2.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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}>
  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}"
  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 [log4j2Code:log4j2.xml] format to configure a [sentryConsoleAppenderCode:ConsoleAppender] that logs to standard out at the INFO level, and a [sentryAppenderCode:SentryAppender] that logs to the Sentry server at the ERROR level.',
  226. {
  227. log4j2Code: <code />,
  228. sentryConsoleAppenderCode: <code />,
  229. sentryAppenderCode: <code />,
  230. }
  231. ),
  232. code: getConsoleAppenderSnippet(params),
  233. additionalInfo: tct(
  234. "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].",
  235. {
  236. code: <code />,
  237. link: (
  238. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/log4j2/#dsn-configuration" />
  239. ),
  240. }
  241. ),
  242. },
  243. {
  244. description: tct(
  245. "Next, you'll need to set your log levels, as illustrated here. You can learn more about [link:configuring log levels] in our documentation.",
  246. {
  247. link: (
  248. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/log4j2/#configure" />
  249. ),
  250. }
  251. ),
  252. configurations: [
  253. {
  254. language: 'xml',
  255. code: getLogLevelSnippet(params),
  256. },
  257. ],
  258. },
  259. ],
  260. },
  261. ],
  262. verify: () => [
  263. {
  264. type: StepType.VERIFY,
  265. description: t(
  266. 'Last, create an intentional error, so you can test that everything is working:'
  267. ),
  268. configurations: [
  269. {
  270. language: 'java',
  271. code: [
  272. {
  273. language: 'java',
  274. label: 'Java',
  275. value: 'java',
  276. code: getVerifyJavaSnippet(),
  277. },
  278. {
  279. language: 'java',
  280. label: 'Kotlin',
  281. value: 'kotlin',
  282. code: getVerifyKotlinSnippet(),
  283. },
  284. ],
  285. },
  286. ],
  287. additionalInfo: (
  288. <Fragment>
  289. <p>
  290. {t(
  291. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  292. )}
  293. </p>
  294. <p>
  295. {t(
  296. "If you're an existing user and have disabled alerts, you won't receive this email."
  297. )}
  298. </p>
  299. </Fragment>
  300. ),
  301. },
  302. ],
  303. nextSteps: () => [
  304. {
  305. id: 'examples',
  306. name: t('Examples'),
  307. description: t('Check out our sample applications.'),
  308. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  309. },
  310. ],
  311. };
  312. const docs: Docs<PlatformOptions> = {
  313. platformOptions,
  314. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiJava,
  315. crashReportOnboarding: feedbackOnboardingCrashApiJava,
  316. customMetricsOnboarding: getJavaMetricsOnboarding(),
  317. onboarding,
  318. };
  319. export default docs;