Documentation Menu

Adding request headers with beforeSend

The jsnlog.js client side library lets you add a beforeSend method to modify the log request before it goes to the server (including adding request headers), and track success or failure of the log request.

Adding beforeSend

You can add a beforeSend method in client side code:

  • to a single appender using its setOptions method;
  • to all appenders using the global setOptions method.

If there are appenders with their own beforeSend method and there is also a global beforeSend method:

  • If an appender has its own beforeSend method, only that method will run before the appender sends a log request.
  • The global beforeSend method is only used for appenders that do not have their own beforeSend method.

beforeSend method

The beforeSend method is called right before a log request is sent to the server. It receives these parameters:

beforeSend(xhr: XMLHttpRequest, json: any)
xhr XMLHttpRequest object used to send the request. Lets you add your own request headers, or add event handlers tracking success or failure of the log request to the server.
json Message to be sent. Lets you modify this message before it is sent. See below.

The message passed in via the json parameter has this format:

{
    r:  "request id", // Obsolete. May be empty string.
    lg: [
        { l: level, m: 'message', n: 'logger name', t: timestamp, u: event id },
        ...
    ]
}

Please note that a single JSON message to the server can contain multiple log messages. This is because the AJAX appender can be configured to batch messages, for example 2 log messages per JSON message to the server.

About the individual fields:

Field Description
request id Obsolete. Do not use.
level Numeric severity of the log message.
message Message to be logged.
logger name Name of the logger.
timestamp The time the message was logged by your JavaScript. This is the number of milliseconds since 1 January 1970 00:00:00 UTC, according to the client machine's clock.
event id Number that uniquely identifies the event within the request.

Caution: onreadystatechange event

JSNLog uses the onreadystatechange and status properties of the XMLHttpRequest object to detect whether the response to a log request was received. That allows it to deal with losing the Internet connection.

Your beforeSend method will be called after jsnlog.js has set its own onreadystatechange handler. This means that if you decide to set your own onreadystatechange handler, make it call the original onreadystatechange handler in addition to your own functionality, so jsnlog.js can continue handling connection lost situations.

Caution: cross domain log requests using CORS

This applies if you both:

In that case, additional configuration is needed so your log requests are accepted by the server (details).

Example: Add request headers

Ensure that an additional request header myheader with value myvalue is added to every log request to the server.

// Create beforeSend handler
var beforeSendExample = function (xhr) {
    xhr.setRequestHeader('myheader', 'myvalue');
};

// set global beforeSend method
JL.setOptions({
    "defaultBeforeSend": beforeSendExample
});