Skip to content

Commit 4cd71c5

Browse files
authored
feat(logging): trim down debug logging further for non debug builds (#4341)
1 parent 0889534 commit 4cd71c5

File tree

11 files changed

+97
-47
lines changed

11 files changed

+97
-47
lines changed

packages/browser/src/helpers.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
addNonEnumerableProperty,
77
getGlobalObject,
88
getOriginalFunction,
9+
isDebugBuild,
910
logger,
1011
markFunctionWrapped,
1112
} from '@sentry/utils';
@@ -191,12 +192,16 @@ export function injectReportDialog(options: ReportDialogOptions = {}): void {
191192
}
192193

193194
if (!options.eventId) {
194-
logger.error(`Missing eventId option in showReportDialog call`);
195+
if (isDebugBuild()) {
196+
logger.error(`Missing eventId option in showReportDialog call`);
197+
}
195198
return;
196199
}
197200

198201
if (!options.dsn) {
199-
logger.error(`Missing dsn option in showReportDialog call`);
202+
if (isDebugBuild()) {
203+
logger.error(`Missing dsn option in showReportDialog call`);
204+
}
200205
return;
201206
}
202207

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
addExceptionMechanism,
66
addInstrumentationHandler,
77
getLocationHref,
8+
isDebugBuild,
89
isErrorEvent,
910
isPrimitive,
1011
isString,
@@ -239,7 +240,9 @@ function _enhanceEventWithInitialFrame(event: Event, url: any, line: any, column
239240
}
240241

241242
function globalHandlerLog(type: string): void {
242-
logger.log(`Global Handler attached: ${type}`);
243+
if (isDebugBuild()) {
244+
logger.log(`Global Handler attached: ${type}`);
245+
}
243246
}
244247

