mirror of
https://github.com/zebrajr/react.git
synced 2026-01-15 12:15:22 +00:00
Basically make `console.error` and `console.warn` behave like normal - when a component stack isn't appended. I need this because I need to be able to print rich logs with the component stack option and to be able to disable instrumentation completely in `console.createTask` environments that don't need it. Currently we can't print logs with richer objects because they're toString:ed first. In practice, pretty much all arguments we log are already toString:ed so it's not necessary anyway. Some might be like a number. So it would only be a problem if some environment can't handle proper consoles but then it's up to that environment to toString it before logging. The `Warning: ` prefix is historic and is both noisy and confusing. It's mostly unnecessary since the UI surrounding `console.error` and `console.warn` tend to have visual treatment around it anyway. However, it's actively misleading when `console.error` gets prefixed with a Warning that we consider an error level. There's an argument to be made that some of our `console.error` don't make the bar for an error but then the argument is to downgrade each of those to `console.warn` - not to brand all our actual error logging with `Warning: `. Apparently something needs to change in React Native before landing this because it depends on the prefix somehow which probably doesn't make sense already.
65 lines
2.1 KiB
JavaScript
65 lines
2.1 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import ReactSharedInternals from 'shared/ReactSharedInternals';
|
|
import {enableOwnerStacks} from 'shared/ReactFeatureFlags';
|
|
|
|
let suppressWarning = false;
|
|
export function setSuppressWarning(newSuppressWarning) {
|
|
if (__DEV__) {
|
|
suppressWarning = newSuppressWarning;
|
|
}
|
|
}
|
|
|
|
// In DEV, calls to console.warn and console.error get replaced
|
|
// by calls to these methods by a Babel plugin.
|
|
//
|
|
// In PROD (or in packages without access to React internals),
|
|
// they are left as they are instead.
|
|
|
|
export function warn(format, ...args) {
|
|
if (__DEV__) {
|
|
if (!suppressWarning) {
|
|
printWarning('warn', format, args, new Error('react-stack-top-frame'));
|
|
}
|
|
}
|
|
}
|
|
|
|
export function error(format, ...args) {
|
|
if (__DEV__) {
|
|
if (!suppressWarning) {
|
|
printWarning('error', format, args, new Error('react-stack-top-frame'));
|
|
}
|
|
}
|
|
}
|
|
|
|
// eslint-disable-next-line react-internal/no-production-logging
|
|
const supportsCreateTask = __DEV__ && enableOwnerStacks && !!console.createTask;
|
|
|
|
function printWarning(level, format, args, currentStack) {
|
|
// When changing this logic, you might want to also
|
|
// update consoleWithStackDev.www.js as well.
|
|
if (__DEV__) {
|
|
if (!supportsCreateTask && ReactSharedInternals.getCurrentStack) {
|
|
// We only add the current stack to the console when createTask is not supported.
|
|
// Since createTask requires DevTools to be open to work, this means that stacks
|
|
// can be lost while DevTools isn't open but we can't detect this.
|
|
const stack = ReactSharedInternals.getCurrentStack(currentStack);
|
|
if (stack !== '') {
|
|
format += '%s';
|
|
args = args.concat([stack]);
|
|
}
|
|
}
|
|
|
|
args.unshift(format);
|
|
// We intentionally don't use spread (or .apply) directly because it
|
|
// breaks IE9: https://github.com/facebook/react/issues/13610
|
|
// eslint-disable-next-line react-internal/no-production-logging
|
|
Function.prototype.apply.call(console[level], console, args);
|
|
}
|
|
}
|