简体   繁体   English

有没有办法禁用控制台中出现的所有错误和警告?

[英]Is there any way to disable all the errors and warnings that appear in the console?

I don't want to show the warnings in the console for a certain development environment. 我不想在控制台中显示某个开发环境的警告。 Is there any way to achieve that? 有没有办法实现这一目标? My application was bootstrapped using create react app. 我的应用程序是使用create react app引导的。

Since you are using React, my guess is you are already using babel. 既然你正在使用React,我猜你已经在使用了babel。 There's a plugin for that purpose. 有一个插件用于此目的。 It's called babel-plugin-transform-remove-console . 它被称为babel-plugin-transform-remove-console This will exclude all console.log 's statement during the build process. 这将在构建过程中排除所有console.log的语句。
Install that in your app and configure it via .babelrc as follows: 在您的应用程序中安装它并通过.babelrc配置,如下所示:

{
  "plugins": ["transform-remove-console"]
}

You can also specify the variant(s) of the console functions to exclude: 您还可以指定要排除的控制台功能的变体:

{
  "plugins": [ ["transform-remove-console", { "exclude": [ "error", "warn"] }] ]
}

My advise is to not use console logs in your code except necessary. 我建议不要在代码中使用控制台日志,除非必要。

In my App.js I have the following code for accomplishing this: 在我的App.js中,我有以下代码来完成此任务:

import { YellowBox } from 'react-native';

componentDidMount() {
    // The following lines are a workaround
    // in order to stop getting warnings about timer
    // See: https://github.com/firebase/firebase-js-sdk/issues/97#issuecomment-365456531
    YellowBox.ignoreWarnings(['Setting a timer']);
    const _console = _.clone(console);
    console.warn = message => {
      if (message.indexOf('Setting a timer') <= -1) {
        _console.warn(message);
      }
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React-控制台警告和错误 - React - console warnings & errors OnClick 不工作,但控制台中没有出现错误 - OnClick is not working, but no errors appear in the console 忽略 React 中的某些控制台错误/警告? - Ignore certain console errors / warnings in React? 在 react.js 有没有办法禁用所有子事件 - In react.js is there any way to disable all children events 有没有办法只隐藏开发人员控制台中出现的道具类型错误 - 不是所有错误 - Is there a way to hide just the prop type errors that come in developer console - not all the errors 如何禁用 i18next 控制台警告? - How do I disable i18next console warnings? 有没有办法禁用React的警告“无效的事件处理程序属性`onclick`”? - Is there a way to disable the warnings “Invalid event handler property `onclick`” of React? 是否可以在所有 eslint 规则上显示警告而不是错误? - Is it possible to show warnings instead of errors on ALL of eslint rules? Gatsby 开发/构建时有什么方法可以显示警告? - Is there any way to show warnings when Gatsby develop/build? 有没有办法普遍取消所有未安装组件的setstate调用或隐藏尝试它产生的错误? - Is there a way to universally cancel all calls to setstate on any unmounted component or to hide the errors produced by attempting it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM