log4j2.tsx 9.1 KB

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