如何为log4javascript使用有效负载而不是form-data

时间:2012-08-21 04:37:45

标签: log4javascript

我受到webservice的限制:它需要一个json-payload!

所以,做一些像

这样的事情
var ajaxAppender = new log4javascript.AjaxAppender("clientLogger");
var jsonLayout = new log4javascript.JsonLayout();
ajaxAppender.setLayout(jsonLayout);
log.addAppender(ajaxAppender);

无效,因为它会在表单集合中创建两个键(datalayout)。

如何使用内置选项获取json-payload?

1 个答案:

答案 0 :(得分:5)

我创建了一个 JsonAppender

function JsonAppender(url) {
    var isSupported = true;
    var successCallback = function(data, textStatus, jqXHR) { return; };
    if (!url) {
        isSupported = false;
    }
    this.setSuccessCallback = function(successCallbackParam) {
        successCallback = successCallbackParam;
    };
    this.append = function (loggingEvent) {
        if (!isSupported) {
            return;
        }
        $.post(url, {
            'logger': loggingEvent.logger.name,
            'timestamp': loggingEvent.timeStampInMilliseconds,
            'level': loggingEvent.level.name,
            'url': window.location.href,
            'message': loggingEvent.getCombinedMessages(),
            'exception': loggingEvent.getThrowableStrRep()
        }, successCallback, 'json');
    };
}

JsonAppender.prototype = new log4javascript.Appender();
JsonAppender.prototype.toString = function() {
    return 'JsonAppender';
};
log4javascript.JsonAppender = JsonAppender;

像这样使用

var logger = log4javascript.getLogger('clientLogger');
var jsonAppender = new JsonAppender(url);
logger.addAppender(jsonAppender);