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

旧巷老猫 提交于 2020-08-04 19:35:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/52386509/how-do-i-get-all-console-messages-to-log-on-cordova-ios-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!