log4j2.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 PlaformOptionKey = '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<PlaformOptionKey, 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: t("Add the Sentry SDK to your project's dependencies"),
  130. code: `
  131. <dependency>
  132. <groupId>io.sentry</groupId>
  133. <artifactId>sentry-log4j2</artifactId>
  134. <version>${
  135. sourcePackageRegistries?.isLoading
  136. ? t('\u2026loading')
  137. : sourcePackageRegistries?.data?.['sentry.java.log4j2']?.version ?? '6.27.0'
  138. }</version>
  139. </dependency>
  140. `,
  141. },
  142. {
  143. language: 'xml',
  144. partialLoading: sourcePackageRegistries?.isLoading,
  145. description: t(
  146. 'To upload your source code to Sentry so it can be shown in stack traces, use our Maven plugin.'
  147. ),
  148. code: `
  149. <build>
  150. <plugins>
  151. <plugin>
  152. <groupId>io.sentry</groupId>
  153. <artifactId>sentry-maven-plugin</artifactId>
  154. <version>${
  155. sourcePackageRegistries?.isLoading
  156. ? t('\u2026loading')
  157. : sourcePackageRegistries?.data?.['sentry.java.mavenplugin']?.version ?? '0.0.4'
  158. }</version>
  159. <configuration>
  160. <!-- for showing output of sentry-cli -->
  161. <debugSentryCli>true</debugSentryCli>
  162. <org>${organizationSlug}</org>
  163. <project>${projectSlug}</project>
  164. <!-- in case you're self hosting, provide the URL here -->
  165. <!--<url>http://localhost:8000/</url>-->
  166. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  167. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  168. </configuration>
  169. <executions>
  170. <execution>
  171. <phase>generate-resources</phase>
  172. <goals>
  173. <goal>uploadSourceBundle</goal>
  174. </goals>
  175. </execution>
  176. </executions>
  177. </plugin>
  178. </plugins>
  179. ...
  180. </build>
  181. `,
  182. },
  183. ]
  184. : []),
  185. ],
  186. },
  187. {
  188. type: StepType.CONFIGURE,
  189. description: t(
  190. "Configure Sentry as soon as possible in your application's lifecycle:"
  191. ),
  192. configurations: [
  193. {
  194. language: 'xml',
  195. description: (
  196. <p>
  197. {tct(
  198. '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.',
  199. {
  200. log4j2Code: <code />,
  201. sentryConsoleAppenderCode: <code />,
  202. sentryAppenderCode: <code />,
  203. }
  204. )}
  205. </p>
  206. ),
  207. code: `
  208. <?xml version="1.0" encoding="UTF-8"?>
  209. <Configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
  210. <Appenders>
  211. <Console name="Console" target="SYSTEM_OUT">
  212. <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  213. </Console>
  214. <Sentry name="Sentry"
  215. dsn=${dsn}>
  216. </Appenders>
  217. <Loggers>
  218. <Root level="info">
  219. <AppenderRef ref="Sentry"/>
  220. <AppenderRef ref="Console"/>
  221. </Root>
  222. </Loggers>
  223. </Configuration>
  224. `,
  225. additionalInfo: (
  226. <p>
  227. {tct(
  228. "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].",
  229. {
  230. code: <code />,
  231. link: (
  232. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/log4j2/#dsn-configuration" />
  233. ),
  234. }
  235. )}
  236. </p>
  237. ),
  238. },
  239. {
  240. description: (
  241. <p>
  242. {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. </p>
  251. ),
  252. configurations: [
  253. {
  254. language: 'xml',
  255. code: `
  256. <!-- Setting minimumBreadcrumbLevel modifies the default minimum level to add breadcrumbs from INFO to DEBUG -->
  257. <!-- Setting minimumEventLevel the default minimum level to capture an event from ERROR to WARN -->
  258. <Sentry name="Sentry"
  259. dsn="${dsn}"
  260. minimumBreadcrumbLevel="DEBUG"
  261. minimumEventLevel="WARN"
  262. />
  263. `,
  264. },
  265. ],
  266. },
  267. ],
  268. },
  269. {
  270. type: StepType.VERIFY,
  271. description: t(
  272. 'Last, create an intentional error, so you can test that everything is working:'
  273. ),
  274. configurations: [
  275. {
  276. language: 'java',
  277. code: [
  278. {
  279. language: 'java',
  280. label: 'Java',
  281. value: 'java',
  282. code: `
  283. import java.lang.Exception;
  284. import io.sentry.Sentry;
  285. try {
  286. throw new Exception("This is a test.");
  287. } catch (Exception e) {
  288. Sentry.captureException(e);
  289. }`,
  290. },
  291. {
  292. language: 'java',
  293. label: 'Kotlin',
  294. value: 'kotlin',
  295. code: `
  296. import java.lang.Exception
  297. import io.sentry.Sentry
  298. try {
  299. throw Exception("This is a test.")
  300. } catch (e: Exception) {
  301. Sentry.captureException(e)
  302. }`,
  303. },
  304. ],
  305. },
  306. ],
  307. additionalInfo: (
  308. <Fragment>
  309. <p>
  310. {t(
  311. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  312. )}
  313. </p>
  314. <p>
  315. {t(
  316. "If you're an existing user and have disabled alerts, you won't receive this email."
  317. )}
  318. </p>
  319. </Fragment>
  320. ),
  321. },
  322. ];
  323. export const nextSteps = [
  324. {
  325. id: 'examples',
  326. name: t('Examples'),
  327. description: t('Check out our sample applications.'),
  328. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  329. },
  330. ];
  331. // Configuration End
  332. export function GettingStartedWithLog4j2({
  333. dsn,
  334. sourcePackageRegistries,
  335. projectSlug,
  336. organization,
  337. ...props
  338. }: ModuleProps) {
  339. const optionValues = useUrlPlatformOptions(platformOptions);
  340. const nextStepDocs = [...nextSteps];
  341. return (
  342. <Layout
  343. steps={steps({
  344. dsn,
  345. sourcePackageRegistries,
  346. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  347. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  348. packageManager: optionValues.packageManager as PackageManager,
  349. })}
  350. nextSteps={nextStepDocs}
  351. introduction={introduction}
  352. platformOptions={platformOptions}
  353. projectSlug={projectSlug}
  354. {...props}
  355. />
  356. );
  357. }
  358. export default GettingStartedWithLog4j2;