Skip to content

Commit bcde38c

Browse files
committed
Add toggle to unpatch global console
1 parent 818c480 commit bcde38c

File tree

1 file changed

+58
-53
lines changed

1 file changed

+58
-53
lines changed

packages/shared/ConsolePatchingDev.js

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,71 +20,76 @@ let prevError;
2020
let prevGroup;
2121
let prevGroupCollapsed;
2222
let prevGroupEnd;
23+
let enableDoubleLogging = global.__REACT_DEVTOOLS_ENABLE_DOUBLE_LOGGING__;
2324

2425
function disabledLog() {}
2526
disabledLog.__reactDisabledLog = true;
2627

2728
export function disableLogs(): void {
2829
if (__DEV__) {
29-
if (disabledDepth === 0) {
30-
/* eslint-disable react-internal/no-production-logging */
31-
prevLog = console.log;
32-
prevInfo = console.info;
33-
prevWarn = console.warn;
34-
prevError = console.error;
35-
prevGroup = console.group;
36-
prevGroupCollapsed = console.groupCollapsed;
37-
prevGroupEnd = console.groupEnd;
38-
// https://github.com/facebook/react/issues/19099
39-
const props = {
40-
configurable: true,
41-
enumerable: true,
42-
value: disabledLog,
43-
writable: true,
44-
};
45-
// $FlowFixMe Flow thinks console is immutable.
46-
Object.defineProperties(console, {
47-
info: props,
48-
log: props,
49-
warn: props,
50-
error: props,
51-
group: props,
52-
groupCollapsed: props,
53-
groupEnd: props,
54-
});
55-
/* eslint-enable react-internal/no-production-logging */
30+
if (!enableDoubleLogging) {
31+
if (disabledDepth === 0) {
32+
/* eslint-disable react-internal/no-production-logging */
33+
prevLog = console.log;
34+
prevInfo = console.info;
35+
prevWarn = console.warn;
36+
prevError = console.error;
37+
prevGroup = console.group;
38+
prevGroupCollapsed = console.groupCollapsed;
39+
prevGroupEnd = console.groupEnd;
40+
// https://github.com/facebook/react/issues/19099
41+
const props = {
42+
configurable: true,
43+
enumerable: true,
44+
value: disabledLog,
45+
writable: true,
46+
};
47+
// $FlowFixMe Flow thinks console is immutable.
48+
Object.defineProperties(console, {
49+
info: props,
50+
log: props,
51+
warn: props,
52+
error: props,
53+
group: props,
54+
groupCollapsed: props,
55+
groupEnd: props,
56+
});
57+
/* eslint-enable react-internal/no-production-logging */
58+
}
59+
disabledDepth++;
5660
}
57-
disabledDepth++;
5861
}
5962
}
6063

6164
export function reenableLogs(): void {
6265
if (__DEV__) {
63-
disabledDepth--;
64-
if (disabledDepth === 0) {
65-
/* eslint-disable react-internal/no-production-logging */
66-
const props = {
67-
configurable: true,
68-
enumerable: true,
69-
writable: true,
70-
};
71-
// $FlowFixMe Flow thinks console is immutable.
72-
Object.defineProperties(console, {
73-
log: {...props, value: prevLog},
74-
info: {...props, value: prevInfo},
75-
warn: {...props, value: prevWarn},
76-
error: {...props, value: prevError},
77-
group: {...props, value: prevGroup},
78-
groupCollapsed: {...props, value: prevGroupCollapsed},
79-
groupEnd: {...props, value: prevGroupEnd},
80-
});
81-
/* eslint-enable react-internal/no-production-logging */
82-
}
83-
if (disabledDepth < 0) {
84-
console.error(
85-
'disabledDepth fell below zero. ' +
86-
'This is a bug in React. Please file an issue.',
87-
);
66+
if (!enableDoubleLogging) {
67+
disabledDepth--;
68+
if (disabledDepth === 0) {
69+
/* eslint-disable react-internal/no-production-logging */
70+
const props = {
71+
configurable: true,
72+
enumerable: true,
73+
writable: true,
74+
};
75+
// $FlowFixMe Flow thinks console is immutable.
76+
Object.defineProperties(console, {
77+
log: {...props, value: prevLog},
78+
info: {...props, value: prevInfo},
79+
warn: {...props, value: prevWarn},
80+
error: {...props, value: prevError},
81+
group: {...props, value: prevGroup},
82+
groupCollapsed: {...props, value: prevGroupCollapsed},
83+
groupEnd: {...props, value: prevGroupEnd},
84+
});
85+
/* eslint-enable react-internal/no-production-logging */
86+
}
87+
if (disabledDepth < 0) {
88+
console.error(
89+
'disabledDepth fell below zero. ' +
90+
'This is a bug in React. Please file an issue.',
91+
);
92+
}
8893
}
8994
}
9095
}

0 commit comments

Comments
 (0)