From 0f8cde9449d1d21cc52111b91673bcb532c8926d Mon Sep 17 00:00:00 2001 From: David Friedel Date: Fri, 26 Dec 2025 11:32:00 +0000 Subject: [PATCH] Add logMessage, getBreadcrumbs, and enableDebugLogging features MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add logMessage(level, title, message?, data?) for structured logging with title - Add getBreadcrumbs() public method to retrieve current breadcrumbs - Add clearBreadcrumbs() method to clear all breadcrumbs - Add enableDebugLogging global flag and setDebugLogging() function - Update all debug logging to respect the global debug flag 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/client.ts | 61 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index 97661ad..757502a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -16,6 +16,20 @@ import { OfflineQueue } from './queue'; import { BreadcrumbManager } from './breadcrumbs'; import { Journey, JourneyScope, Step, StepScope } from './journey'; +/** + * Global debug logging flag. When enabled, all IronTelemetry clients + * will output debug information to the console. + */ +export let enableDebugLogging = false; + +/** + * Enable or disable global debug logging for all TelemetryClient instances. + * @param enabled Whether to enable debug logging + */ +export function setDebugLogging(enabled: boolean): void { + enableDebugLogging = enabled; +} + /** * Main IronTelemetry client class */ @@ -46,7 +60,7 @@ export class TelemetryClient { this.flushInterval = setInterval(() => this.processQueue(), 30000); } - if (this.options.debug) { + if (this.options.debug || enableDebugLogging) { console.log('[IronTelemetry] Initialized with DSN:', this.options.dsn); } } @@ -73,6 +87,32 @@ export class TelemetryClient { return this.sendEvent(event); } + /** + * Log a structured message with title, message, and optional data. + * Useful for structured logging that differentiates the log title from its details. + * @param level The severity level of the log + * @param title A short, descriptive title for the log entry + * @param message Optional detailed message + * @param data Optional additional data to attach to the log + */ + async logMessage( + level: SeverityLevel, + title: string, + message?: string, + data?: Record + ): Promise { + const fullMessage = message ? `${title}: ${message}` : title; + const event = this.createEvent(level, fullMessage); + + if (data) { + event.extra = { ...event.extra, logTitle: title, logData: data }; + } else { + event.extra = { ...event.extra, logTitle: title }; + } + + return this.sendEvent(event); + } + /** * Add a breadcrumb */ @@ -96,6 +136,21 @@ export class TelemetryClient { } } + /** + * Get a copy of the current breadcrumbs list. + * @returns A read-only array of breadcrumbs + */ + getBreadcrumbs(): ReadonlyArray { + return this.breadcrumbs.getAll(); + } + + /** + * Clear all breadcrumbs. + */ + clearBreadcrumbs(): void { + this.breadcrumbs.clear(); + } + /** * Set user context */ @@ -201,7 +256,7 @@ export class TelemetryClient { private async sendEvent(event: TelemetryEvent): Promise { // Check sample rate if (Math.random() > this.options.sampleRate) { - if (this.options.debug) { + if (this.options.debug || enableDebugLogging) { console.log('[IronTelemetry] Event dropped due to sample rate'); } return { success: true, eventId: event.eventId }; @@ -210,7 +265,7 @@ export class TelemetryClient { // Apply beforeSend hook const beforeSendResult = this.options.beforeSend(event); if (beforeSendResult === false) { - if (this.options.debug) { + if (this.options.debug || enableDebugLogging) { console.log('[IronTelemetry] Event dropped by beforeSend hook'); } return { success: true, eventId: event.eventId };