log4j2.tsx 7.6 KB

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