245248
function addMechanismAndCapture(hub: Hub, error: EventHint['originalException'], event: Event, type: string): void {

packages/browser/src/sdk.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
2-
import { addInstrumentationHandler, getGlobalObject, logger, resolvedSyncPromise } from '@sentry/utils';
2+
import { Hub } from '@sentry/types';
3+
import { addInstrumentationHandler, getGlobalObject, isDebugBuild, logger, resolvedSyncPromise } from '@sentry/utils';
34

45
import { BrowserOptions } from './backend';
56
import { BrowserClient } from './client';
@@ -161,7 +162,9 @@ export function flush(timeout?: number): PromiseLike<boolean> {
161162
if (client) {
162163
return client.flush(timeout);
163164
}
164-
logger.warn('Cannot flush events. No client defined.');
165+
if (isDebugBuild()) {
166+
logger.warn('Cannot flush events. No client defined.');
167+
}
165168
return resolvedSyncPromise(false);
166169
}
167170

@@ -178,7 +181,9 @@ export function close(timeout?: number): PromiseLike<boolean> {
178181
if (client) {
179182
return client.close(timeout);
180183
}
181-
logger.warn('Cannot flush events and disable SDK. No client defined.');
184+
if (isDebugBuild()) {
185+
logger.warn('Cannot flush events and disable SDK. No client defined.');
186+
}
182187
return resolvedSyncPromise(false);
183188
}
184189

@@ -194,6 +199,11 @@ export function wrap(fn: (...args: any) => any): any {
194199
return internalWrap(fn)();
195200
}
196201

202+
function startSessionOnHub(hub: Hub): void {
203+
hub.startSession({ ignoreDuration: true });
204+
hub.captureSession();
205+
}
206+
197207
/**
198208
* Enable automatic Session Tracking for the initial page load.
199209
*/
@@ -202,7 +212,9 @@ function startSessionTracking(): void {
202212
const document = window.document;
203213

204214
if (typeof document === 'undefined') {
205-
logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
215+
if (isDebugBuild()) {
216+
logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
217+
}
206218
return;
207219
}
208220

@@ -214,24 +226,21 @@ function startSessionTracking(): void {
214226
// https://github.com/getsentry/sentry-javascript/issues/3207 and
215227
// https://github.com/getsentry/sentry-javascript/issues/3234 and
216228
// https://github.com/getsentry/sentry-javascript/issues/3278.
217-
if (typeof hub.startSession !== 'function' || typeof hub.captureSession !== 'function') {
229+
if (!hub.captureSession) {
218230
return;
219231
}
220232

221233
// The session duration for browser sessions does not track a meaningful
222234
// concept that can be used as a metric.
223235
// Automatically captured sessions are akin to page views, and thus we
224236
// discard their duration.
225-
hub.startSession({ ignoreDuration: true });
226-
hub.captureSession();
237+
startSessionOnHub(hub);
227238

228239
// We want to create a session for every navigation as well
229240
addInstrumentationHandler('history', ({ from, to }) => {
230241
// Don't create an additional session for the initial route or if the location did not change
231-
if (from === undefined || from === to) {
232-
return;
242+
if (!(from === undefined || from === to)) {
243+
startSessionOnHub(getCurrentHub());
233244
}
234-
hub.startSession({ ignoreDuration: true });
235-
hub.captureSession();
236245
});
237246
}

packages/browser/src/transports/base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
dsnToString,
2222
eventStatusFromHttpCode,
2323
getGlobalObject,
24+
isDebugBuild,
2425
logger,
2526
makePromiseBuffer,
2627
parseRetryAfterHeader,
@@ -173,8 +174,9 @@ export abstract class BaseTransport implements Transport {
173174
* https://developer.mozilla.org/en-US/docs/Web/API/Headers/get
174175
*/
175176
const limited = this._handleRateLimit(headers);
176-
if (limited)
177+
if (limited && isDebugBuild()) {
177178
logger.warn(`Too many ${requestType} requests, backing off until: ${this._disabledUntil(requestType)}`);
179+
}
178180

179181
if (status === 'success') {
180182
resolve({ status });

packages/browser/src/transports/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { forget, getGlobalObject, isNativeFetch, logger, supportsFetch } from '@sentry/utils';
1+
import { forget, getGlobalObject, isDebugBuild, isNativeFetch, logger, supportsFetch } from '@sentry/utils';
22

33
const global = getGlobalObject<Window>();
44
let cachedFetchImpl: FetchImpl;
@@ -69,7 +69,9 @@ export function getNativeFetchImplementation(): FetchImpl {
6969
}
7070
document.head.removeChild(sandbox);
7171
} catch (e) {
72-
logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e);
72+
if (isDebugBuild()) {
73+
logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e);
74+
}
7375
}
7476
}
7577

packages/core/src/basebackend.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Event, EventHint, Options, Session, SeverityLevel, Transport } from '@sentry/types';
2-
import { logger, SentryError } from '@sentry/utils';
2+
import { isDebugBuild, logger, SentryError } from '@sentry/utils';
33

44
import { NoopTransport } from './transports/noop';
55

@@ -92,7 +92,9 @@ export abstract class BaseBackend<O extends Options> implements Backend {
9292
*/
9393
public sendEvent(event: Event): void {
9494
void this._transport.sendEvent(event).then(null, reason => {
95-
logger.error(`Error while sending event: ${reason}`);
95+
if (isDebugBuild()) {
96+
logger.error(`Error while sending event: ${reason}`);
97+
}
9698
});
9799
}
98100

@@ -101,12 +103,16 @@ export abstract class BaseBackend<O extends Options> implements Backend {
101103
*/
102104
public sendSession(session: Session): void {
103105
if (!this._transport.sendSession) {
104-
logger.warn("Dropping session because custom transport doesn't implement sendSession");
106+
if (isDebugBuild()) {
107+
logger.warn("Dropping session because custom transport doesn't implement sendSession");
108+
}
105109
return;
106110
}
107111

108112
void this._transport.sendSession(session).then(null, reason => {
109-
logger.error(`Error while sending session: ${reason}`);
113+
if (isDebugBuild()) {
114+
logger.error(`Error while sending session: ${reason}`);
115+
}
110116
});
111117
}
112118

packages/core/src/baseclient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import {
1515
checkOrSetAlreadyCaught,
1616
dateTimestampInSeconds,
17+
isDebugBuild,
1718
isPlainObject,
1819
isPrimitive,
1920
isThenable,
@@ -172,12 +173,16 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
172173
*/
173174
public captureSession(session: Session): void {
174175
if (!this._isEnabled()) {
175-
logger.warn('SDK not enabled, will not capture session.');
176+
if (isDebugBuild()) {
177+
logger.warn('SDK not enabled, will not capture session.');
178+
}
176179
return;
177180
}
178181

179182
if (!(typeof session.release === 'string')) {
180-
logger.warn('Discarded session because of missing or non-string release');
183+
if (isDebugBuild()) {
184+
logger.warn('Discarded session because of missing or non-string release');
185+
}
181186
} else {
182187
this._sendSession(session);
183188
// After sending, we set init false to indicate it's not the first occurrence

packages/core/src/integrations/inboundfilters.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
22
import { Event, Integration, StackFrame } from '@sentry/types';
3-
import { getEventDescription, isMatchingPattern, logger } from '@sentry/utils';
3+
import { getEventDescription, isDebugBuild, isMatchingPattern, logger } from '@sentry/utils';
44

55
// "Script error." is hard coded into browsers for errors that it can't read.
66
// this is the result of a script being pulled in from an external domain and CORS.
@@ -64,29 +64,37 @@ export class InboundFilters implements Integration {
6464
/** JSDoc */
6565
private _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean {
6666
if (this._isSentryError(event, options)) {
67-
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
67+
if (isDebugBuild()) {
68+
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
69+
}
6870
return true;
6971
}
7072
if (this._isIgnoredError(event, options)) {
71-
logger.warn(
72-
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
73-
);
73+
if (isDebugBuild()) {
74+
logger.warn(
75+
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
76+
);
77+
}
7478
return true;
7579
}
7680
if (this._isDeniedUrl(event, options)) {
77-
logger.warn(
78-
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
79-
event,
80-
)}.\nUrl: ${this._getEventFilterUrl(event)}`,
81-
);
81+
if (isDebugBuild()) {
82+
logger.warn(
83+
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
84+
event,
85+
)}.\nUrl: ${this._getEventFilterUrl(event)}`,
86+
);
87+
}
8288
return true;
8389
}
8490
if (!this._isAllowedUrl(event, options)) {
85-
logger.warn(
86-
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
87-
event,
88-
)}.\nUrl: ${this._getEventFilterUrl(event)}`,
89-
);
91+
if (isDebugBuild()) {
92+
logger.warn(
93+
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
94+
event,
95+
)}.\nUrl: ${this._getEventFilterUrl(event)}`,
96+
);
97+
}
9098
return true;
9199
}
92100
return false;
@@ -179,7 +187,9 @@ export class InboundFilters implements Integration {
179187
const { type = '', value = '' } = (event.exception.values && event.exception.values[0]) || {};
180188
return [`${value}`, `${type}: ${value}`];
181189
} catch (oO) {
182-
logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
190+
if (isDebugBuild()) {
191+
logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
192+
}
183193
return [];
184194
}
185195
}
@@ -214,7 +224,9 @@ export class InboundFilters implements Integration {
214224
}
215225
return frames ? this._getLastValidUrl(frames) : null;
216226
} catch (oO) {
217-
logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
227+
if (isDebugBuild()) {
228+
logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
229+
}
218230
return null;
219231
}
220232
}

