2

How can I turn off the JavaScipt console.log() for production in Cordova / PhoneGap? 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?

Community
  • 1
  • 1
Michael Schmidt
  • 3,391
  • 5
  • 34
  • 39

3 Answers3

1

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.

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.

Robin Drexler
  • 4,307
  • 3
  • 25
  • 28
1

The correct way to do it is to override the Debug Console plugin which on iOS defaults to CDVDebugConsole. 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.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
0

I would recommend using: https://github.com/sunnykgupta/jsLogger

Features:

  • It safely overrides the 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.

Sunny R Gupta
  • 5,026
  • 1
  • 31
  • 40