forked from adrianhall/winston-splunk-httplogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (144 loc) · 6.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (C) 2015-2017 Adrian Hall
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var util = require('util');
var winston = require('winston');
var SplunkLogger = require('splunk-logging').Logger;
/**
* A class that implements a Winston transport provider for Splunk HTTP Event Collector
*
* @param {object} config - Configuration settings for a new Winston transport
* @param {string} [config.level=info] - the minimum level to log
* @param {object} config.splunk - the Splunk Logger settings
* @param {string} config.splunk.token - the Splunk HTTP Event Collector token
* @param {string} [config.splunk.source=winston] - the source for the events sent to Splunk
* @param {string} [config.splunk.sourcetype=winston-splunk-logger] - the sourcetype for the events sent to Splunk
* @param {string} [config.splunk.host=localhost] - the Splunk HTTP Event Collector host
* @param {number} [config.splunk.maxRetries=0] - How many times to retry the splunk logger
* @param {number} [config.splunk.port=8088] - the Splunk HTTP Event Collector port
* @param {string} [config.splunk.path=/services/collector/event/1.0] - URL path to use
* @param {string} [config.splunk.protocol=https] - the protocol to use
* @param {string} [config.splunk.url] - URL string to pass to url.parse for the information
* @param {function} [config.splunk.eventFormatter] - Formats events, returning an event as a string, <code>function(message, severity)</code>
* @param {string} [config.level=info] - Logging level to use, will show up as the <code>severity</code> field of an event, see
* [SplunkLogger.levels]{@link SplunkLogger#levels} for common levels.
* @param {number} [config.batchInterval=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.
* @param {number} [config.maxBatchSize=0] - Automatically flush events after the size of queued
* events exceeds this many bytes. This setting is ignored when non-positive.
* @param {number} [config.maxBatchCount=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.
*
* @constructor
*/
var SplunkStreamEvent = function (config) {
winston.Transport.call(this, config);
/** @property {string} name - the name of the transport */
this.name = 'SplunkStreamEvent';
/** @property {string} level - the minimum level to log */
this.level = config.level || 'info';
// Verify that we actually have a splunk object and a token
if (!config.splunk || !config.splunk.token) {
throw new Error('Invalid Configuration: options.splunk is invalid');
}
// If source/sourcetype are mentioned in the splunk object, then store the
// defaults in this and delete from the splunk object
this.defaultMetadata = {
source: 'winston',
sourcetype: 'winston-splunk-logger'
};
if (config.splunk.source) {
this.defaultMetadata.source = config.splunk.source;
delete config.splunk.source;
}
if (config.splunk.sourcetype) {
this.defaultMetadata.sourcetype = config.splunk.sourcetype;
delete config.splunk.sourcetype;
}
// This gets around a problem with setting maxBatchCount
config.splunk.maxBatchCount = 1;
this.server = new SplunkLogger(config.splunk);
// Override the default event formatter
if (config.splunk.eventFormatter) {
this.server.eventFormatter = config.splunk.eventFormatter;
}
};
util.inherits(SplunkStreamEvent, winston.Transport);
/**
* Returns the configuration for this logger
* @returns {Object} Configuration for this logger.
* @public
*/
SplunkStreamEvent.prototype.config = function () {
return this.server.config;
};
/**
* Core logging method exposed to Winston.
*
* @function log
* @member SplunkStreamEvent
* @param {string} level - the level at which to log the message
* @param {string} msg - the message to log
* @param {object} [meta] - any additional meta data to attach
* @param {function} callback - Continuation to respond to when complete
*/
SplunkStreamEvent.prototype.log = function (level, msg, meta, callback) {
var self = this;
if (meta instanceof Error) {
meta = {
errmsg: meta.message,
name: meta.name,
stack: meta.stack
};
}
var payload = {
message: {
msg: msg
},
metadata: {
source: this.defaultMetadata.source,
sourcetype: this.defaultMetadata.sourcetype
},
severity: level
};
if (meta) {
if (meta instanceof Error) {
payload.message.meta = {
error: meta.message,
name: meta.name,
stack: meta.stack
};
} else if (Object.keys(meta).length) {
payload.message.meta = meta;
}
}
this.server.send(payload, function (err) {
if (err) {
self.emit('error', err);
}
self.emit('logged');
callback(null, true);
});
};
// Insert this object into the Winston transports list
winston.transports.SplunkStreamEvent = SplunkStreamEvent;
// Export the Winston transport
module.exports = exports = winston.transports.SplunkStreamEvent;