Browse Source

feat(sentry-plugin): Add option to capture logs

David Höck 3 months ago
parent
commit
77777217e2
1 changed files with 18 additions and 1 deletions
  1. 18 1
      packages/sentry-plugin/instrument.ts

+ 18 - 1
packages/sentry-plugin/instrument.ts

@@ -1,3 +1,4 @@
+import type { ConsoleLevel } from '@sentry/core';
 import * as Sentry from '@sentry/nestjs';
 import { nodeProfilingIntegration } from '@sentry/profiling-node';
 
@@ -8,6 +9,10 @@ const SENTRY_TRACES_SAMPLE_RATE = process.env.SENTRY_TRACES_SAMPLE_RATE
 const SENTRY_PROFILES_SAMPLE_RATE = process.env.SENTRY_PROFILES_SAMPLE_RATE
     ? Number(process.env.SENTRY_PROFILES_SAMPLE_RATE)
     : undefined;
+const SENTRY_ENABLE_LOGS = process.env.SENTRY_ENABLE_LOGS === 'true';
+const SENTRY_CAPTURE_LOG_LEVELS = process.env.SENTRY_CAPTURE_LOG_LEVELS
+    ? process.env.SENTRY_CAPTURE_LOG_LEVELS.split(',').map(l => l.trim())
+    : ['log', 'warn', 'error'];
 
 if (!SENTRY_DSN) {
     throw new Error('SENTRY_DSN is not set');
@@ -15,7 +20,12 @@ if (!SENTRY_DSN) {
 
 Sentry.init({
     dsn: SENTRY_DSN,
-    integrations: [nodeProfilingIntegration()],
+    integrations: [
+        nodeProfilingIntegration(),
+        ...(SENTRY_ENABLE_LOGS
+            ? [Sentry.consoleLoggingIntegration({ levels: SENTRY_CAPTURE_LOG_LEVELS as ConsoleLevel[] })]
+            : []),
+    ],
     /**
      * @description
      * The sample rate for tracing. Value should be between 0 and 1.
@@ -30,4 +40,11 @@ Sentry.init({
      * @default undefined
      */
     profilesSampleRate: SENTRY_PROFILES_SAMPLE_RATE,
+
+    /**
+     * @description
+     * Enable logs to be sent to Sentry.
+     * @default false
+     */
+    enableLogs: SENTRY_ENABLE_LOGS,
 });