packages/types/src/hub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,5 @@ export interface Hub {
227227
* Sends the current session on the scope to Sentry
228228
* @param endSession If set the session will be marked as exited and removed from the scope
229229
*/
230-
captureSession(endSession: boolean): void;
230+
captureSession(endSession?: boolean): void;
231231
}

packages/utils/src/instrument.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* eslint-disable @typescript-eslint/ban-types */
44
import { WrappedFunction } from '@sentry/types';
55

6+
import { isDebugBuild } from '.';
67
import { getGlobalObject } from './global';
78
import { isInstanceOf, isString } from './is';
89
import { logger } from './logger';
@@ -93,11 +94,13 @@ function triggerHandlers(type: InstrumentHandlerType, data: any): void {
9394
try {
9495
handler(data);
9596
} catch (e) {
96-
logger.error(
97-
`Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(
98-
handler,
99-
)}\nError: ${e}`,
100-
);
97+
if (isDebugBuild()) {
98+
logger.error(
99+
`Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(
100+
handler,
101+
)}\nError: ${e}`,
102+
);
103+
}
101104
}
102105
}
103106
}

packages/utils/src/supports.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isDebugBuild } from './env';
12
import { getGlobalObject } from './global';
23
import { logger } from './logger';
34

@@ -112,7 +113,9 @@ export function supportsNativeFetch(): boolean {
112113
}
113114
doc.head.removeChild(sandbox);
114115
} catch (err) {
115-
logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
116+
if (isDebugBuild()) {
117+
logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
118+
}
116119
}
117120
}
118121

0 commit comments

Comments
 (0)