🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

Enhancing Error Logging in NestJS with a Custom Sentry Logger

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Effective error logging is vital for application stability. Integrating Sentry into a NestJS application can significantly improve how we handle logging. This article will guide you through creating a custom logger that sends error logs to Sentry while keeping the standard console logging.






Step 1: Install Sentry



First, install the Sentry SDK:




CODE
npm install @sentry/node









Step 2: Create the Custom Logger



Create a new file named sentry.logger.ts:




CODE
import { ConsoleLogger } from "@nestjs/common";
import * as Sentry from "@sentry/node";

export class SentryLogger extends ConsoleLogger {
error(message: any, ...optionalParams: any[]): void {
const errorMessage = message.toString();
let stack: string | object = "";
let logContext = "";

if (optionalParams.length === 1) {
logContext = optionalParams[0];
} else if (optionalParams.length === 2) {
[stack, logContext] = optionalParams;
}

const formattedMessage = logContext
? `${logContext}: ${errorMessage}`
: errorMessage;

Sentry.withScope((scope) => {
scope.setExtra("message", errorMessage);
scope.setExtra("context", logContext);
scope.setExtra("stack", stack);
Sentry.captureMessage(formattedMessage, "error");
});

super.error(errorMessage, ...optionalParams);
}

verbose(message: any, ...optionalParams: any[]): void {
const verboseMessage = message ? message.toString() : "";
const logContext = optionalParams.shift() || "";
const extra = optionalParams;

const formattedMessage = logContext
? `${logContext}: ${verboseMessage}`
: verboseMessage;

Sentry.withScope((scope) => {
scope.setExtra("message", verboseMessage);
scope.setExtra("context", logContext);
scope.setExtra("extra", extra);
Sentry.captureMessage(formattedMessage, "info");
});

super.verbose(verboseMessage, ...extra);
}
}









Explanation





  • Error Handling: The error method formats the error message and sends it to Sentry, attaching extra context.


  • Verbose Logging: The verbose method captures informational messages, providing insights into the application's state.






Step 3: Initialize Sentry



In your main application file (e.g., main.ts), initialize Sentry with the following configuration:




CODE
import { Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { NestExpressApplication } from "@nestjs/platform-express";
import * as Sentry from "@sentry/node";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
import { AppModule } from "./app.module";
import { SentryLogger } from "./utility/logger/sentry.logger";

const logger = new Logger("MyApp");

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
const cfg = app.get(ConfigService);

Sentry.init({
dsn: cfg.get<string>("SENTRY_DNS", ""),
environment: cfg.get<string>("NODE_ENV", "development"),
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
normalizeDepth: 5,
integrations: [nodeProfilingIntegration()],
});

app.useLogger(new SentryLogger());

await app.listen(3000);
}

bootstrap()
.then(() => logger.log("Server is running"))
.catch((err) => logger.error("Bootstrap failed", err));









Explanation of Sentry Initialization





  • DSN: The Data Source Name (DSN) allows Sentry to identify and route errors to the correct project.


  • Environment: Setting the environment (e.g., development or production) helps in distinguishing logs.


  • Sample Rates: Both tracesSampleRate and profilesSampleRate control how much data is sent to Sentry for performance monitoring, set to 100% (1.0) for full data capture.


  • Integrations: The nodeProfilingIntegration() is included for additional profiling features.



By implementing a custom logger with Sentry in your NestJS application, you enhance your error tracking and debugging capabilities. With detailed context and real-time monitoring, you can maintain more reliable applications. Start integrating Sentry into your projects today!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Enhancing Error Logging in NestJS with a Custom Sentry Logger

Thematisch verwandte Begriffe: Enhancing, Error, Logging, NestJS · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...