Skip to content

Commit bbe5ff2

Browse files
author
Shakeel Mohamed
committed
Add docstrings. jsdoc will build docs during npm test
1 parent 69c28ae commit bbe5ff2

File tree

4 files changed

+181
-61
lines changed

4 files changed

+181
-61
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
coverage
22
node_modules
33
npm-debug.log
4-
test/config.json
4+
test/config.json
5+
.idea
6+
docs

index.js

+154-50
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,69 @@ var request = require("request");
33
var url = require("url");
44

55
/**
6-
* TODO: docs
7-
* default error handler
6+
* Default error handler for <code>SplunkLogger</code>.
7+
* Prints the <code>err</code> and <code>context</code> to console.
8+
*
9+
* @param {Error|string} err - The error message, or an <code>Error</code> object.
10+
* @param {object} [context] - The <code>context</code> of an event.
11+
* @private
812
*/
913
function _err(err, context) {
1014
console.log("ERROR:", err, " CONTEXT", context);
1115
}
1216

1317
/**
14-
* TODO: docs
18+
* Constructs a SplunkLogger, to send events to Splunk via the HTTP Event Collector.
19+
* See <code>defaultConfig</code> for default configuration settings.
20+
*
21+
* @example
22+
* var SplunkLogger = require("splunk-logging").Logger;
23+
*
24+
* var config = {
25+
* token: "your-token-here",
26+
* name: "my application",
27+
* host: "splunk.local",
28+
* batching: SplunkLogging.Logger.batchingModes.MANUAL
29+
* };
30+
*
31+
* var logger = new SplunkLogger(config);
1532
*
16-
* Constructor
33+
* @property {object} config - Configuration settings for this <code>SplunkLogger</code> instance.
34+
* @property {function[]} middlewares - Middleware functions to run before sending data to Splunk. See {@link SplunkLogger#use}
35+
* @property {object[]} contextQueue - Queue of <code>context</code> objects to be sent to Splunk.
36+
* @property {function} error - A callback function for errors: <code>function(err, context)</code>.
37+
* Defaults to <code>console.log</code> both values;
1738
*
18-
* Takes an object with keys:
19-
* token (required)
20-
* host (optional, defaults to localhost)
21-
* name (optional, defaults to splunk-javascript-logging)
22-
* etc.
39+
* @param {object} config - Configuration settings for a new [SplunkLogger]{@link SplunkLogger}.
40+
* @param {string} config.token - Splunk HTTP Event Collector token, required.
41+
* @param {string} [config.name=splunk-javascript-logging/0.8.0] - Name for this logger.
42+
* @param {string} [config.host=localhost] - Hostname or IP address of Splunk server.
43+
* @param {string} [config.path=/services/collector/event/1.0] - URL path to send data to on the Splunk server.
44+
* @param {string} [config.protocol=https] - Protocol used to communicate with the Splunk server, <code>http</code> or <code>https</code>.
45+
* @param {number} [config.port=8088] - HTTP Event Collector port on the Splunk server.
46+
* @param {string} [config.url] - URL string to pass to {@link https://nodejs.org/api/url.html#url_url_parsing|url.parse}. This will try to set
47+
* <code>host</code>, <code>path</code>, <code>protocol</code>, <code>port</code>, <code>url</code>. Any of these values will be overwritten if
48+
* the corresponding property is set on <code>config</code>.
49+
* @param {string} [config.level=info] - Logging level to use, will show up as the <code>severity</code> field of an event, see
50+
* [SplunkLogger.levels]{@link SplunkLogger#levels} for common levels.
51+
* @param {string} [config.batching=off] - Batching mode for sending events, see [SplunkLogger.batchingModes]{@link SplunkLogger#batchingModes}
52+
* for valid modes.
53+
* @constructor
54+
* @throws Will throw an error if the <code>config</code> parameter is malformed.
2355
*/
2456
var SplunkLogger = function(config) {
2557
this.config = this._initializeConfig(config);
26-
this.middlewares = []; // Array of callbacks to run between send and _sendEvents
27-
this.contextQueue = []; // Queue of contexts
58+
this.middlewares = [];
59+
this.contextQueue = [];
2860
this.error = _err;
2961
};
3062

