Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get all console messages to log on Cordova iOS at runtime?

When I am running my WKWebview application under Cordova on iOS, messages that I log in my web application using console.debug and console.info are not getting output to the Xcode console or in my logs. console.log messages are getting output however. How can I get all of my log messages to output?

like image 271
2Aguy Avatar asked Sep 18 '18 12:09

2Aguy


1 Answers

After much research and reviewing of the cordova.js source, I realized that the reason why my console.info messages were not getting logged is because Cordova overrides the console object when running under iOS. By default, only console.log messages, console.error, and console.warn messages are output. The reason for this is that the Cordova logger supports the following log levels:

  • LOG
  • ERROR
  • WARN <-- DEFAULT LEVEL
  • INFO
  • DEBUG

and WARN is the default log level. This means that only messages logged at WARN level and above will be output, so console.warn, console.error and console.log messages will only be output. To change the log level, you will need to do the following:

1) In a similar fashion to how you would use the Cordova exec function by loading its module, you will also need to load the Cordova logger module in your javascript. To load the logger module, add this to your javascript:

var logger = require('cordova/plugin/ios/logger');

2) Next, all you need to do in order to change the default logging level from WARN to another level, such as DEBUG, is to add the following call at the appropriate place in your initialization code. Where you insert this code, will depend on your application, but the above logger module that you declared in step 1 above will need to be in scope.

logger.level('DEBUG');

Levels you can use are as mentioned above: 'LOG', 'ERROR', 'WARN', 'INFO', 'DEBUG'

Alternatively you should also be able to use the level constants that the Cordova logging module defines: logger.LOG, logger.ERROR, logger.WARN, logger.INFO, logger.DEBUG. I haven't tried this, but it should work.

Hopefully this helps save others some time in the future since I was unable to find any documentation on this.

like image 199
2Aguy Avatar answered Nov 15 '22 04:11

2Aguy