|
1 | 1 | /* |
2 | | - * Copyright (C) 2015-2017 Adrian Hall |
| 2 | + * Copyright (C) 2015-2021 Adrian Hall |
3 | 3 | * |
4 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | 5 | * of this software and associated documentation files (the "Software"), to deal |
|
19 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
20 | 20 | * THE SOFTWARE. |
21 | 21 | */ |
22 | | -var util = require('util'); |
23 | | -var winston = require('winston'); |
24 | | -var isStream = require('is-stream'); |
25 | | -var SplunkLogger = require('splunk-logging').Logger; |
| 22 | +const winston = require('winston'); |
| 23 | +const isStream = require('is-stream'); |
| 24 | +const SplunkLogger = require('splunk-logging').Logger; |
26 | 25 |
|
27 | 26 | if (!isStream(new winston.Transport())) { |
28 | 27 | console.error('winston-splunk-httplogger >= 2.0.0 is not compatiable with winston < 3.0.0.'); |
@@ -58,102 +57,102 @@ if (!isStream(new winston.Transport())) { |
58 | 57 | * |
59 | 58 | * @constructor |
60 | 59 | */ |
61 | | -var SplunkStreamEvent = function (config) { |
62 | | - winston.Transport.call(this, config); |
| 60 | +class SplunkStreamEvent extends winston.Transport { |
| 61 | + constructor (config) { |
| 62 | + super(config); |
63 | 63 |
|
64 | | - /** @property {string} name - the name of the transport */ |
65 | | - this.name = 'SplunkStreamEvent'; |
| 64 | + /** @property {string} name - the name of the transport */ |
| 65 | + this.name = 'SplunkStreamEvent'; |
66 | 66 |
|
67 | | - /** @property {string} level - the minimum level to log */ |
68 | | - this.level = config.level || 'info'; |
69 | | - this.exitOnError = typeof config.exitOnError != 'boolean' || config.exitOnError; |
| 67 | + /** @property {string} level - the minimum level to log */ |
| 68 | + this.level = config.level || 'info'; |
| 69 | + this.exitOnError = typeof config.exitOnError !== 'boolean' || config.exitOnError; |
70 | 70 |
|
71 | | - // Verify that we actually have a splunk object and a token |
72 | | - if (!config.splunk || !config.splunk.token) { |
73 | | - throw new Error('Invalid Configuration: options.splunk is invalid'); |
74 | | - } |
| 71 | + // Verify that we actually have a splunk object and a token |
| 72 | + if (!config.splunk || !config.splunk.token) { |
| 73 | + throw new Error('Invalid Configuration: options.splunk is invalid'); |
| 74 | + } |
75 | 75 |
|
76 | | - // If source/sourcetype are mentioned in the splunk object, then store the |
77 | | - // defaults in this and delete from the splunk object |
78 | | - this.defaultMetadata = { |
79 | | - source: 'winston', |
80 | | - sourcetype: 'winston-splunk-logger', |
81 | | - index: 'winston-index' |
82 | | - }; |
83 | | - if (config.splunk.source) { |
84 | | - this.defaultMetadata.source = config.splunk.source; |
85 | | - delete config.splunk.source; |
86 | | - } |
87 | | - if (config.splunk.sourcetype) { |
88 | | - this.defaultMetadata.sourcetype = config.splunk.sourcetype; |
89 | | - delete config.splunk.sourcetype; |
90 | | - } |
91 | | - if (config.splunk.index) { |
92 | | - this.defaultMetadata.index = config.splunk.index; |
93 | | - delete config.splunk.index; |
94 | | - } |
| 76 | + // If source/sourcetype are mentioned in the splunk object, then store the |
| 77 | + // defaults in this and delete from the splunk object |
| 78 | + this.defaultMetadata = { |
| 79 | + source: 'winston', |
| 80 | + sourcetype: 'winston-splunk-logger', |
| 81 | + index: 'winston-index' |
| 82 | + }; |
| 83 | + if (config.splunk.source) { |
| 84 | + this.defaultMetadata.source = config.splunk.source; |
| 85 | + delete config.splunk.source; |
| 86 | + } |
| 87 | + if (config.splunk.sourcetype) { |
| 88 | + this.defaultMetadata.sourcetype = config.splunk.sourcetype; |
| 89 | + delete config.splunk.sourcetype; |
| 90 | + } |
| 91 | + if (config.splunk.index) { |
| 92 | + this.defaultMetadata.index = config.splunk.index; |
| 93 | + delete config.splunk.index; |
| 94 | + } |
95 | 95 |
|
96 | | - // This gets around a problem with setting maxBatchCount |
97 | | - config.splunk.maxBatchCount = 1; |
98 | | - this.server = new SplunkLogger(config.splunk); |
| 96 | + // This gets around a problem with setting maxBatchCount |
| 97 | + config.splunk.maxBatchCount = 1; |
| 98 | + this.server = new SplunkLogger(config.splunk); |
99 | 99 |
|
100 | | - // Override the default event formatter |
101 | | - if (config.splunk.eventFormatter) { |
102 | | - this.server.eventFormatter = config.splunk.eventFormatter; |
| 100 | + // Override the default event formatter |
| 101 | + if (config.splunk.eventFormatter) { |
| 102 | + this.server.eventFormatter = config.splunk.eventFormatter; |
| 103 | + } |
103 | 104 | } |
104 | | -}; |
105 | | - |
106 | | -util.inherits(SplunkStreamEvent, winston.Transport); |
107 | 105 |
|
108 | | -/** |
109 | | - * Returns the configuration for this logger |
110 | | - * @returns {Object} Configuration for this logger. |
111 | | - * @public |
112 | | - */ |
113 | | -SplunkStreamEvent.prototype.config = function () { |
114 | | - return this.server.config; |
115 | | -}; |
116 | | - |
117 | | -/** |
118 | | - * Core logging method exposed to Winston. |
119 | | - * |
120 | | - * @function log |
121 | | - * @member SplunkStreamEvent |
122 | | - * @param {object} [info] - the log message info object |
123 | | - * @param {function} callback - Continuation to respond to when complete |
124 | | - */ |
125 | | -SplunkStreamEvent.prototype.log = function (info, callback) { |
126 | | - var self = this; |
127 | | - var level = info[Symbol.for('level')]; |
128 | | - var msg = info['message']; |
129 | | - var meta = Object.assign({}, info); |
130 | | - |
131 | | - delete meta[Symbol.for('level')]; |
132 | | - delete meta[Symbol.for('message')]; |
133 | | - var splunkInfo = info.splunk || {}; |
134 | | - |
135 | | - var payload = { |
136 | | - message: { |
137 | | - msg: msg |
138 | | - }, |
139 | | - metadata: Object.assign({}, this.defaultMetadata, splunkInfo), |
140 | | - severity: level |
141 | | - }; |
142 | | - |
143 | | - if (meta) { |
144 | | - if (Object.keys(meta).length) { |
145 | | - payload.message.meta = meta; |
146 | | - } |
| 106 | + /** |
| 107 | + * Returns the configuration for this logger |
| 108 | + * @returns {Object} Configuration for this logger. |
| 109 | + * @public |
| 110 | + */ |
| 111 | + config () { |
| 112 | + return this.server.config; |
147 | 113 | } |
148 | 114 |
|
149 | | - this.server.send(payload, function (err) { |
150 | | - if (err && self.exitOnError) { |
151 | | - self.emit('error', err); |
| 115 | + /** |
| 116 | + * Core logging method exposed to Winston. |
| 117 | + * |
| 118 | + * @function log |
| 119 | + * @member SplunkStreamEvent |
| 120 | + * @param {object} [info] - the log message info object |
| 121 | + * @param {function} callback - Continuation to respond to when complete |
| 122 | + */ |
| 123 | + log (info, callback) { |
| 124 | + const self = this; |
| 125 | + const level = info[Symbol.for('level')]; |
| 126 | + const msg = info.message; |
| 127 | + const meta = Object.assign({}, info); |
| 128 | + delete meta[Symbol.for('level')]; |
| 129 | + delete meta[Symbol.for('message')]; |
| 130 | + |
| 131 | + const splunkInfo = info.splunk || {}; |
| 132 | + |
| 133 | + const payload = { |
| 134 | + message: { |
| 135 | + msg: msg |
| 136 | + }, |
| 137 | + metadata: Object.assign({}, this.defaultMetadata, splunkInfo), |
| 138 | + severity: level |
| 139 | + }; |
| 140 | + |
| 141 | + if (meta) { |
| 142 | + if (Object.keys(meta).length) { |
| 143 | + payload.message.meta = meta; |
| 144 | + } |
152 | 145 | } |
153 | | - self.emit('logged'); |
154 | | - callback(null, true); |
155 | | - }); |
156 | | -}; |
| 146 | + |
| 147 | + this.server.send(payload, function (err) { |
| 148 | + if (err && self.exitOnError) { |
| 149 | + self.emit('error', err); |
| 150 | + } |
| 151 | + self.emit('logged'); |
| 152 | + callback(null, true); |
| 153 | + }); |
| 154 | + }; |
| 155 | +} |
157 | 156 |
|
158 | 157 | // Insert this object into the Winston transports list |
159 | 158 | winston.transports.SplunkStreamEvent = SplunkStreamEvent; |
|
0 commit comments