log4j2.tsx 9.6 KB

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