Configuring the data of the AjaxAppender Request Log4Javascript

541 Views Asked by At

I am very new to Log4Javascript and logging in general, so bear with me.

I am attempting to POST the logs to a CouchDB. I am receiving an error from the server:

{"error":"bad_request","reason":"Document must be a JSON object"}

Okay cool. So its not a JSON Object, I can fix that.

Except that I can't figure out how.

The code I am using is:

var log = log4javascript.getLogger();

var ajaxAppender = new log4javascript.AjaxAppender(url);
var layout = new log4javascript.JsonLayout(true, false);
ajaxAppender.addHeader("Content-Type", "application/json");
ajaxAppender.setLayout(layout);
log.addAppender(ajaxAppender);

// Test the logger
log.debug(JSON.stringify("Hello world!"));

Everywhere I have searched says this is the way to send it as a JSON object, so I imagine this is correct.

When I look at the request payload, I realize that the CouchDB server must not like the way it is formatted, which is like so:

[
{
    "logger": "[anonymous]",
    "timestamp": 1487961971605,
    "level": "DEBUG",
    "url": "url.to.couchdb",
    "message": "\"Hello world!\""
}
]

As you can see, it is a JSON object within an array, and I believe this is where my problem lies.

So my questions are:

  • Did I miss something in setting up the AjaxAppender and the JsonLayout?
  • Is there a way to change the format of the Request Payload with log4javascript?
  • If not, is there a way in CouchDB that when a document is POSTed, I can intercept and change the Request Payload and continue on, so it removes the array (that I am assuming it does not like)?

Thanks.

1

There are 1 best solutions below

0
Hypnic Jerk On BEST ANSWER

I have found my answer.

The JsonLayout has properties called batchHeader and batchFooter among others, but these two were the culprits for me.

For whatever reason, it still included them even though I was not doing batch log sending, so I removed both of them like so:

...
var layout = new log4javascript.JsonLayout(true, false);
layout.batchHeader = "";
layout.batchFooter = "";
...

This fixed my problem and I successfully POSTed logs to CouchDB.

Hope this helps someone else.