Splunk HTTP Event Collector Stream for Bunyan
raw JSON →The `splunk-bunyan-logger` package provides a Bunyan stream specifically designed to send structured log data to Splunk Enterprise or Splunk Cloud via the HTTP Event Collector (HEC). It acts as a bridge, allowing applications using the Bunyan JSON logging library to seamlessly integrate with Splunk's data ingestion capabilities. The current stable version is 0.11.0. While no explicit release cadence is provided, the recent update (0.11.0) indicates active maintenance, with the last update fixing deprecated internal dependencies. Key differentiators include its tight integration with Bunyan's streaming architecture, batching capabilities for efficient data transfer, and support for custom event formatting. It requires Node.js v4 or later (tested with v10 and v14) and Splunk Enterprise 6.3.0 or later, or Splunk Cloud.
Common errors
error TypeError: splunkBunyan.createStream is not a function ↓
const splunkBunyan = require('splunk-bunyan-logger'); and then access splunkBunyan.createStream(config);. Do not use const { createStream } = require('splunk-bunyan-logger'); or ESM import { createStream } from 'splunk-bunyan-logger'; error Error: self signed certificate in certificate chain ↓
strictSSL (not recommended for production) by splunkStream.logger.requestOptions.strictSSL = false; or, preferably, configure Node.js to trust your self-signed certificate by setting the NODE_EXTRA_CA_CERTS environment variable to the path of your CA certificate file. error Error: connect ECONNREFUSED <splunk-host>:<port> ↓
config.url is correct (including port). Check network connectivity from your application server to the Splunk HEC host, and ensure Splunk HEC is enabled and listening on the specified port. Warnings
gotcha SSL certificate validation is disabled by default (`strictSSL = false`). For production environments, it is crucial to explicitly enable SSL certificate validation by setting `logger.requestOptions.strictSSL = true` to prevent man-in-the-middle attacks. ↓
breaking Version 0.11.0 replaced the underlying HTTP client from `request` to `needle`. If your application relied on specific `request` library options passed via `requestOptions`, these might behave differently or no longer be supported. Review `needle`'s documentation for compatibility. ↓
gotcha The library primarily uses CommonJS `require()` syntax. While modern Node.js versions support ESM, directly using `import` statements for `splunk-bunyan-logger` may not work as expected without proper transpilation or a CJS-to-ESM wrapper. ↓
gotcha The Splunk HTTP Event Collector (HEC) requires a valid token and a reachable URL. Incorrect configuration can lead to logs not being sent to Splunk, often failing silently unless an error handler is attached to the stream. ↓
Install
npm install splunk-bunyan-logger yarn add splunk-bunyan-logger pnpm add splunk-bunyan-logger Imports
- splunkBunyan wrong
import splunkBunyan from 'splunk-bunyan-logger';correctconst splunkBunyan = require('splunk-bunyan-logger'); - createStream wrong
const { createStream } = require('splunk-bunyan-logger');correctconst splunkBunyan = require('splunk-bunyan-logger'); const splunkStream = splunkBunyan.createStream(config); - Logger wrong
const Logger = require('bunyan').Logger;correctconst bunyan = require('bunyan'); const Logger = bunyan.createLogger({ name: 'my logger', streams: [splunkStream] });
Quickstart
const bunyan = require("bunyan");
const splunkBunyan = require("splunk-bunyan-logger");
const config = {
token: process.env.SPLUNK_HEC_TOKEN ?? "your-splunk-hec-token-here",
url: process.env.SPLUNK_HEC_URL ?? "https://splunk.local:8088"
};
// Create a SplunkStream instance
const splunkStream = splunkBunyan.createStream(config);
// Optional: Add an error handler for the stream
splunkStream.on("error", (err, context) => {
// Log error, update metric, etc.
console.error("Splunk HEC Logger Error:", err, context);
});
// Create a Bunyan Logger instance, adding the SplunkStream
const Logger = bunyan.createLogger({
name: "my-application-logger",
streams: [
{ stream: process.stdout, level: "info" }, // Also log to console
splunkStream
]
});
const payload = {
// Message can be anything, doesn't have to be an object
message: {
temperature: "72F",
humidity: "60%",
sensorId: "TH-001"
},
// Custom metadata for Splunk (optional)
sourcetype: "_json",
source: "my-node-app"
};
console.log("Sending payload to Splunk HEC...");
Logger.info(payload, "Environment sensor data collected.");
Logger.warn("Potential anomaly detected in sensor readings.");
console.log("Payload sent (asynchronously).");