log4j2.tsx 8.8 KB

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