Skip to content

Instantly share code, notes, and snippets.

@techmechie
Created February 12, 2014 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techmechie/12d59a117d2db94d5901 to your computer and use it in GitHub Desktop.
Save techmechie/12d59a117d2db94d5901 to your computer and use it in GitHub Desktop.
Splunk SDK
//required node-modules
var express = require('express');
var splunk = require('./splunkSdkLogger');
var app = express(express.logger());
app.get('/', function(request, response) {
splunk.log({hello: "world"});
splunk.error("ERROR HAPPENED");
splunk.info(["useful", "info"]);
splunk.warn({"this": {"is": ["a", "warning"]}});
response.send('Started logging to Splunk');
});
var port = process.env.PORT || 4000;
app.listen(port, function() {
//splunk.log('info', 'appln started', {}, function(err, result) {return;});;
splunk.info('appln started');
console.log(process.env.LOG_LEVEL);
});
// ("ALL" = 4 | "INFO" = 3 | "WARN" = 2 | "ERROR" = 1 | "NONE" = 0) indicating the logging level.
process.env.LOG_LEVEL = 0;
var splunkjs = require('splunk-sdk');
//read CL arguments
var level = process.argv[2];
var scheme = process.argv[3];
var host = process.argv[4];
var port = process.argv[5];
var username = process.argv[6];
var password = process.argv[7];
var Splunklogger = splunkjs.Class.extend({
init: function(service, opts) {
this.service = service;
opts = opts || {};
this.params = {};
if (opts.index) this.params.index = opts.index;
if (opts.host) this.params.host = opts.host;
if (opts.source) this.params.source = opts.source;
if (opts.sourcetype) this.params.sourcetype = opts.sourcetype || "demo-Splunklogger";
if (!this.service) {
throw new Error("Must supply a valid service");
}
},
log: function(data) {
var message = {
__time: (new Date()).toUTCString(),
level: "LOG",
data: data
};
this.service.log(message, this.params);
console.log(data);
},
error: function(data) {
var message = {
__time: (new Date()).toUTCString(),
level: "ERROR",
data: data
};
this.service.log(message, this.params);
console.error(data);
},
info: function(data) {
var message = {
__time: (new Date()).toUTCString(),
level: "INFO",
data: data
};
this.service.log(message, this.params);
console.info(data);
},
warn: function(data) {
var message = {
__time: (new Date()).toUTCString(),
level: "WARN",
data: data
};
this.service.log(message, this.params);
console.warn(data);
},
/* setLevel: function (level) {
process.env.LOG_LEVEL = level;
}*/
});
var service = new splunkjs.Service({
username:username || 'admin',
password:password || 'admin123'
});
service.login(function(err, success) {
if (err) {
throw err;
console.log(err);
}
console.log("Login was successful: " + success);
});
// Create our Splunklogger
var logger = new Splunklogger(service, { sourcetype: "mylogger", source: "myapp" });
// ("ALL" = 4 | "INFO" = 3 | "WARN" = 2 | "ERROR" = 1 | "NONE" = 0) indicating the logging level.
//logger.setLevel(level);
module.exports = logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment