如何使用WL.Logger api将日志消息输出到文件 [英] How to use WL.Logger api to output log messages to a file

查看:276
本文介绍了如何使用WL.Logger api将日志消息输出到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IBM Worklight框架开发iOS应用程序。我想在我的代码运行时记录消息,例如应用程序启动成功!



我的问题是



如何使用WL.Logger api来记录消息?

解决方案

Worklight 6.0:



查看IBM InfoCenter内更新的WL.Logger功能(( logHandler )。传递日志消息,它们将显示在 Google Analytics搜索视图


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?

解决方案

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.

这篇关于如何使用WL.Logger api将日志消息输出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