Search code examples
ibm-mobilefirstworklight-adapters

WL.Logger.Send() its Not callback the WLClientLogReceiver adapter


I have enabled the nativeOptions: {capture: true} in initOptions.js

logger : {enabled: true, level: 'debug', stringify: true, pretty: false, tag: {level: false, pkg: true}, whitelist: [], blacklist: [], nativeOptions: {capture: true}}

In my main js file i have the following code.

function wlCommonInit(){
// Common initialization code goes here
WL.Logger.setNativeOptions({'capture': true});

var logger = WL.Logger.create({pkg: 'mypackage'});

logger.debug('Hello world - debug');
//[mypackage] Hello world

logger.log('Hello world - log');
//[mypackage] Hello world

logger.info('Hello world - info');
//[mypackage] Hello world

logger.warn('Hello world - warn');
//[mypackage] Hello world

logger.error('Hello world - error');
//[mypackage] Hello world
WL.Logger.send();  }

WL.Logger.send() suppose to call my adapter "WLClientLogReceiver". But i am not getting any call for this adapter.

Please let me know, i need to enable any other settings to upload my client side captured log to server.

 function log(deviceInfo, logMessages) {
 return true;}

<procedure name="log" securityTest="wl_unprotected" audit="true" />

Solution

  • logger : {enabled: true, level: 'debug', stringify: true, pretty: false, tag: {level: false, pkg: true}, whitelist: [], blacklist: [], nativeOptions: {capture: true}}

    You have enabled the native capture as true in initOptions.js so no need to set it again.

    You can log using your package that will help you in filtering the messages based on the package in your WLClientLogReceiver adapter.

    var myloggerObject = WL.Logger.create({pkg: 'mypackage'});
    myloggerObject.debug("Hello world");
    

    you can specify your level in your js file to be logged in client device.

    In the adapter you will get the log messages as an json array.
    

    function log(deviceInfo, logMessages) {

    /* The adapter can choose to process the parameters, for example to forward them to a backend server for safekeeping and further analysis.

     The deviceInfo object may look like this:
     {
       "appName":       "wlapp",
       "appVersion":    "1.0",
       "deviceId":      "66eed0c9-ecf7-355f-914a-3cedac70ebcc",
       "model":         "Galaxy Nexus - 4.2.2 - API 17 - 720x1280",
       "systemName":    "Android",
       "systemVersion": "4.2.2",
       "os.arch":       "i686",           // Android only
       "os.version":    "3.4.0-qemu+"     // Android only
      }
      The logMessages parameter is a JSON array 
      that contains JSON object elements, and might look like this:
    
        [{
          "timestamp"    : "17-02-2013 13:54:23:745",  // "dd-MM-yyyy hh:mm:ss:S"
          "level"        : "ERROR",                    // ERROR||WARN||INFO||LOG|| DEBUG
          "package"      : "your_tag",                 // typically a class name
          "msg"          : "the message",              // a helpful log message
          "threadid"     : 42,                         // (Android only)the current thread
          "metadata"     : { "$src" : "js" }           // metadata placed on the log call
        }]
    

    */

    //sample log and filtering method

    var logs= [{
          "timestamp"    : "17-02-2013 13:54:23:745",  // "dd-MM-yyyy hh:mm:ss:S"
          "level"        : "ERROR",                    // ERROR||WARN||INFO||LOG|| DEBUG
          "package"      : "your_tag",                 // typically a class name
          "msg"          : "the message",              // a helpful log message
          "threadid"     : 42,                         // (Android only)the current thread
          "metadata"     : { "$src" : "js" }           // metadata placed on the log call
        },
        {
          "timestamp"    : "17-02-2013 13:54:23:745",  // "dd-MM-yyyy hh:mm:ss:S"
          "level"        : "ERROR",                    // ERROR||WARN||INFO||LOG|| DEBUG
          "package"      : "mypackage",                 // typically a class name
          "msg"          : "my package message",              // a helpful log message
          "threadid"     : 42,                         // (Android only)the current thread
          "metadata"     : { "$src" : "js" }           // metadata placed on the log call
        }
      ];
     var filteredLogs = logs.filter(function(log){
               if(log.package == mypackage) //comparing the package and returns the object
                    { return log; }   
    });
    
    WL.Logger.error(filteredLogs);// This is send only the filtered array to your server
    
     }
    

    If you log using metadata such as filename along with the debug message you will get those in the array in metadata attribute.

    It is suggested to stringify and parse the object to avoid errors before parsing the device logs in the adapter.

    var logs = JSON.stringify(JSON.parse(logs));
    var filteredLogs = logs.filter ...
    

    Hope this will work for you.

    Make sure you test it using the device.