How to configure retries with Splunk logging for JavaScript

This topic demonstrates how to configure retries upon connection errors with SplunkLogger. The retry.js example included in the examples directory of the Splunk logging for JavaScript package provides a sample implementation. It has also been pasted below.

Note: The examples are not installed when using the npm installation method. To obtain copies of the examples, download the Splunk logging for JavaScript package.

Example walkthrough

This example includes the logic to configure connection retries if there are connection errors that occur upon sending a request to HTTP Event Collector on Splunk Enterprise or Splunk Cloud. To see the retry logic working, you should disable your network connection, run the sample, and then re-enable the connection. Once the connection is enabled, the events will be sent to Splunk Enterprise or Splunk Cloud.

First, we declare a SplunkLogger variable based on the library's Logger object.

Then, we declare a config variable to store the configuration information for the Splunk Enterprise instance or Splunk Cloud server. In the retry.js example, we've added the token, url, and level properties, as described in the basic.js example. We've also added the maxRetries property, which determines the maximum number of times the request will be retried if it encounters a connection error. In this case, we've set it equal to 10.

Next, we create a new logger (Logger), and then add an error handler (Logger.error).

Then we define the event payload in the payload variable, as described in the basic.js example.

Finally, we send the payload (Logger.send) and log the response from Splunk Enterprise or Splunk Cloud.


 * Copyright 2015 Splunk, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License"): you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.

 * This example shows how to configure retries with SplunkLogger.

// Change to require("splunk-logging").Logger;
var SplunkLogger = require("../index").Logger;

 * Only the token property is required.
 * Here we've set maxRetries to 10,
 * If there are any connection errors the request to Splunk will
 * be retried up to 10 times.
 * The default is 0.
var config = {
    token: "your-token-here",
    url: "https://localhost:8088",
    level: "info",
    maxRetries: 10

// Create a new logger
var Logger = new SplunkLogger(config);

Logger.error = function(err, context) {
    // Handle errors here
    console.log("error", err, "context", context);

// Define the payload to send to HTTP Event Collector
var payload = {
    // Message can be anything, doesn't have to be an object
    message: {
        temperature: "70F",
        chickenCount: 500
    // Metadata is optional
    metadata: {
        source: "chicken coop",
        sourcetype: "httpevent",
        index: "main",
        host: "farm.local"
    // Severity is also optional
    severity: "info"

console.log("Sending payload", payload);
Logger.send(payload, function(err, resp, body) {
    // If successful, body will be { text: 'Success', code: 0 }
    console.log("Response from Splunk", body);