logback.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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: t("Add the Sentry SDK to your project's dependencies"),
  128. code: `
  129. <dependency>
  130. <groupId>io.sentry</groupId>
  131. <artifactId>sentry-log4j2</artifactId>
  132. <version>${
  133. sourcePackageRegistries?.isLoading
  134. ? t('\u2026loading')
  135. : sourcePackageRegistries?.data?.['sentry.java.log4j2']?.version ?? '6.27.0'
  136. }</version>
  137. </dependency>
  138. `,
  139. },
  140. {
  141. language: 'xml',
  142. partialLoading: sourcePackageRegistries?.isLoading,
  143. description: t(
  144. 'To upload your source code to Sentry so it can be shown in stack traces, use our Maven plugin.'
  145. ),
  146. code: `
  147. <build>
  148. <plugins>
  149. <plugin>
  150. <groupId>io.sentry</groupId>
  151. <artifactId>sentry-maven-plugin</artifactId>
  152. <version>${
  153. sourcePackageRegistries?.isLoading
  154. ? t('\u2026loading')
  155. : sourcePackageRegistries?.data?.['sentry.java.mavenplugin']?.version ?? '0.0.4'
  156. }</version>
  157. <configuration>
  158. <!-- for showing output of sentry-cli -->
  159. <debugSentryCli>true</debugSentryCli>
  160. <org>${organizationSlug}</org>
  161. <project>${projectSlug}</project>
  162. <!-- in case you're self hosting, provide the URL here -->
  163. <!--<url>http://localhost:8000/</url>-->
  164. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  165. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  166. </configuration>
  167. <executions>
  168. <execution>
  169. <phase>generate-resources</phase>
  170. <goals>
  171. <goal>uploadSourceBundle</goal>
  172. </goals>
  173. </execution>
  174. </executions>
  175. </plugin>
  176. </plugins>
  177. ...
  178. </build>
  179. `,
  180. },
  181. ]
  182. : []),
  183. ],
  184. },
  185. {
  186. type: StepType.CONFIGURE,
  187. description: t(
  188. "Configure Sentry as soon as possible in your application's lifecycle:"
  189. ),
  190. configurations: [
  191. {
  192. language: 'xml',
  193. description: t(
  194. '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.'
  195. ),
  196. code: `
  197. <configuration>
  198. <!-- Configure the Console appender -->
  199. <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
  200. <encoder>
  201. <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
  202. </encoder>
  203. </appender>
  204. <!-- Configure the Sentry appender, overriding the logging threshold to the WARN level -->
  205. <appender name="Sentry" class="io.sentry.logback.SentryAppender">
  206. <options>
  207. <dsn>${dsn}</dsn>
  208. </options>
  209. </appender>
  210. <!-- Enable the Console and Sentry appenders, Console is provided as an example
  211. of a non-Sentry logger that is set to a different logging threshold -->
  212. <root level="INFO">
  213. <appender-ref ref="Console" />
  214. <appender-ref ref="Sentry" />
  215. </root>
  216. </configuration>
  217. `,
  218. additionalInfo: (
  219. <p>
  220. {tct(
  221. "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].",
  222. {
  223. code: <code />,
  224. link: (
  225. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/logback/#dsn-configuration/" />
  226. ),
  227. }
  228. )}
  229. </p>
  230. ),
  231. },
  232. {
  233. description: (
  234. <p>
  235. {tct(
  236. "Next, you'll need to set your log levels, as illustrated here. You can learn more about [link:configuring log levels] in our documentation.",
  237. {
  238. link: (
  239. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/logback/#minimum-log-level/" />
  240. ),
  241. }
  242. )}
  243. </p>
  244. ),
  245. configurations: [
  246. {
  247. language: 'xml',
  248. code: `
  249. <appender name="Sentry" class="io.sentry.logback.SentryAppender">
  250. <options>
  251. <dsn>${dsn}</dsn>
  252. </options>
  253. <!-- Optionally change minimum Event level. Default for Events is ERROR -->
  254. <minimumEventLevel>WARN</minimumEventLevel>
  255. <!-- Optionally change minimum Breadcrumbs level. Default for Breadcrumbs is INFO -->
  256. <minimumBreadcrumbLevel>DEBUG</minimumBreadcrumbLevel>
  257. </appender>
  258. `,
  259. },
  260. ],
  261. },
  262. ],
  263. },
  264. {
  265. type: StepType.VERIFY,
  266. description: t(
  267. 'Last, create an intentional error, so you can test that everything is working:'
  268. ),
  269. configurations: [
  270. {
  271. language: 'java',
  272. code: [
  273. {
  274. language: 'java',
  275. label: 'Java',
  276. value: 'java',
  277. code: `
  278. import java.lang.Exception;
  279. import io.sentry.Sentry;
  280. try {
  281. throw new Exception("This is a test.");
  282. } catch (Exception e) {
  283. Sentry.captureException(e);
  284. }`,
  285. },
  286. {
  287. language: 'java',
  288. label: 'Kotlin',
  289. value: 'kotlin',
  290. code: `
  291. import java.lang.Exception
  292. import io.sentry.Sentry
  293. try {
  294. throw Exception("This is a test.")
  295. } catch (e: Exception) {
  296. Sentry.captureException(e)
  297. }`,
  298. },
  299. ],
  300. },
  301. ],
  302. additionalInfo: (
  303. <Fragment>
  304. <p>
  305. {t(
  306. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  307. )}
  308. </p>
  309. <p>
  310. {t(
  311. "If you're an existing user and have disabled alerts, you won't receive this email."
  312. )}
  313. </p>
  314. </Fragment>
  315. ),
  316. },
  317. {
  318. title: t('Other build tools'),
  319. additionalInfo: (
  320. <p>
  321. {tct('For other dependency managers see the [link:central Maven repository].', {
  322. link: (
  323. <ExternalLink href="https://search.maven.org/artifact/io.sentry/sentry-logback" />
  324. ),
  325. })}
  326. </p>
  327. ),
  328. },
  329. ];
  330. export const nextSteps = [
  331. {
  332. id: 'examples',
  333. name: t('Examples'),
  334. description: t('Check out our sample applications.'),
  335. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  336. },
  337. ];
  338. // Configuration End
  339. export function GettingStartedWithLogBack({
  340. dsn,
  341. sourcePackageRegistries,
  342. projectSlug,
  343. organization,
  344. ...props
  345. }: ModuleProps) {
  346. const optionValues = useUrlPlatformOptions(platformOptions);
  347. const nextStepDocs = [...nextSteps];
  348. return (
  349. <Layout
  350. steps={steps({
  351. dsn,
  352. sourcePackageRegistries,
  353. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  354. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  355. packageManager: optionValues.packageManager as PackageManager,
  356. })}
  357. nextSteps={nextStepDocs}
  358. introduction={introduction}
  359. platformOptions={platformOptions}
  360. projectSlug={projectSlug}
  361. {...props}
  362. />
  363. );
  364. }
  365. export default GettingStartedWithLogBack;