@@ -3,34 +3,69 @@ var request = require("request");
3
3
var url = require ( "url" ) ;
4
4
5
5
/**
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
8
12
*/
9
13
function _err ( err , context ) {
10
14
console . log ( "ERROR:" , err , " CONTEXT" , context ) ;
11
15
}
12
16
13
17
/**
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);
15
32
*
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;
17
38
*
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.
23
55
*/
24
56
var SplunkLogger = function ( config ) {
25
57
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 = [ ] ;
28
60
this . error = _err ;
29
61
} ;
30
62
31
63
/**
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}
34
69
*/
35
70
SplunkLogger . prototype . levels = {
36
71
DEBUG : "debug" ,
@@ -40,15 +75,16 @@ SplunkLogger.prototype.levels = {
40
75
} ;
41
76
42
77
/**
43
- * TODO: add other batching modes
78
+ * Enum for batching modes.
44
79
*
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}
49
83
*/
50
84
SplunkLogger . prototype . batchingModes = {
85
+ /** No batching. Make a request to Splunk every time <code>send()</code> is called. */
51
86
OFF : "off" ,
87
+ /** Events are manually batched. Invoke <code>flush()</code> manually to send all queued events in 1 request. */
52
88
MANUAL : "manual"
53
89
} ;
54
90
@@ -70,9 +106,12 @@ var defaultRequestOptions = {
70
106
} ;
71
107
72
108
/**
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.
76
115
*/
77
116
SplunkLogger . prototype . _initializeConfig = function ( config ) {
78
117
// Copy over the instance config
@@ -141,9 +180,13 @@ SplunkLogger.prototype._initializeConfig = function(config) {
141
180
} ;
142
181
143
182
/**
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
147
190
*/
148
191
SplunkLogger . prototype . _initializeRequestOptions = function ( config , options ) {
149
192
var ret = { } ;
@@ -168,8 +211,10 @@ SplunkLogger.prototype._initializeRequestOptions = function(config, options) {
168
211
} ;
169
212
170
213
/**
171
- * TODO: docs
214
+ * Throws an error if data is <code>undefined</code> or <code>null</code>.
172
215
*
216
+ * @private
217
+ * @throws Will throw an error if the <code>data</code> parameter is malformed.
173
218
*/
174
219
SplunkLogger . prototype . _initializeData = function ( data ) {
175
220
if ( typeof data === "undefined" || data === null ) {
@@ -179,10 +224,12 @@ SplunkLogger.prototype._initializeData = function(data) {
179
224
} ;
180
225
181
226
/**
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
186
233
*/
187
234
SplunkLogger . prototype . _initializeContext = function ( context ) {
188
235
if ( ! context ) {
@@ -209,15 +256,12 @@ SplunkLogger.prototype._initializeContext = function(context) {
209
256
} ;
210
257
211
258
/**
212
- * TODO: docs
259
+ * Takes anything and puts it in a JS object for the event/1.0 Splunk HTTP Event Collector format.
213
260
*
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.
221
265
*/
222
266
SplunkLogger . prototype . _makeBody = function ( context ) {
223
267
if ( ! context ) {
@@ -253,10 +297,26 @@ SplunkLogger.prototype._makeBody = function(context) {
253
297
} ;
254
298
255
299
/**
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
259
301
* 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>.
260
320
*/
261
321
SplunkLogger . prototype . use = function ( middleware ) {
262
322
if ( ! middleware || typeof middleware !== "function" ) {
@@ -268,10 +328,11 @@ SplunkLogger.prototype.use = function(middleware) {
268
328
} ;
269
329
270
330
/**
271
- * TODO: docs
272
- *
273
331
* 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
275
336
*/
276
337
SplunkLogger . prototype . _sendEvents = function ( context , callback ) {
277
338
callback = callback || /* istanbul ignore next*/ function ( ) { } ;
@@ -296,9 +357,51 @@ SplunkLogger.prototype._sendEvents = function(context, callback) {
296
357
} ;
297
358
298
359
/**
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
+ * };
301
368
*
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
302
405
*/
303
406
SplunkLogger . prototype . send = function ( context , callback ) {
304
407
callback = callback || function ( ) { } ;
@@ -319,12 +422,13 @@ SplunkLogger.prototype.send = function (context, callback) {
319
422
} ;
320
423
321
424
/**
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>.
323
429
*
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
328
432
*/
329
433
SplunkLogger . prototype . flush = function ( callback ) {
330
434
callback = callback || function ( ) { } ;
0 commit comments