log4j2.tsx 7.7 KB

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