Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 1x 1x 19x 19x 19x 19x 19x 19x 20x 16x 1x 15x 15x 15x 8x 7x 8x 7x 5x 2x 7x 5x 1x 4x 2x 7x 7x 2x 7x 6x 6x 1x 6x 1x 6x 9x 3x 4x 2x 1x | import { LogEntry, LogProvider, LogLevel } from '../log-provider.interface';
import * as Sentry from '@sentry/angular';
/**
* Configuration options for Sentry log provider
*/
export interface SentryLogProviderOptions {
/**
* Minimum log level to send to Sentry
* @default 'warn'
*/
minLevel?: LogLevel;
/**
* Whether to add info/debug logs as breadcrumbs
* @default true
*/
addBreadcrumbs?: boolean;
}
/**
* Sentry log provider
* Sends error/warning logs to Sentry and adds breadcrumbs for lower levels
*
* This provider works in conjunction with Sentry.init() in main.ts
* which handles global error catching and performance monitoring.
*
* @example
* ```typescript
* // In app.config.ts
* provideLogProviders([
* createIndexedDBLogProvider(),
* createSentryLogProvider({ minLevel: 'warn' }),
* ])
* ```
*/
export class SentryLogProvider implements LogProvider {
readonly name = 'sentry';
private readonly minLevel: LogLevel;
private readonly addBreadcrumbs: boolean;
private readonly levelPriority: Record<LogLevel, number> = {
debug: 0,
info: 1,
warn: 2,
error: 3,
};
constructor(
private readonly isProduction: boolean,
private readonly isBrowser: boolean,
options?: SentryLogProviderOptions
) {
this.minLevel = options?.minLevel ?? 'warn';
this.addBreadcrumbs = options?.addBreadcrumbs ?? true;
}
get isAvailable(): boolean {
// Only available in browser and when Sentry is initialized
return this.isBrowser && this.isProduction;
}
log(entry: LogEntry): void {
if (!this.isAvailable) {
return;
}
const entryPriority = this.levelPriority[entry.level];
const minPriority = this.levelPriority[this.minLevel];
// Add breadcrumb for logs below minimum level (if enabled)
if (entryPriority < minPriority) {
if (this.addBreadcrumbs) {
this.addBreadcrumb(entry);
}
return;
}
// Send to Sentry based on level
if (entry.level === 'error') {
this.captureError(entry);
} else {
this.captureMessage(entry);
}
}
/**
* Add a breadcrumb for context (for debug/info logs)
*/
private addBreadcrumb(entry: LogEntry): void {
Sentry.addBreadcrumb({
category: entry.context ?? 'app',
message: entry.message,
level: this.mapToSentryLevel(entry.level),
data: entry.data as Record<string, unknown> | undefined,
timestamp: entry.timestamp.getTime() / 1000,
});
}
/**
* Capture error-level logs
*/
private captureError(entry: LogEntry): void {
// If data contains an Error instance, capture it as exception
if (entry.data instanceof Error) {
Sentry.captureException(entry.data, {
tags: this.buildTags(entry),
extra: {
message: entry.message,
url: entry.url,
},
});
} else {
Sentry.captureMessage(entry.message, {
level: 'error',
tags: this.buildTags(entry),
extra: this.buildExtra(entry),
});
}
}
/**
* Capture warning-level logs as messages
*/
private captureMessage(entry: LogEntry): void {
Sentry.captureMessage(entry.message, {
level: this.mapToSentryLevel(entry.level),
tags: this.buildTags(entry),
extra: this.buildExtra(entry),
});
}
/**
* Build tags for Sentry event
*/
private buildTags(entry: LogEntry): Record<string, string> {
const tags: Record<string, string> = {};
if (entry.context) {
tags['log.context'] = entry.context;
}
return tags;
}
/**
* Build extra data for Sentry event
*/
private buildExtra(entry: LogEntry): Record<string, unknown> {
const extra: Record<string, unknown> = {};
if (entry.data !== undefined) {
extra['data'] = entry.data;
}
if (entry.url) {
extra['pageUrl'] = entry.url;
}
return extra;
}
/**
* Map LogLevel to Sentry severity level
*/
private mapToSentryLevel(level: LogLevel): 'debug' | 'info' | 'warning' | 'error' {
switch (level) {
case 'debug':
return 'debug';
case 'info':
return 'info';
case 'warn':
return 'warning';
case 'error':
return 'error';
}
}
}
/**
* Factory function to create a SentryLogProvider
* Use this in provideLogProviders()
*
* @param isProduction - Whether the app is running in production mode
* @param isBrowser - Whether the app is running in browser
* @param options - Optional configuration
*
*/
export function createSentryLogProvider(
isProduction: boolean,
isBrowser: boolean,
options?: SentryLogProviderOptions
): SentryLogProvider {
return new SentryLogProvider(isProduction, isBrowser, options);
}
|