Skip to content

Commit d3c329f

Browse files
author
Shakeel Mohamed
committed
Add examples
1 parent 3b94884 commit d3c329f

File tree

4 files changed

+261
-1
lines changed

4 files changed

+261
-1
lines changed

examples/basic.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2015 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
/**
18+
* This example shows basic usage of the SplunkLogger.
19+
*/
20+
21+
// Change to require("splunk-logging").Logger;
22+
var SplunkLogger = require("../index").Logger;
23+
24+
/**
25+
* Only the token property is required.
26+
* Defaults are listed explicitly.
27+
*
28+
* Alternatively, specify config.url like so:
29+
*
30+
* "https://localhost:8088/services/collector/event/1.0"
31+
*/
32+
var config = {
33+
token: "your-token-here", // TODO: remove the token
34+
host: "localhost",
35+
path: "/services/collector/event/1.0",
36+
protocol: "https",
37+
port: 8088,
38+
level: "info",
39+
autoFlush: true
40+
};
41+
42+
// Create a new logger
43+
var Logger = new SplunkLogger(config);
44+
45+
Logger.error = function(err, context) {
46+
// Handle errors here
47+
console.log("error", err, "context", context);
48+
};
49+
50+
// Define the payload to send to Splunk's Event Collector
51+
var payload = {
52+
// Message can be anything, doesn't have to be an object
53+
message: {
54+
temperature: "70F",
55+
chickenCount: 500
56+
},
57+
// Metadata is optional
58+
metadata: {
59+
source: "chicken coop",
60+
sourcetype: "httpevent",
61+
index: "main",
62+
host: "farm.local",
63+
},
64+
// Severity is also optional
65+
severity: "info"
66+
};
67+
68+
console.log("Sending payload", payload);
69+
Logger.send(payload, function(err, resp, body) {
70+
// If successful, body will be { text: 'Success', code: 0 }
71+
console.log("Response from Splunk", body);
72+
});

examples/batching.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2015 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
/**
18+
* This example shows how to batch events with the
19+
* SplunkLogger by manually calling flush.
20+
*
21+
* By default autoFlush is enabled, this means
22+
* an HTTP request is made each time send()
23+
* is called.
24+
*
25+
* By disabling autoFlush, events will be queued
26+
* until flush() is called.
27+
*/
28+
29+
// Change to require("splunk-logging").Logger;
30+
var SplunkLogger = require("../index").Logger;
31+
32+
/**
33+
* Only the token property is required.
34+
*
35+
* Here, autoFlush is set to false
36+
*/
37+
var config = {
38+
token: "your-token-here", // TODO: remove the token
39+
host: "localhost",
40+
path: "/services/collector/event/1.0",
41+
protocol: "https",
42+
port: 8088,
43+
level: "info",
44+
autoFlush: false
45+
};
46+
47+
// Create a new logger
48+
var Logger = new SplunkLogger(config);
49+
50+
Logger.error = function(err, context) {
51+
// Handle errors here
52+
console.log("error", err, "context", context);
53+
};
54+
55+
// Define the payload to send to Splunk's Event Collector
56+
var payload = {
57+
// Message can be anything, doesn't have to be an object
58+
message: {
59+
temperature: "70F",
60+
chickenCount: 500
61+
},
62+
// Metadata is optional
63+
metadata: {
64+
source: "chicken coop",
65+
sourcetype: "httpevent",
66+
index: "main",
67+
host: "farm.local",
68+
},
69+
// Severity is also optional
70+
severity: "info"
71+
};
72+
73+
console.log("Queuing payload", payload);
74+
// Don't need a callback here
75+
Logger.send(payload);
76+
77+
var payload2 = {
78+
message: {
79+
temperature: "75F",
80+
chickenCount: 600,
81+
note: "New chickens have arrived!"
82+
},
83+
metadata: payload.metadata
84+
};
85+
86+
console.log("Queuing second payload", payload2);
87+
// Don't need a callback here
88+
Logger.send(payload2);
89+
90+
/**
91+
* Since autoFlush is disabled, call flush manually.
92+
* This will send both payloads in a single
93+
* HTTP request.
94+
*
95+
* The same callback can work for send() and flush().
96+
*/
97+
Logger.flush(function(err, resp, body) {
98+
// If successful, body will be { text: 'Success', code: 0 }
99+
console.log("Response from Splunk", body);
100+
});

examples/middleware.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2015 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
/**
18+
* This example shows how to use middleware with the SplunkLogger.
19+
*/
20+
21+
// Change to require("splunk-logging").Logger;
22+
var SplunkLogger = require("../index").Logger;
23+
24+
/**
25+
* Only the token property is required.
26+
* Defaults are listed explicitly.
27+
*
28+
* Alternatively, specify config.url like so:
29+
*
30+
* "https://localhost:8088/services/collector/event/1.0"
31+
*/
32+
var config = {
33+
token: "your-token-here", // TODO: remove the token
34+
host: "localhost",
35+
path: "/services/collector/event/1.0",
36+
protocol: "https",
37+
port: 8088,
38+
level: "info",
39+
autoFlush: true
40+
};
41+
42+
// Create a new logger
43+
var Logger = new SplunkLogger(config);
44+
45+
Logger.error = function(err, context) {
46+
// Handle errors here
47+
console.log("error", err, "context", context);
48+
};
49+
50+
// Add a middleware function
51+
Logger.use(function(context, next) {
52+
console.log("Message before middleware", context.message);
53+
54+
// Add a property to the message if it's an object
55+
if (typeof context.message === "object") {
56+
context.message.nestedValue = {
57+
b00l: true,
58+
another: "string"
59+
};
60+
}
61+
62+
console.log("Message after middleware", context.message);
63+
next(null, context);
64+
});
65+
66+
// Define the payload to send to Splunk's Event Collector
67+
var payload = {
68+
// Message can be anything, doesn't have to be an object
69+
message: {
70+
temperature: "70F",
71+
chickenCount: 500
72+
},
73+
// Metadata is optional
74+
metadata: {
75+
source: "chicken coop",
76+
sourcetype: "httpevent",
77+
index: "main",
78+
host: "farm.local"
79+
},
80+
// Severity is also optional
81+
severity: "info"
82+
};
83+
84+
console.log("Sending payload", payload);
85+
Logger.send(payload, function(err, resp, body) {
86+
// If successful, body will be { text: 'Success', code: 0 }
87+
console.log("Response from Splunk", body);
88+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "http://dev.splunk.com",
66
"main": "index.js",
77
"scripts": {
8-
"pretest": "jshint *.js test && jsdoc -d docs .",
8+
"pretest": "jshint *.js test examples && jsdoc -d docs .",
99
"test": "istanbul cover _mocha -- -R spec"
1010
},
1111
"repository": {

0 commit comments

Comments
 (0)