logback.tsx 11 KB

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