logback.tsx 11 KB

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