3163
/**
32-
* TODO: docs, do we want to add other levels from bunyan logger?
33-
*
64+
* Enum for common logging levels.
65+
*
66+
* @default info
67+
* @readonly
68+
* @enum {string}
3469
*/
3570
SplunkLogger.prototype.levels = {
3671
DEBUG: "debug",
@@ -40,15 +75,16 @@ SplunkLogger.prototype.levels = {
4075
};
4176

4277
/**
43-
* TODO: add other batching modes
78+
* Enum for batching modes.
4479
*
45-
* Modes
46-
* - off: no batching (default)
47-
* - manual: must call flush manually to actually send events
48-
*
80+
* @default off
81+
* @readonly
82+
* @enum {string}
4983
*/
5084
SplunkLogger.prototype.batchingModes = {
85+
/** No batching. Make a request to Splunk every time <code>send()</code> is called. */
5186
OFF: "off",
87+
/** Events are manually batched. Invoke <code>flush()</code> manually to send all queued events in 1 request. */
5288
MANUAL: "manual"
5389
};
5490

@@ -70,9 +106,12 @@ var defaultRequestOptions = {
70106
};
71107

72108
/**
73-
* TODO: docs
74-
*
75-
* validates the config, throwing an error if it's horribly wrong
109+
* Sets up the <code>config</code> with any default properties, and/or
110+
* config properties set on <code>this.config</code>.
111+
*
112+
* @return {object} config
113+
* @private
114+
* @throws Will throw an error if the <code>config</code> parameter is malformed.
76115
*/
77116
SplunkLogger.prototype._initializeConfig = function(config) {
78117
// Copy over the instance config
@@ -141,9 +180,13 @@ SplunkLogger.prototype._initializeConfig = function(config) {
141180
};
142181

143182
/**
144-
* TODO: docs
145-
*
146-
* NOTE: This function calls _initializeConfig
183+
* Initializes request options.
184+
*
185+
* @param {object} config
186+
* @param {object} options - Options to pass to <code>{@link https://github.com/request/request#requestpost|request.post()}</code>.
187+
* See the {@link http://github.com/request/request|request documentation} for all available options.
188+
* @returns {object} requestOptions
189+
* @private
147190
*/
148191
SplunkLogger.prototype._initializeRequestOptions = function(config, options) {
149192
var ret = {};
@@ -168,8 +211,10 @@ SplunkLogger.prototype._initializeRequestOptions = function(config, options) {
168211
};
169212

170213
/**
171-
* TODO: docs
214+
* Throws an error if data is <code>undefined</code> or <code>null</code>.
172215
*
216+
* @private
217+
* @throws Will throw an error if the <code>data</code> parameter is malformed.
173218
*/
174219
SplunkLogger.prototype._initializeData = function(data) {
175220
if (typeof data === "undefined" || data === null) {
@@ -179,10 +224,12 @@ SplunkLogger.prototype._initializeData = function(data) {
179224
};
180225

181226
/**
182-
* TODO: docs
183-
*
184-
* Takes the context object & tries to initialize the
185-
* config and request options.
227+
* Initializes a context.
228+
*
229+
* @param context
230+
* @returns {object} context
231+
* @throws Will throw an error if the <code>context</code> parameter is malformed.
232+
* @private
186233
*/
187234
SplunkLogger.prototype._initializeContext = function(context) {
188235
if (!context) {
@@ -209,15 +256,12 @@ SplunkLogger.prototype._initializeContext = function(context) {
209256
};
210257

211258
/**
212-
* TODO: docs
259+
* Takes anything and puts it in a JS object for the event/1.0 Splunk HTTP Event Collector format.
213260
*
214-
* Takes anything and puts it in a JS object for the event/1.0 EC format
215-
* If context has any of the following properties set, they will overwrite the token's settings
216-
*
217-
* - host
218-
* - source
219-
* - sourcetype
220-
* - index (TODO: can index by changed on the fly?)
261+
* @param {object} context
262+
* @returns {{time: string, event: {message: *, severity: (string|SplunkLogger.levels)}}}
263+
* @private
264+
* @throws Will throw an error if the <code>context</code> parameter is malformed.
221265
*/
222266
SplunkLogger.prototype._makeBody = function(context) {
223267
if (!context) {
@@ -253,10 +297,26 @@ SplunkLogger.prototype._makeBody = function(context) {
253297
};
254298

255299
/**
256-
* TODO: docs
257-
*
258-
* Adds a middleware function to run before sending the
300+
* Adds an express-like middleware function to run before sending the
259301
* data to Splunk.
302+
* Multiple middleware functions can be used, they will be executed
303+
* in the order they are added.
304+
*
305+
* This function is a wrapper around <code>this.middlewares.push()</code>.
306+
*
307+
* @example
308+
* var SplunkLogger = require("splunk-logging").Logger;
309+
*
310+
* var Logger = new SplunkLogger({token: "your-token-here"});
311+
* Logger.use(function(context, next) {
312+
* context.data.additionalProperty = "Add this before sending the data";
313+
* next(null, context);
314+
* });
315+
*
316+
* @param {function} middleware - A middleware function: <code>function(context, next){}</code>.
317+
* It must call <code>next(error, context)</code> to continue.
318+
* @public
319+
* @throws Will throw an error if <code>middleware</code> is not a <code>function</code>.
260320
*/
261321
SplunkLogger.prototype.use = function(middleware) {
262322
if (!middleware || typeof middleware !== "function") {
@@ -268,10 +328,11 @@ SplunkLogger.prototype.use = function(middleware) {
268328
};
269329

270330
/**
271-
* TODO: docs
272-
*
273331
* Makes an HTTP POST to the configured server.
274-
* Any config not specified will be set to the default configuration.
332+
*
333+
* @param context
334+
* @param {function} callback - A callback function: <code>function(err, response, body)</code>
335+
* @private
275336
*/
276337
SplunkLogger.prototype._sendEvents = function(context, callback) {
277338
callback = callback || /* istanbul ignore next*/ function(){};
@@ -296,9 +357,51 @@ SplunkLogger.prototype._sendEvents = function(context, callback) {
296357
};
297358

298359
/**
299-
* TODO: docs
300-
* Takes config context, anything, & a callback(err, resp, body)
360+
* Sends or queues data to be sent based on <code>context.config.batching</code>.
361+
* Default behavior is to send immediately.
362+
*
363+
* @example
364+
* var SplunkLogger = require("splunk-logging").Logger;
365+
* var config = {
366+
* token: "your-token-here"
367+
* };
301368
*
369+
* var logger = new SplunkLogger(config);
370+
*
371+
* // Payload to send to Splunk's Event Collector
372+
* var payload = {
373+
* data: {
374+
* temperature: "70F",
375+
* chickenCount: 500
376+
* },
377+
* source: "chicken coop",
378+
* sourcetype: "chicken feeder",
379+
* index: "main",
380+
* host: "farm.local",
381+
* severity: "info"
382+
* };
383+
*
384+
* logger.send(payload, function(err, resp, body) {
385+
* if (err) {
386+
* console.log("error:", err);
387+
* }
388+
* console.log("body", body); // body { text: 'Success', code: 0 }
389+
* });
390+
*
391+
* @param {object} context - An object with at least the <code>data</code> property.
392+
* @param {(object|string|Array|number|bool)} context.data - Data to send to Splunk.
393+
* @param {object} [context.requestOptions] - Additional options to pass to <code>{@link https://github.com/request/request#requestpost|request.post()}</code>.
394+
* See the {@link http://github.com/request/request|request documentation} for all available options.
395+
* @param {string} [context.severity=info] - Severity level of this event.
396+
* @param {object} [context.config] - See {@link SplunkLogger} for default values.
397+
* @param {string} [context.host] - If not specified, Splunk will decide the value.
398+
* @param {string} [context.index] - The token set in <code>config.token</code> must be configured to send to this <code>index</code>.
399+
* If not specified, Splunk will decide the value.
400+
* @param {string} [context.source] - If not specified, Splunk will decide the value.
401+
* @param {string} [context.sourcetype] - If not specified, Splunk will decide the value.
402+
* @param {function} [callback] - A callback function: <code>function(err, response, body)</code>.
403+
* @throws Will throw an error if the <code>context</code> parameter is malformed.
404+
* @public
302405
*/
303406
SplunkLogger.prototype.send = function (context, callback) {
304407
callback = callback || function(){};
@@ -319,12 +422,13 @@ SplunkLogger.prototype.send = function (context, callback) {
319422
};
320423

321424
/**
322-
* TODO: docs
425+
* Manually send events in <code>this.contextQueue</code> to Splunk, after
426+
* chaining any <code>functions</code> in <code>this.middlewares</code>.
427+
* Batching settings will be used from <code>this.config.batching</code>,
428+
* ignoring batching settings every on every <code>context</code> in <code>this.contextQueue</code>.
323429
*
324-
* Batching settings will be used from this.config.batching,
325-
* ignoring any possible this.contextQueue[i].config.batching
326-
* values.
327-
*
430+
* @param {function} [callback] - A callback function: <code>function(err, response, body)</code>.
431+
* @public
328432
*/
329433
SplunkLogger.prototype.flush = function (callback) {
330434
callback = callback || function(){};

package.json

+4-2
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",
8+
"pretest": "jshint *.js test && jsdoc -d docs . ",
99
"test": "istanbul cover _mocha -- -R spec"
1010
},
1111
"repository": {
@@ -14,6 +14,7 @@
1414
},
1515
"keywords": [
1616
"splunk",
17+
"HTTP",
1718
"event",
1819
"collector",
1920
"logging",
@@ -24,7 +25,7 @@
2425
"email": "[email protected]",
2526
"url": "http://dev.splunk.com"
2627
},
27-
"license": "Apache 2.0",
28+
"license": "Apache-2.0",
2829
"engine": {
2930
"node": ">=0.10.0"
3031
},
@@ -33,6 +34,7 @@
3334
},
3435
"devDependencies": {
3536
"istanbul": "0.3.17",
37+
"jsdoc": "3.3.2",
3638
"jshint": "<2.5.0",
3739
"mocha": "2.2.5"
3840
}

0 commit comments

Comments
 (0)