log4j2.tsx 11 KB

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