log4j2.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 {
  6. BasePlatformOptions,
  7. Docs,
  8. DocsParams,
  9. OnboardingConfig,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {t, tct} from 'sentry/locale';
  12. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  13. export enum PackageManager {
  14. GRADLE = 'gradle',
  15. MAVEN = 'maven',
  16. }
  17. const platformOptions = {
  18. packageManager: {
  19. label: t('Package Manager'),
  20. items: [
  21. {
  22. label: t('Gradle'),
  23. value: PackageManager.GRADLE,
  24. },
  25. {
  26. label: t('Maven'),
  27. value: PackageManager.MAVEN,
  28. },
  29. ],
  30. },
  31. } satisfies BasePlatformOptions;
  32. type PlatformOptions = typeof platformOptions;
  33. type Params = DocsParams<PlatformOptions>;
  34. const getGradleInstallSnippet = (params: Params) => `
  35. buildscript {
  36. repositories {
  37. mavenCentral()
  38. }
  39. }
  40. plugins {
  41. id "io.sentry.jvm.gradle" version "${getPackageVersion(
  42. params,
  43. 'sentry.java.android.gradle-plugin',
  44. '3.12.0'
  45. )}"
  46. }
  47. sentry {
  48. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  49. // This enables source context, allowing you to see your source
  50. // code as part of your stack traces in Sentry.
  51. includeSourceContext = true
  52. org = "${params.organization.slug}"
  53. projectName = "${params.projectSlug}"
  54. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  55. }`;
  56. const getMavenInstallSnippet = (params: Params) => `
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>io.sentry</groupId>
  61. <artifactId>sentry-maven-plugin</artifactId>
  62. <version>${getPackageVersion(params, 'sentry.java.maven-plugin', '0.0.4')}</version>
  63. <extensions>true</extensions>
  64. <configuration>
  65. <!-- for showing output of sentry-cli -->
  66. <debugSentryCli>true</debugSentryCli>
  67. <org>${params.organization.slug}</org>
  68. <project>${params.projectSlug}</project>
  69. <!-- in case you're self hosting, provide the URL here -->
  70. <!--<url>http://localhost:8000/</url>-->
  71. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  72. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  73. </configuration>
  74. <executions>
  75. <execution>
  76. <goals>
  77. <!--
  78. Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  79. This enables source context, allowing you to see your source
  80. code as part of your stack traces in Sentry.
  81. -->
  82. <goal>uploadSourceBundle</goal>
  83. </goals>
  84. </execution>
  85. </executions>
  86. </plugin>
  87. </plugins>
  88. ...
  89. </build>`;
  90. const getConsoleAppenderSnippet = (params: Params) => `
  91. <?xml version="1.0" encoding="UTF-8"?>
  92. <Configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
  93. <Appenders>
  94. <Console name="Console" target="SYSTEM_OUT">
  95. <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  96. </Console>
  97. <Sentry name="Sentry"
  98. dsn=${params.dsn}>
  99. </Appenders>
  100. <Loggers>
  101. <Root level="info">
  102. <AppenderRef ref="Sentry"/>
  103. <AppenderRef ref="Console"/>
  104. </Root>
  105. </Loggers>
  106. </Configuration>`;
  107. const getLogLevelSnippet = (params: Params) => `
  108. <!-- Setting minimumBreadcrumbLevel modifies the default minimum level to add breadcrumbs from INFO to DEBUG -->
  109. <!-- Setting minimumEventLevel the default minimum level to capture an event from ERROR to WARN -->
  110. <Sentry name="Sentry"
  111. dsn="${params.dsn}"
  112. minimumBreadcrumbLevel="DEBUG"
  113. minimumEventLevel="WARN"
  114. />`;
  115. const getVerifyJavaSnippet = () => `
  116. import java.lang.Exception;
  117. import io.sentry.Sentry;
  118. try {
  119. throw new Exception("This is a test.");
  120. } catch (Exception e) {
  121. Sentry.captureException(e);
  122. }`;
  123. const getVerifyKotlinSnippet = () => `
  124. import java.lang.Exception
  125. import io.sentry.Sentry
  126. try {
  127. throw Exception("This is a test.")
  128. } catch (e: Exception) {
  129. Sentry.captureException(e)
  130. }`;
  131. const introduction = (
  132. <p>
  133. {tct(
  134. 'The [code:sentry-log4j2] library provides [log4jLink:Log4j 2.x] support for Sentry via an [appenderLink:Appender] that sends logged exceptions to Sentry.',
  135. {
  136. log4jLink: <ExternalLink href="https://logging.apache.org/log4j/2.x//" />,
  137. appenderLink: (
  138. <ExternalLink href="https://logging.apache.org/log4j/2.x/manual/appenders.html" />
  139. ),
  140. code: <code />,
  141. }
  142. )}
  143. </p>
  144. );
  145. const onboarding: OnboardingConfig<PlatformOptions> = {
  146. introduction: () => introduction,
  147. install: params => [
  148. {
  149. type: StepType.INSTALL,
  150. description: t(
  151. "Install Sentry's integration with Log4j 2.x using %s:",
  152. params.platformOptions.packageManager === PackageManager.GRADLE
  153. ? 'Gradle'
  154. : 'Maven'
  155. ),
  156. configurations: [
  157. {
  158. description: tct(
  159. '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.',
  160. {
  161. link: <Link to="/settings/auth-tokens/" />,
  162. }
  163. ),
  164. language: 'bash',
  165. code: 'SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___',
  166. },
  167. ...(params.platformOptions.packageManager === PackageManager.GRADLE
  168. ? [
  169. {
  170. description: tct(
  171. '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:',
  172. {
  173. code: <code />,
  174. link: (
  175. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  176. ),
  177. }
  178. ),
  179. language: 'groovy',
  180. code: getGradleInstallSnippet(params),
  181. },
  182. ]
  183. : []),
  184. ...(params.platformOptions.packageManager === PackageManager.MAVEN
  185. ? [
  186. {
  187. language: 'xml',
  188. partialLoading: params.sourcePackageRegistries?.isLoading,
  189. description: tct(
  190. '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:',
  191. {
  192. code: <code />,
  193. link: (
  194. <ExternalLink href="https://github.com/getsentry/sentry-maven-plugin" />
  195. ),
  196. }
  197. ),
  198. code: getMavenInstallSnippet(params),
  199. },
  200. ]
  201. : []),
  202. ],
  203. additionalInfo: tct(
  204. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  205. {
  206. link: (
  207. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  208. ),
  209. }
  210. ),
  211. },
  212. ],
  213. configure: params => [
  214. {
  215. type: StepType.CONFIGURE,
  216. description: t(
  217. "Configure Sentry as soon as possible in your application's lifecycle:"
  218. ),
  219. configurations: [
  220. {
  221. language: 'xml',
  222. description: tct(
  223. '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.',
  224. {
  225. log4j2Code: <code />,
  226. sentryConsoleAppenderCode: <code />,
  227. sentryAppenderCode: <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: 'java',
  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. onboarding,
  313. };
  314. export default docs;