简体   繁体   English

如何在 Cordova / PhoneGap 中关闭 console.log() 进行生产?

[英]How to turn off console.log() for production in Cordova / PhoneGap?

How can I turn off the JavaScipt console.log() for production in Cordova / PhoneGap?如何在 Cordova / PhoneGap 中关闭 JavaScipt console.log() 以进行生产? Disabling the debugging information in the console probably speeds up the mobile app quite a bit.在控制台中禁用调试信息可能会大大加快移动应用程序的速度。

There are numerous approaches, but none seems to work.有很多方法,但似乎没有一个有效。

Any working examples?任何工作示例? Other feasible approaches?其他可行的方法?

Wrapping might be a solution.包装可能是一个解决方案。 If no 3rd party library is using the actual console and if you only want to use the log method.如果没有第 3 方库使用实际的控制台,并且您只想使用 log 方法。

var konsole = {
    log: function () {
        if (env === 'dev') {
            console.log(arguments)
        }

    }
}

konsole.log('fff');

env is a replacement for an environment var, which you could get from some kind of registry. env 是环境变量的替代品,您可以从某种注册表中获得它。

The correct way to do it is to override the Debug Console plugin which on iOS defaults to CDVDebugConsole .正确的做法是覆盖 iOS 上默认为CDVDebugConsoleDebug Console插件。 Subclass that class then do something like this:子类化那个类然后做这样的事情:

- (void)log:(CDVInvokedUrlCommand*)command
{
    #ifdef DEBUG
        [super log:command];
    #endif
}

Remember to update your config.xml to use the new plugin.请记住更新您的config.xml以使用新插件。

I would recommend using: https://github.com/sunnykgupta/jsLogger我建议使用: https : //github.com/sunnykgupta/jsLogger

Features:特征:

  • It safely overrides the console.log.它安全地覆盖了 console.log。 Takes care if the console is not available (oh yes, you need to factor that too.)如果控制台不可用,请注意(哦,是的,您也需要考虑这一点。)
  • Stores all logs (even if they are suppressed) for later retrieval.存储所有日志(即使它们被抑制)以供以后检索。
  • Handles major console functions like log, warn, error, info.处理主要的控制台功能,如日志、警告、错误、信息。
  • Is open for modifications and will be updated whenever new suggestions come up.可以修改,并且会在出现新建议时更新。

Disclaimer: I am the author of the plugin.免责声明:我是插件的作者。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM