Add logMessage, getBreadcrumbs, and enableDebugLogging features
- 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 <noreply@anthropic.com>
This commit is contained in:
parent
f2bed60e85
commit
0f8cde9449
|
|
@ -16,6 +16,20 @@ import { OfflineQueue } from './queue';
|
||||||
import { BreadcrumbManager } from './breadcrumbs';
|
import { BreadcrumbManager } from './breadcrumbs';
|
||||||
import { Journey, JourneyScope, Step, StepScope } from './journey';
|
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
|
* Main IronTelemetry client class
|
||||||
*/
|
*/
|
||||||
|
|
@ -46,7 +60,7 @@ export class TelemetryClient {
|
||||||
this.flushInterval = setInterval(() => this.processQueue(), 30000);
|
this.flushInterval = setInterval(() => this.processQueue(), 30000);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.debug) {
|
if (this.options.debug || enableDebugLogging) {
|
||||||
console.log('[IronTelemetry] Initialized with DSN:', this.options.dsn);
|
console.log('[IronTelemetry] Initialized with DSN:', this.options.dsn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,6 +87,32 @@ export class TelemetryClient {
|
||||||
return this.sendEvent(event);
|
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<string, unknown>
|
||||||
|
): Promise<SendResult> {
|
||||||
|
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
|
* 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<Breadcrumb> {
|
||||||
|
return this.breadcrumbs.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear all breadcrumbs.
|
||||||
|
*/
|
||||||
|
clearBreadcrumbs(): void {
|
||||||
|
this.breadcrumbs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set user context
|
* Set user context
|
||||||
*/
|
*/
|
||||||
|
|
@ -201,7 +256,7 @@ export class TelemetryClient {
|
||||||
private async sendEvent(event: TelemetryEvent): Promise<SendResult> {
|
private async sendEvent(event: TelemetryEvent): Promise<SendResult> {
|
||||||
// Check sample rate
|
// Check sample rate
|
||||||
if (Math.random() > this.options.sampleRate) {
|
if (Math.random() > this.options.sampleRate) {
|
||||||
if (this.options.debug) {
|
if (this.options.debug || enableDebugLogging) {
|
||||||
console.log('[IronTelemetry] Event dropped due to sample rate');
|
console.log('[IronTelemetry] Event dropped due to sample rate');
|
||||||
}
|
}
|
||||||
return { success: true, eventId: event.eventId };
|
return { success: true, eventId: event.eventId };
|
||||||
|
|
@ -210,7 +265,7 @@ export class TelemetryClient {
|
||||||
// Apply beforeSend hook
|
// Apply beforeSend hook
|
||||||
const beforeSendResult = this.options.beforeSend(event);
|
const beforeSendResult = this.options.beforeSend(event);
|
||||||
if (beforeSendResult === false) {
|
if (beforeSendResult === false) {
|
||||||
if (this.options.debug) {
|
if (this.options.debug || enableDebugLogging) {
|
||||||
console.log('[IronTelemetry] Event dropped by beforeSend hook');
|
console.log('[IronTelemetry] Event dropped by beforeSend hook');
|
||||||
}
|
}
|
||||||
return { success: true, eventId: event.eventId };
|
return { success: true, eventId: event.eventId };
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue