log4j2.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 [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.',
  133. {
  134. log4j2Code: <code />,
  135. sentryConsoleAppenderCode: <code />,
  136. sentryAppenderCode: <code />,
  137. }
  138. )}
  139. </p>
  140. ),
  141. code: `
  142. <?xml version="1.0" encoding="UTF-8"?>
  143. <Configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
  144. <Appenders>
  145. <Console name="Console" target="SYSTEM_OUT">
  146. <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  147. </Console>
  148. <Sentry name="Sentry"
  149. dsn=${dsn}>
  150. </Appenders>
  151. <Loggers>
  152. <Root level="info">
  153. <AppenderRef ref="Sentry"/>
  154. <AppenderRef ref="Console"/>
  155. </Root>
  156. </Loggers>
  157. </Configuration>
  158. `,
  159. additionalInfo: (
  160. <p>
  161. {tct(
  162. "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].",
  163. {
  164. code: <code />,
  165. link: (
  166. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/log4j2/#dsn-configuration" />
  167. ),
  168. }
  169. )}
  170. </p>
  171. ),
  172. },
  173. {
  174. description: (
  175. <p>
  176. {tct(
  177. "Next, you'll need to set your log levels, as illustrated here. You can learn more about [link:configuring log levels] in our documentation.",
  178. {
  179. link: (
  180. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/log4j2/#configure" />
  181. ),
  182. }
  183. )}
  184. </p>
  185. ),
  186. configurations: [
  187. {
  188. language: 'xml',
  189. code: `
  190. <!-- Setting minimumBreadcrumbLevel modifies the default minimum level to add breadcrumbs from INFO to DEBUG -->
  191. <!-- Setting minimumEventLevel the default minimum level to capture an event from ERROR to WARN -->
  192. <Sentry name="Sentry"
  193. dsn="${dsn}"
  194. minimumBreadcrumbLevel="DEBUG"
  195. minimumEventLevel="WARN"
  196. />
  197. `,
  198. },
  199. ],
  200. },
  201. ],
  202. },
  203. {
  204. type: StepType.VERIFY,
  205. description: t(
  206. 'Last, create an intentional error, so you can test that everything is working:'
  207. ),
  208. configurations: [
  209. {
  210. description: <h5>Java</h5>,
  211. language: 'java',
  212. code: `
  213. import java.lang.Exception;
  214. import io.sentry.Sentry;
  215. try {
  216. throw new Exception("This is a test.");
  217. } catch (Exception e) {
  218. Sentry.captureException(e);
  219. }
  220. `,
  221. },
  222. {
  223. description: <h5>Kotlin</h5>,
  224. language: 'java',
  225. code: `
  226. import java.lang.Exception
  227. import io.sentry.Sentry
  228. try {
  229. throw Exception("This is a test.")
  230. } catch (e: Exception) {
  231. Sentry.captureException(e)
  232. }
  233. `,
  234. },
  235. ],
  236. additionalInfo: (
  237. <Fragment>
  238. <p>
  239. {t(
  240. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  241. )}
  242. </p>
  243. <p>
  244. {t(
  245. "If you're an existing user and have disabled alerts, you won't receive this email."
  246. )}
  247. </p>
  248. </Fragment>
  249. ),
  250. },
  251. ];
  252. // Configuration End
  253. export function GettingStartedWithLog4j2({dsn, ...props}: ModuleProps) {
  254. return <Layout steps={steps({dsn})} introduction={introduction} {...props} />;
  255. }
  256. export default GettingStartedWithLog4j2;