0

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.

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
user2967626
  • 25
  • 1
  • 7
  • Which "console"? are you placing these logs in adapter code? app code? javascript? java? what is the application server used in the test/prod environment...? – Idan Adar Jan 06 '14 at 04:21
  • I want to write all the logs written in the app code i.e javascript to file . Iam using Websphere 8.5 full profile for the test environment – user2967626 Jan 06 '14 at 05:18
  • im writing the log statements in application js files, and iam seeing these in worklight console not specific to environment as of now, later i need to use for both andriod ans iphone environments. But when i deploy the same on the test/production environments, I need to see all the logs, where i will be able to view and what are the settings I need to do? – user2967626 Jan 06 '14 at 06:01
  • Read my edited answer. – Idan Adar Jan 06 '14 at 06:02
  • ok. Got the point. Thanks Adan.One more question, how to write the log statements in adapter code to a file? – user2967626 Jan 06 '14 at 06:16

1 Answers1

2

Server-side logging (adapters):


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); });
    }
    
Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • Thanks Adan. Got the point. But can we store these logs on the mobile device itself?.The requirement is like this, I should store the logs on the mobile device.If some problem occours, the user should be able to report a problem by attaching that log file.There will be menu 'Report problem', if the user clicks on that, email is opened with this log attached automatically. – user2967626 Jan 06 '14 at 06:33
  • See my edited answer on a possible way to send back a log file. – Idan Adar Jan 06 '14 at 06:52
  • var wlInitOptions = { logger : {enabled: true, level: 'debug', stringify: true, pretty: false, tag: {level: false, pkg: true}, whitelist: [], blacklist: [],callback: logHandler}, analytics : { enabled: false } }; if (window.addEventListener) { window.addEventListener('load', function() { WL.Client.init(wlInitOptions); }, false); } else if (window.attachEvent) { window.attachEvent('onload', function() { WL.Client.init(wlInitOptions); }); } var logHandler = function (message, priority, pkg) { alert('Written in Log File'); }; – user2967626 Jan 07 '14 at 02:09
  • This is my initOptions.js, logHandler function is not being called for every log written to console.Please help me – user2967626 Jan 07 '14 at 02:11
  • You need to either place the var logHandler above var wlInitOptions, or declare it as a function instead of a variable. See my edited answer. – Idan Adar Jan 07 '14 at 20:44
  • @user2967626, If my answer helped you resolve your problem, please mark as answered. – Idan Adar Nov 01 '14 at 14:30