Skip to content

Commit ccbf627

Browse files
authored
feat(core): Introduce debug to replace logger (#16906)
resolves #16903 ref #16901 This PR introduces a `debug` function that aims to replace `logger`. This gives us a couple of benefits: 1. communicates intent of the export much better 1. avoids collision with `logger` exported from other sdks (which is used for sentry structured logging) 1. we can move structured logging `logger` code into `@sentry/core`, reducing duplication across our sdks We don't deprecate `logger` just yet, we need to convert everything else in the other SDKs first.
1 parent 6a245a0 commit ccbf627

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+233
-152
lines changed

packages/core/src/client.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { dsnToString, makeDsn } from './utils/dsn';
3737
import { addItemToEnvelope, createAttachmentEnvelopeItem } from './utils/envelope';
3838
import { getPossibleEventMessages } from './utils/eventUtils';
3939
import { isParameterizedString, isPlainObject, isPrimitive, isThenable } from './utils/is';
40-
import { logger } from './utils/logger';
40+
import { debug } from './utils/logger';
4141
import { merge } from './utils/merge';
4242
import { checkOrSetAlreadyCaught, uuid4 } from './utils/misc';
4343
import { parseSampleRate } from './utils/parseSampleRate';
@@ -154,7 +154,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
154154
if (options.dsn) {
155155
this._dsn = makeDsn(options.dsn);
156156
} else {
157-
DEBUG_BUILD && logger.warn('No DSN provided, client will not send events.');
157+
DEBUG_BUILD && debug.warn('No DSN provided, client will not send events.');
158158
}
159159

160160
if (this._dsn) {
@@ -182,7 +182,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
182182

183183
// ensure we haven't captured this very object before
184184
if (checkOrSetAlreadyCaught(exception)) {
185-
DEBUG_BUILD && logger.log(ALREADY_SEEN_ERROR);
185+
DEBUG_BUILD && debug.log(ALREADY_SEEN_ERROR);
186186
return eventId;
187187
}
188188

@@ -237,7 +237,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
237237

238238
// ensure we haven't captured this very object before
239239
if (hint?.originalException && checkOrSetAlreadyCaught(hint.originalException)) {
240-
DEBUG_BUILD && logger.log(ALREADY_SEEN_ERROR);
240+
DEBUG_BUILD && debug.log(ALREADY_SEEN_ERROR);
241241
return eventId;
242242
}
243243

@@ -429,15 +429,15 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
429429
if ('aggregates' in session) {
430430
const sessionAttrs = session.attrs || {};
431431
if (!sessionAttrs.release && !clientReleaseOption) {
432-
DEBUG_BUILD && logger.warn(MISSING_RELEASE_FOR_SESSION_ERROR);
432+
DEBUG_BUILD && debug.warn(MISSING_RELEASE_FOR_SESSION_ERROR);
433433
return;
434434
}
435435
sessionAttrs.release = sessionAttrs.release || clientReleaseOption;
436436
sessionAttrs.environment = sessionAttrs.environment || clientEnvironmentOption;
437437
session.attrs = sessionAttrs;
438438
} else {
439439
if (!session.release && !clientReleaseOption) {
440-
DEBUG_BUILD && logger.warn(MISSING_RELEASE_FOR_SESSION_ERROR);
440+
DEBUG_BUILD && debug.warn(MISSING_RELEASE_FOR_SESSION_ERROR);
441441
return;
442442
}
443443
session.release = session.release || clientReleaseOption;
@@ -465,7 +465,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
465465
// would be `Partial<Record<SentryRequestType, Partial<Record<Outcome, number>>>>`
466466
// With typescript 4.1 we could even use template literal types
467467
const key = `${reason}:${category}`;
468-
DEBUG_BUILD && logger.log(`Recording outcome: "${key}"${count > 1 ? ` (${count} times)` : ''}`);
468+
DEBUG_BUILD && debug.log(`Recording outcome: "${key}"${count > 1 ? ` (${count} times)` : ''}`);
469469
this._outcomes[key] = (this._outcomes[key] || 0) + count;
470470
}
471471
}
@@ -866,12 +866,12 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
866866

867867
if (this._isEnabled() && this._transport) {
868868
return this._transport.send(envelope).then(null, reason => {
869-
DEBUG_BUILD && logger.error('Error while sending envelope:', reason);
869+
DEBUG_BUILD && debug.error('Error while sending envelope:', reason);
870870
return reason;
871871
});
872872
}
873873

874-
DEBUG_BUILD && logger.error('Transport disabled');
874+
DEBUG_BUILD && debug.error('Transport disabled');
875875

876876
return resolvedSyncPromise({});
877877
}
@@ -1021,7 +1021,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
10211021
isolationScope = getIsolationScope(),
10221022
): PromiseLike<string | undefined> {
10231023
if (DEBUG_BUILD && isErrorEvent(event)) {
1024-
logger.log(`Captured error event \`${getPossibleEventMessages(event)[0] || '<unknown>'}\``);
1024+
debug.log(`Captured error event \`${getPossibleEventMessages(event)[0] || '<unknown>'}\``);
10251025
}
10261026

10271027
return this._processEvent(event, hint, currentScope, isolationScope).then(
@@ -1031,11 +1031,11 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
10311031
reason => {
10321032
if (DEBUG_BUILD) {
10331033
if (_isDoNotSendEventError(reason)) {
1034-
logger.log(reason.message);
1034+
debug.log(reason.message);
10351035
} else if (_isInternalError(reason)) {
1036-
logger.warn(reason.message);
1036+
debug.warn(reason.message);
10371037
} else {
1038-
logger.warn(reason);
1038+
debug.warn(reason);
10391039
}
10401040
}
10411041
return undefined;
@@ -1196,22 +1196,22 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
11961196
* Sends client reports as an envelope.
11971197
*/
11981198
protected _flushOutcomes(): void {
1199-
DEBUG_BUILD && logger.log('Flushing outcomes...');
1199+
DEBUG_BUILD && debug.log('Flushing outcomes...');
12001200

12011201
const outcomes = this._clearOutcomes();
12021202

12031203
if (outcomes.length === 0) {
1204-
DEBUG_BUILD && logger.log('No outcomes to send');
1204+
DEBUG_BUILD && debug.log('No outcomes to send');
12051205
return;
12061206
}
12071207

12081208
// This is really the only place where we want to check for a DSN and only send outcomes then
12091209
if (!this._dsn) {
1210-
DEBUG_BUILD && logger.log('No dsn provided, will not send outcomes');
1210+
DEBUG_BUILD && debug.log('No dsn provided, will not send outcomes');
12111211
return;
12121212
}
12131213

1214-
DEBUG_BUILD && logger.log('Sending outcomes:', outcomes);
1214+
DEBUG_BUILD && debug.log('Sending outcomes:', outcomes);
12151215

12161216
const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn));
12171217

packages/core/src/eventProcessors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DEBUG_BUILD } from './debug-build';
22
import type { Event, EventHint } from './types-hoist/event';
33
import type { EventProcessor } from './types-hoist/eventprocessor';
44
import { isThenable } from './utils/is';
5-
import { logger } from './utils/logger';
5+
import { debug } from './utils/logger';
66
import { SyncPromise } from './utils/syncpromise';
77

88
/**
@@ -21,7 +21,7 @@ export function notifyEventProcessors(
2121
} else {
2222
const result = processor({ ...event }, hint) as Event | null;
2323

24-
DEBUG_BUILD && processor.id && result === null && logger.log(`Event processor "${processor.id}" dropped event`);
24+
DEBUG_BUILD && processor.id && result === null && debug.log(`Event processor "${processor.id}" dropped event`);
2525

2626
if (isThenable(result)) {
2727
void result

packages/core/src/exports.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { Session, SessionContext } from './types-hoist/session';
1111
import type { SeverityLevel } from './types-hoist/severity';
1212
import type { User } from './types-hoist/user';
1313
import { isThenable } from './utils/is';
14-
import { logger } from './utils/logger';
14+
import { debug } from './utils/logger';
1515
import { uuid4 } from './utils/misc';
1616
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
1717
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
@@ -136,9 +136,9 @@ export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorCo
136136
const scope = getCurrentScope();
137137
const client = getClient();
138138
if (!client) {
139-
DEBUG_BUILD && logger.warn('Cannot capture check-in. No client defined.');
139+
DEBUG_BUILD && debug.warn('Cannot capture check-in. No client defined.');
140140
} else if (!client.captureCheckIn) {
141-
DEBUG_BUILD && logger.warn('Cannot capture check-in. Client does not support sending check-ins.');
141+
DEBUG_BUILD && debug.warn('Cannot capture check-in. Client does not support sending check-ins.');
142142
} else {
143143
return client.captureCheckIn(checkIn, upsertMonitorConfig, scope);
144144
}
@@ -206,7 +206,7 @@ export async function flush(timeout?: number): Promise<boolean> {
206206
if (client) {
207207
return client.flush(timeout);
208208
}
209-
DEBUG_BUILD && logger.warn('Cannot flush events. No client defined.');
209+
DEBUG_BUILD && debug.warn('Cannot flush events. No client defined.');
210210
return Promise.resolve(false);
211211
}
212212

@@ -223,7 +223,7 @@ export async function close(timeout?: number): Promise<boolean> {
223223
if (client) {
224224
return client.close(timeout);
225225
}
226-
DEBUG_BUILD && logger.warn('Cannot flush events and disable SDK. No client defined.');
226+
DEBUG_BUILD && debug.warn('Cannot flush events and disable SDK. No client defined.');
227227
return Promise.resolve(false);
228228
}
229229

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export {
165165
isVueViewModel,
166166
} from './utils/is';
167167
export { isBrowser } from './utils/isBrowser';
168-
export { CONSOLE_LEVELS, consoleSandbox, logger, originalConsoleMethods } from './utils/logger';
168+
export { CONSOLE_LEVELS, consoleSandbox, debug, logger, originalConsoleMethods } from './utils/logger';
169169
export type { Logger } from './utils/logger';
170170
export {
171171
addContextToFrame,

packages/core/src/instrument/handlers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DEBUG_BUILD } from '../debug-build';
2-
import { logger } from '../utils/logger';
2+
import { debug } from '../utils/logger';
33
import { getFunctionName } from '../utils/stacktrace';
44

55
export type InstrumentHandlerType =
@@ -41,7 +41,7 @@ export function maybeInstrument(type: InstrumentHandlerType, instrumentFn: () =>
4141
try {
4242
instrumentFn();
4343
} catch (e) {
44-
DEBUG_BUILD && logger.error(`Error while instrumenting ${type}`, e);
44+
DEBUG_BUILD && debug.error(`Error while instrumenting ${type}`, e);
4545
}
4646
}
4747
}
@@ -58,7 +58,7 @@ export function triggerHandlers(type: InstrumentHandlerType, data: unknown): voi
5858
handler(data);
5959
} catch (e) {
6060
DEBUG_BUILD &&
61-
logger.error(
61+
debug.error(
6262
`Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(handler)}\nError:`,
6363
e,
6464
);

packages/core/src/integration.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DEBUG_BUILD } from './debug-build';
44
import type { Event, EventHint } from './types-hoist/event';
55
import type { Integration, IntegrationFn } from './types-hoist/integration';
66
import type { Options } from './types-hoist/options';
7-
import { logger } from './utils/logger';
7+
import { debug } from './utils/logger';
88

99
export const installedIntegrations: string[] = [];
1010

@@ -99,7 +99,7 @@ export function afterSetupIntegrations(client: Client, integrations: Integration
9999
/** Setup a single integration. */
100100
export function setupIntegration(client: Client, integration: Integration, integrationIndex: IntegrationIndex): void {
101101
if (integrationIndex[integration.name]) {
102-
DEBUG_BUILD && logger.log(`Integration skipped because it was already installed: ${integration.name}`);
102+
DEBUG_BUILD && debug.log(`Integration skipped because it was already installed: ${integration.name}`);
103103
return;
104104
}
105105
integrationIndex[integration.name] = integration;
@@ -130,15 +130,15 @@ export function setupIntegration(client: Client, integration: Integration, integ
130130
client.addEventProcessor(processor);
131131
}
132132

133-
DEBUG_BUILD && logger.log(`Integration installed: ${integration.name}`);
133+
DEBUG_BUILD && debug.log(`Integration installed: ${integration.name}`);
134134
}
135135

136136
/** Add an integration to the current scope's client. */
137137
export function addIntegration(integration: Integration): void {
138138
const client = getClient();
139139

140140
if (!client) {
141-
DEBUG_BUILD && logger.warn(`Cannot add integration "${integration.name}" because no SDK Client is available.`);
141+
DEBUG_BUILD && debug.warn(`Cannot add integration "${integration.name}" because no SDK Client is available.`);
142142
return;
143143
}
144144

packages/core/src/integrations/dedupe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Event } from '../types-hoist/event';
44
import type { Exception } from '../types-hoist/exception';
55
import type { IntegrationFn } from '../types-hoist/integration';
66
import type { StackFrame } from '../types-hoist/stackframe';
7-
import { logger } from '../utils/logger';
7+
import { debug } from '../utils/logger';
88
import { getFramesFromEvent } from '../utils/stacktrace';
99

1010
const INTEGRATION_NAME = 'Dedupe';
@@ -24,7 +24,7 @@ const _dedupeIntegration = (() => {
2424
// Juuust in case something goes wrong
2525
try {
2626
if (_shouldDropEvent(currentEvent, previousEvent)) {
27-
DEBUG_BUILD && logger.warn('Event dropped due to being a duplicate of previously captured event.');
27+
DEBUG_BUILD && debug.warn('Event dropped due to being a duplicate of previously captured event.');
2828
return null;
2929
}
3030
} catch (_oO) {} // eslint-disable-line no-empty

packages/core/src/integrations/eventFilters.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Event } from '../types-hoist/event';
44
import type { IntegrationFn } from '../types-hoist/integration';
55
import type { StackFrame } from '../types-hoist/stackframe';
66
import { getPossibleEventMessages } from '../utils/eventUtils';
7-
import { logger } from '../utils/logger';
7+
import { debug } from '../utils/logger';
88
import { getEventDescription } from '../utils/misc';
99
import { stringMatchesSomePattern } from '../utils/string';
1010

@@ -111,14 +111,14 @@ function _shouldDropEvent(event: Event, options: Partial<EventFiltersOptions>):
111111
// Filter errors
112112
if (_isIgnoredError(event, options.ignoreErrors)) {
113113
DEBUG_BUILD &&
114-
logger.warn(
114+
debug.warn(
115115
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
116116
);
117117
return true;
118118
}
119119
if (_isUselessError(event)) {
120120
DEBUG_BUILD &&
121-
logger.warn(
121+
debug.warn(
122122
`Event dropped due to not having an error message, error type or stacktrace.\nEvent: ${getEventDescription(
123123
event,
124124
)}`,
@@ -127,7 +127,7 @@ function _shouldDropEvent(event: Event, options: Partial<EventFiltersOptions>):
127127
}
128128
if (_isDeniedUrl(event, options.denyUrls)) {
129129
DEBUG_BUILD &&
130-
logger.warn(
130+
debug.warn(
131131
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
132132
event,
133133
)}.\nUrl: ${_getEventFilterUrl(event)}`,
@@ -136,7 +136,7 @@ function _shouldDropEvent(event: Event, options: Partial<EventFiltersOptions>):
136136
}
137137
if (!_isAllowedUrl(event, options.allowUrls)) {
138138
DEBUG_BUILD &&
139-
logger.warn(
139+
debug.warn(
140140
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
141141
event,
142142
)}.\nUrl: ${_getEventFilterUrl(event)}`,
@@ -148,7 +148,7 @@ function _shouldDropEvent(event: Event, options: Partial<EventFiltersOptions>):
148148

149149
if (_isIgnoredTransaction(event, options.ignoreTransactions)) {
150150
DEBUG_BUILD &&
151-
logger.warn(
151+
debug.warn(
152152
`Event dropped due to being matched by \`ignoreTransactions\` option.\nEvent: ${getEventDescription(event)}`,
153153
);
154154
return true;
@@ -212,7 +212,7 @@ function _getEventFilterUrl(event: Event): string | null {
212212
const frames = rootException?.stacktrace?.frames;
213213
return frames ? _getLastValidUrl(frames) : null;
214214
} catch (oO) {
215-
DEBUG_BUILD && logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
215+
DEBUG_BUILD && debug.error(`Cannot extract url for event ${getEventDescription(event)}`);
216216
return null;
217217
}
218218
}

packages/core/src/integrations/extraerrordata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ExtendedError } from '../types-hoist/error';
55
import type { Event, EventHint } from '../types-hoist/event';
66
import type { IntegrationFn } from '../types-hoist/integration';
77
import { isError, isPlainObject } from '../utils/is';
8-
import { logger } from '../utils/logger';
8+
import { debug } from '../utils/logger';
99
import { normalize } from '../utils/normalize';
1010
import { addNonEnumerableProperty } from '../utils/object';
1111
import { truncate } from '../utils/string';
@@ -130,7 +130,7 @@ function _extractErrorData(
130130

131131
return extraErrorInfo;
132132
} catch (oO) {
133-
DEBUG_BUILD && logger.error('Unable to extract extra data from the Error object:', oO);
133+
DEBUG_BUILD && debug.error('Unable to extract extra data from the Error object:', oO);
134134
}
135135

136136
return null;

packages/core/src/integrations/supabase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '
1010
import { setHttpStatus, SPAN_STATUS_ERROR, SPAN_STATUS_OK, startSpan } from '../tracing';
1111
import type { IntegrationFn } from '../types-hoist/integration';
1212
import { isPlainObject } from '../utils/is';
13-
import { logger } from '../utils/logger';
13+
import { debug } from '../utils/logger';
1414

1515
const AUTH_OPERATIONS_TO_INSTRUMENT = [
1616
'reauthenticate',
@@ -481,7 +481,7 @@ function instrumentPostgRESTQueryBuilder(PostgRESTQueryBuilder: new () => PostgR
481481
const rv = Reflect.apply(target, thisArg, argumentsList);
482482
const PostgRESTFilterBuilder = (rv as PostgRESTFilterBuilder).constructor;
483483

484-
DEBUG_BUILD && logger.log(`Instrumenting ${operation} operation's PostgRESTFilterBuilder`);
484+
DEBUG_BUILD && debug.log(`Instrumenting ${operation} operation's PostgRESTFilterBuilder`);
485485

486486
instrumentPostgRESTFilterBuilder(PostgRESTFilterBuilder);
487487

@@ -496,7 +496,7 @@ function instrumentPostgRESTQueryBuilder(PostgRESTQueryBuilder: new () => PostgR
496496

497497
export const instrumentSupabaseClient = (supabaseClient: unknown): void => {
498498
if (!supabaseClient) {
499-
DEBUG_BUILD && logger.warn('Supabase integration was not installed because no Supabase client was provided.');
499+
DEBUG_BUILD && debug.warn('Supabase integration was not installed because no Supabase client was provided.');
500500
return;
501501
}
502502
const SupabaseClientConstructor =

0 commit comments

Comments
 (0)