-->

IBM Worklight 6.0 - How to write logs to a file an

2019-08-17 07:41发布

问题:

I am using worklight 6.0 and I am able to log data to a console using WL.Logger.debug("msg");.

My question is: How to write all of these logs to a file?

My requirement is to store these logs on the mobile device itself so that if some problem occurs, the user will be able to report the problem by attaching that log file. In the app there will be a menu "Report problem", if the user clicks on that, email is opened with this log attached automatically.

回答1:

Server-side logging (adapters):

  • Logging and monitoring mechanisms
  • WL.Logger.debug, info, warn, and error
  • Related question: Worklight 5.0.6.1 - System.out.println() logging from Worklight adapter is not working

    Basically, you need to change the logging level in the application server's server.xml in order to be able to view different logging data. There you also decide which file will house these logs (by default messages.log) - see documentation for location based on your application server.


Client-side logging (application):

  • The WL.Logger object (read this!)
  • Related question: How to use WL.Logger api to output log messages to a file

    If you would like to store your log lines and later on send them back to some backend system, you can use a Callback function to send the logs to a file on your server

    Example code:
    In this code the logHandler() function treats only packages that I have decided I want to process, by create a new Logger object ("appLogic") for specific logs I want to log in the app.

    What you would need to do in your own app, for example, instead of displaying an alert() is to use the Cordova File API to save these log lines in a file and then, when required, to send them back to your backend via Worklight Adapters or AJAX calls or an email app, etc...

    common\js\main.js:

    var appLogic = new WL.Logger.create({pkg: 'appLogic'});
    
    function wlCommonInit(){
      appLogic.debug("log from app");
    }
    

    common\js\initOptions.js:

     var wlInitOptions = {
        connectOnStartup : false,
        analytics : {
          enabled: false
          //url : ''
      },
      logger : {enabled: true, level: 'debug', stringify: true, pretty: false,
          tag: {level: false, pkg: true}, whitelist: [], blacklist: [], callback: logHandler},    
     };
    
     function logHandler(message, priority, pkg) {
       if (pkg == 'appLogic') {
          alert (message);
       }
    }
    
    if (window.addEventListener) {
      window.addEventListener('load', function() { WL.Client.init(wlInitOptions); }, false);
    } else if (window.attachEvent) {
      window.attachEvent('onload',  function() { WL.Client.init(wlInitOptions); });
    }