How to use WL.Logger api to output log messages to a file

会有一股神秘感。 提交于 2019-12-25 03:14:22

问题


I am developing an iOS app using IBM Worklight framework. I want to log messages as my code runs e.g. "application startup successful!"

My question is

How can I use WL.Logger api to log messages?


回答1:


Worklight 6.0:

Take a look at the updated WL.Logger functionality inside IBM InfoCenter (here and here). Specifically the part that allows you to set a callback.

Open initOptions.js and add create a callback function that will be called after every log message is sent to the console:

var logHandler = function (message, priority, pkg) {
    //... write to a file or send logs to a server
}; 

You can use the Cordova File API to write log messages to a file on the device (or JSONStore, LocalStorage, Cordova Storage, etc.). Later (setInterval) you could read the contents of that file and send it to a server using an adapter (or jQuery.ajax, etc.). On the server you could use Java inside Adapters to write to files... or write to a DB, ElasticSearch Cluster, data infrastructure cloud offering, etc.

var wlInitOptions = {

    connectOnStartup : true,

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

};

Notice callback: logHandler in the code block above.

Worklight pre-6.0:

This functionality is not part of WL.Logger on Worklight pre 6.0, but you could implement it. For example:

//Implementation:
var LoggerWrapper = (function (callback) {

    var _cb, _ctx;

    return {

        setCallbackAndContext : function (cb, ctx) {
            _cb = cb;
            _ctx = ctx;
        }

        log : function () {
            console.log.apply(console, arguments);
            _cb(_ctx, arguments)

        }
    }

})();

//Usage:
LoggerWrapper.setCallbackAndContext(functionThatCallsAdapter, ObjectThatHasNetworkEnvironmentInfoEtc);

LoggerWrapper.log('hello');

APIs that provide useful info. (context) that could be appended to log messages:

environment : WL.Client.getAppProperty(WL.AppProp.ENVIRONMENT)
appName : WL.Client.getAppProperty(WL.AppProp.APP_DISPLAY_NAME)
appVersion : WL.Client.getAppProperty(WL.AppProp.APP_VERSION)
deviceContext : WL.Device.getContext() //see Location Services API
network: WL.Device.getNetworkInfo() //async
timestamp: new Date()

Debug Mode

A lot of apps capture extra logs and send them to the server by enabling a 'debug mode', not by default.

Operational Analytics

If you have configured the new Operational Analytics feature you can simply call WL.Analytics.log in the callback (logHandler). Pass the the log messages and they will show up in the Analytics Search View.



来源:https://stackoverflow.com/questions/18249703/how-to-use-wl-logger-api-to-output-log-messages-to-a-file

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