logback.tsx 12 KB

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