Yar*_*evi 12 console.log react-native
我的应用程序中有很多console.log().他们中的大多数都是陷阱,所以我可以清楚地看到开发时出了什么问题.有些是记录当前时间,所以我可以检查功能执行时间.
部署到生产时,那些console.log()将在客户端的设备中运行.我可以把它们原样留下吗?它们会损害性能/内存还是可能导致一些异常或不需要的行为?
Is this good practice to disable all console.log statements in your entire app?
Any side effects?
At top of file: App.js include:
// To assign console.log to nothing
if (!__DEV__) {
console.log = () => {};
}
Run Code Online (Sandbox Code Playgroud)
嗯,是。更多代码意味着更长的执行时间。它不仅会占用不必要的 CPU“功率”,console.log而且是同步的,因此会使您的应用程序变慢(甚至慢几纳秒)。
但是,如果您想使用调试,您应该真正研究Winston。它是异步的,因此解决了上述问题。
如果您不太关心毫秒,那么我会保留它,尽管让捆绑包创建者删除即将投入生产的代码并不需要太多时间。您可以执行如下操作:
if(__DEV__) {
console.log('This will be stripped in production.');
}
Run Code Online (Sandbox Code Playgroud)
ReactNative 文档建议删除它们。
其中一种方法是使用 babel 插件来删除console.log语句。我发现它很有帮助,但不一定合适,因为我想在调试应用程序时查看一些日志,并且如果在生产中发生这种情况,我仍然会收到通知(如前面提到的 catch 块)。
我通常做两件事:
dlog代替console.logexport const dlog = (message: string, ...optionalParams: any[]) => {
if (__DEV__) {
// eslint-disable-next-line no-console
console.log(message, ...optionalParams)
} else {
// Logging to Sentry or another bug-tracking system
// withScope(scope => {
// scope.setExtra('params', optionalParams)
// captureMessage(message)
// })
}
}
Run Code Online (Sandbox Code Playgroud)
eslint禁止使用任何console.log语句| 归档时间: |
|
| 查看次数: |
5296 次 |
| 最近记录: |