logback.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import Link from 'sentry/components/links/link';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import type {
  6. BasePlatformOptions,
  7. Docs,
  8. DocsParams,
  9. OnboardingConfig,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {feedbackOnboardingCrashApiJava} from 'sentry/gettingStartedDocs/java/java';
  12. import {t, tct} from 'sentry/locale';
  13. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  14. export enum PackageManager {
  15. GRADLE = 'gradle',
  16. MAVEN = 'maven',
  17. }
  18. const platformOptions = {
  19. packageManager: {
  20. label: t('Package Manager'),
  21. items: [
  22. {
  23. label: t('Gradle'),
  24. value: PackageManager.GRADLE,
  25. },
  26. {
  27. label: t('Maven'),
  28. value: PackageManager.MAVEN,
  29. },
  30. ],
  31. },
  32. } satisfies BasePlatformOptions;
  33. type PlatformOptions = typeof platformOptions;
  34. type Params = DocsParams<PlatformOptions>;
  35. const getGradleInstallSnippet = (params: Params) => `
  36. buildscript {
  37. repositories {
  38. mavenCentral()
  39. }
  40. }
  41. plugins {
  42. id "io.sentry.jvm.gradle" version "${getPackageVersion(
  43. params,
  44. 'sentry.java.android.gradle-plugin',
  45. '3.12.0'
  46. )}"
  47. }
  48. sentry {
  49. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  50. // This enables source context, allowing you to see your source
  51. // code as part of your stack traces in Sentry.
  52. includeSourceContext = true
  53. org = "${params.organization.slug}"
  54. projectName = "${params.projectSlug}"
  55. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  56. }`;
  57. const getMavenInstallSnippet = (params: Params) => `
  58. <build>
  59. <plugins>
  60. <plugin>
  61. <groupId>io.sentry</groupId>
  62. <artifactId>sentry-maven-plugin</artifactId>
  63. <version>${getPackageVersion(params, 'sentry.java.maven-plugin', '0.0.4')}</version>
  64. <extensions>true</extensions>
  65. <configuration>
  66. <!-- for showing output of sentry-cli -->
  67. <debugSentryCli>true</debugSentryCli>
  68. <org>${params.organization.slug}</org>
  69. <project>${params.projectSlug}</project>
  70. <!-- in case you're self hosting, provide the URL here -->
  71. <!--<url>http://localhost:8000/</url>-->
  72. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  73. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  74. </configuration>
  75. <executions>
  76. <execution>
  77. <goals>
  78. <!--
  79. Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  80. This enables source context, allowing you to see your source
  81. code as part of your stack traces in Sentry.
  82. -->
  83. <goal>uploadSourceBundle</goal>
  84. </goals>
  85. </execution>
  86. </executions>
  87. </plugin>
  88. </plugins>
  89. ...
  90. </build>`;
  91. const getConsoleAppenderSnippet = (params: Params) => `
  92. <configuration>
  93. <!-- Configure the Console appender -->
  94. <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
  95. <encoder>
  96. <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
  97. </encoder>
  98. </appender>
  99. <!-- Configure the Sentry appender, overriding the logging threshold to the WARN level -->
  100. <appender name="Sentry" class="io.sentry.logback.SentryAppender">
  101. <options>
  102. <dsn>${params.dsn}</dsn>
  103. </options>
  104. </appender>
  105. <!-- Enable the Console and Sentry appenders, Console is provided as an example
  106. of a non-Sentry logger that is set to a different logging threshold -->
  107. <root level="INFO">
  108. <appender-ref ref="Console" />
  109. <appender-ref ref="Sentry" />
  110. </root>
  111. </configuration>`;
  112. const getLogLevelSnippet = (params: Params) => `
  113. <appender name="Sentry" class="io.sentry.logback.SentryAppender">
  114. <options>
  115. <dsn>${params.dsn}</dsn>
  116. </options>
  117. <!-- Optionally change minimum Event level. Default for Events is ERROR -->
  118. <minimumEventLevel>WARN</minimumEventLevel>
  119. <!-- Optionally change minimum Breadcrumbs level. Default for Breadcrumbs is INFO -->
  120. <minimumBreadcrumbLevel>DEBUG</minimumBreadcrumbLevel>
  121. </appender>`;
  122. const getVerifyJavaSnippet = () => `
  123. import java.lang.Exception;
  124. import io.sentry.Sentry;
  125. try {
  126. throw new Exception("This is a test.");
  127. } catch (Exception e) {
  128. Sentry.captureException(e);
  129. }`;
  130. const getVerifyKotlinSnippet = () => `
  131. import java.lang.Exception
  132. import io.sentry.Sentry
  133. try {
  134. throw Exception("This is a test.")
  135. } catch (e: Exception) {
  136. Sentry.captureException(e)
  137. }`;
  138. const onboarding: OnboardingConfig<PlatformOptions> = {
  139. introduction: () =>
  140. tct(
  141. 'The sentry-logback library provides Logback support for Sentry using an [link:Appender] that sends logged exceptions to Sentry.',
  142. {
  143. link: (
  144. <ExternalLink href="https://logback.qos.ch/apidocs/ch/qos/logback/core/Appender.html" />
  145. ),
  146. }
  147. ),
  148. install: params => [
  149. {
  150. type: StepType.INSTALL,
  151. description: t(
  152. "Install Sentry's integration with Logback using %s:",
  153. params.platformOptions.packageManager === PackageManager.GRADLE
  154. ? 'Gradle'
  155. : 'Maven'
  156. ),
  157. configurations: [
  158. {
  159. description: tct(
  160. 'To see source context in Sentry, you have to generate an auth token by visiting the [link:Organization Auth Tokens] settings. You can then set the token as an environment variable that is used by the build plugins.',
  161. {
  162. link: <Link to="/settings/auth-tokens/" />,
  163. }
  164. ),
  165. language: 'bash',
  166. code: 'SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___',
  167. },
  168. ...(params.platformOptions.packageManager === PackageManager.GRADLE
  169. ? [
  170. {
  171. description: tct(
  172. 'The [link:Sentry Gradle Plugin] automatically installs the Sentry SDK as well as available integrations for your dependencies. Add the following to your [code:build.gradle] file:',
  173. {
  174. code: <code />,
  175. link: (
  176. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  177. ),
  178. }
  179. ),
  180. language: 'groovy',
  181. code: getGradleInstallSnippet(params),
  182. },
  183. ]
  184. : []),
  185. ...(params.platformOptions.packageManager === PackageManager.MAVEN
  186. ? [
  187. {
  188. language: 'xml',
  189. partialLoading: params.sourcePackageRegistries?.isLoading,
  190. description: tct(
  191. 'The [link:Sentry Maven Plugin] automatically installs the Sentry SDK as well as available integrations for your dependencies. Add the following to your [code:pom.xml] file:',
  192. {
  193. code: <code />,
  194. link: (
  195. <ExternalLink href="https://github.com/getsentry/sentry-maven-plugin" />
  196. ),
  197. }
  198. ),
  199. code: getMavenInstallSnippet(params),
  200. },
  201. ]
  202. : []),
  203. ],
  204. additionalInfo: tct(
  205. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  206. {
  207. link: (
  208. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  209. ),
  210. }
  211. ),
  212. },
  213. ],
  214. configure: (params: Params) => [
  215. {
  216. type: StepType.CONFIGURE,
  217. description: t(
  218. "Configure Sentry as soon as possible in your application's lifecycle:"
  219. ),
  220. configurations: [
  221. {
  222. language: 'xml',
  223. description: t(
  224. 'The following example configures a ConsoleAppender that logs to standard out at the INFO level, and a SentryAppender that logs to the Sentry server at the ERROR level. This only an example of a non-Sentry appender set to a different logging threshold, similar to what you may already have in your project.'
  225. ),
  226. code: getConsoleAppenderSnippet(params),
  227. additionalInfo: tct(
  228. "You'll also need to configure your DSN (client key) if it's not already in the [code:logback.xml] configuration. Learn more in [link:our documentation for DSN configuration].",
  229. {
  230. code: <code />,
  231. link: (
  232. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/logback/#dsn-configuration/" />
  233. ),
  234. }
  235. ),
  236. },
  237. {
  238. description: tct(
  239. "Next, you'll need to set your log levels, as illustrated here. You can learn more about [link:configuring log levels] in our documentation.",
  240. {
  241. link: (
  242. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/logback/#minimum-log-level/" />
  243. ),
  244. }
  245. ),
  246. configurations: [
  247. {
  248. language: 'xml',
  249. code: getLogLevelSnippet(params),
  250. },
  251. ],
  252. },
  253. ],
  254. },
  255. ],
  256. verify: () => [
  257. {
  258. type: StepType.VERIFY,
  259. description: t(
  260. 'Last, create an intentional error, so you can test that everything is working:'
  261. ),
  262. configurations: [
  263. {
  264. language: 'java',
  265. code: [
  266. {
  267. language: 'java',
  268. label: 'Java',
  269. value: 'java',
  270. code: getVerifyJavaSnippet(),
  271. },
  272. {
  273. language: 'java',
  274. label: 'Kotlin',
  275. value: 'kotlin',
  276. code: getVerifyKotlinSnippet(),
  277. },
  278. ],
  279. },
  280. ],
  281. additionalInfo: (
  282. <Fragment>
  283. <p>
  284. {t(
  285. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  286. )}
  287. </p>
  288. <p>
  289. {t(
  290. "If you're an existing user and have disabled alerts, you won't receive this email."
  291. )}
  292. </p>
  293. </Fragment>
  294. ),
  295. },
  296. {
  297. title: t('Other build tools'),
  298. additionalInfo: tct(
  299. 'For other dependency managers see the [link:central Maven repository].',
  300. {
  301. link: (
  302. <ExternalLink href="https://search.maven.org/artifact/io.sentry/sentry-logback" />
  303. ),
  304. }
  305. ),
  306. },
  307. ],
  308. nextSteps: () => [
  309. {
  310. id: 'examples',
  311. name: t('Examples'),
  312. description: t('Check out our sample applications.'),
  313. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  314. },
  315. ],
  316. };
  317. const docs: Docs<PlatformOptions> = {
  318. onboarding,
  319. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiJava,
  320. platformOptions,
  321. };
  322. export default docs;