log4j2.tsx 11 KB

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