Class: SplunkLogger

SplunkLogger(requestOptions, config)

new SplunkLogger(requestOptions, config)

Constructs a SplunkLogger, to send events to Splunk Enterprise or Splunk Cloud via HTTP Event Collector. See defaultConfig for default configuration settings.
Parameters:
Name Type Description
requestOptions object Options to pass to needle.post(). See the needle documentation for all available options.
config object Configuration settings for a new SplunkLogger.
Properties
Name Type Attributes Default Description
token string HTTP Event Collector token, required.
name string <optional>
splunk-javascript-logging/0.11.1 Name for this logger.
host string <optional>
localhost Hostname or IP address of Splunk Enterprise or Splunk Cloud server.
maxRetries string <optional>
0 How many times to retry when HTTP POST to Splunk Enterprise or Splunk Cloud fails.
path string <optional>
/services/collector/event/1.0 URL path to send data to on the Splunk Enterprise or Splunk Cloud server.
protocol string <optional>
https Protocol used to communicate with the Splunk Enterprise or Splunk Cloud server, http or https.
port number <optional>
8088 HTTP Event Collector port on the Splunk Enterprise or Splunk Cloud server.
url string <optional>
URL string to pass to url.parse. This will try to set host, path, protocol, port, url. Any of these values will be overwritten if the corresponding property is set on config.
level string <optional>
info Logging level to use, will show up as the severity field of an event, see SplunkLogger.levels for common levels.
batchInterval number <optional>
0 Automatically flush events after this many milliseconds. When set to a non-positive value, events will be sent one by one. This setting is ignored when non-positive.
maxBatchSize number <optional>
0 Automatically flush events after the size of queued events exceeds this many bytes. This setting is ignored when non-positive.
maxBatchCount number <optional>
1 Automatically flush events after this many events have been queued. Defaults to flush immediately on sending an event. This setting is ignored when non-positive.
Properties:
Name Type Description
config object Configuration settings for this SplunkLogger instance.
serializedContextQueue Array.<object> Queue of serialized context objects to be sent to Splunk Enterprise or Splunk Cloud.
eventFormatter function Formats events, returning an event as a string, function(message, severity). Can be overwritten, the default event formatter will display event and severity as properties in a JSON object.
error function A callback function for errors: function(err, context). Defaults to console.log both values;
Source:
Throws:
Will throw an error if the config parameter is malformed.
Example
var SplunkLogger = require("splunk-logging").Logger;

var config = {
    token: "your-token-here",
    name: "my application",
    url: "https://splunk.local:8088"
};

var logger = new SplunkLogger(config);

Members

(readonly) levels :string

Enum for common logging levels.
Type:
  • string
Properties:
Name Type Description
DEBUG string
INFO string
WARN string
ERROR string
Default Value:
  • info
Source:

Methods

flush(callbackopt)

Manually send all events in this.serializedContextQueue to Splunk Enterprise or Splunk Cloud.
Parameters:
Name Type Attributes Description
callback function <optional>
A callback function: function(err, response, body).
Source:

send(context, callbackopt)

Sends or queues data to be sent based on batching settings. Default behavior is to send immediately.
Parameters:
Name Type Attributes Description
context object An object with at least the data property.
Properties
Name Type Attributes Default Description
message object | string | Array | number | bool Data to send to Splunk.
severity string <optional>
info Severity level of this event.
metadata object <optional>
Metadata for this event.
Properties
Name Type Attributes Description
host string <optional>
If not specified, Splunk Enterprise or Splunk Cloud will decide the value.
index string <optional>
The Splunk Enterprise or Splunk Cloud index to send data to. If not specified, Splunk Enterprise or Splunk Cloud will decide the value.
source string <optional>
If not specified, Splunk Enterprise or Splunk Cloud will decide the value.
sourcetype string <optional>
If not specified, Splunk Enterprise or Splunk Cloud will decide the value.
callback function <optional>
A callback function: function(err, response, body).
Source:
Throws:
Will throw an error if the context parameter is malformed.
Example
var SplunkLogger = require("splunk-logging").Logger;
var config = {
    token: "your-token-here"
}; 

var logger = new SplunkLogger(config);

// Payload to send to HTTP Event Collector.
var payload = {
    message: {
        temperature: "70F",
        chickenCount: 500
    },
    severity: "info",
    metadata: {
        source: "chicken coop",
        sourcetype: "httpevent",
        index: "main",
        host: "farm.local",
    }
}; 

// The callback is only used if maxBatchCount=1, or
// batching thresholds have been exceeded.
logger.send(payload, function(err, resp, body) {
    if (err) {
        console.log("error:", err);
    }
    // If successful, body will be { text: 'Success', code: 0 }
    console.log("body", body);
});