Splunk Logging for JavaScript

0.11.1 · active · verified Wed Apr 22

The `splunk-logging` package provides a straightforward JavaScript interface for Node.js applications to send log data to Splunk Enterprise or Splunk Cloud via the HTTP Event Collector (HEC). The current stable version is 0.11.1, with releases occurring periodically to address dependencies and minor enhancements rather than a fixed cadence. This library simplifies the process of sending events by handling connection details, batching, and retries automatically. Key features include configurable batching settings (interval, maximum count, and maximum size), support for custom event formatting, and options for SSL certificate validation. While primarily community-supported, it is an official Splunk project designed for seamless integration with Splunk HEC endpoints, making it a robust choice for Node.js-based logging into the Splunk ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates configuring a Splunk logger with environment variables for token and URL, then sending a structured JSON payload to the HTTP Event Collector, and logging the Splunk response.

import { Logger } from 'splunk-logging';

const config = {
    token: process.env.SPLUNK_HEC_TOKEN ?? '',
    url: process.env.SPLUNK_HEC_URL ?? 'https://splunk.local:8088'
};

const splunkLogger = new Logger(config);

// Optionally enable SSL certificate validation (highly recommended for production)
// splunkLogger.requestOptions.strictSSL = true;

const payload = {
    // Message can be anything; doesn't have to be an object
    message: {
        service: 'my-node-app',
        level: 'info',
        event_id: Math.floor(Math.random() * 1000000),
        details: 'User activity logged successfully.',
        timestamp: new Date().toISOString()
    },
    // Optional metadata
    metadata: {
        source: 'my-node-app',
        sourcetype: 'node:json',
        host: 'my-server'
    }
};

console.log('Sending payload', JSON.stringify(payload, null, 2));
splunkLogger.send(payload, function(err, resp, body) {
    if (err) {
        console.error('Error sending data to Splunk:', err);
        return;
    }
    // If successful, body will be { text: 'Success', code: 0 }
    console.log('Response from Splunk:', body);
});

view raw JSON →