Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

Commit bc81eb0

Browse files
author
Adrian Hall
authored
feat!: Convert package to class semantics, update to NodeJS 12.x minimum (#36)
BREAKING CHANGE: minimum NodeJS version has been updated.
1 parent aaf1c5d commit bc81eb0

File tree

5 files changed

+878
-214
lines changed

5 files changed

+878
-214
lines changed

.eslintrc.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
2-
"parserOptions": {
3-
"ecmaVersion": 6,
4-
"sourceType": "module",
5-
"ecmaFeatures": {
6-
"globalReturn": false,
7-
"impliedStrict": true
2+
"env": {
3+
"commonjs": true,
4+
"es2021": true,
5+
"node": true
6+
},
7+
"extends": [
8+
"standard"
9+
],
10+
"parserOptions": {
11+
"ecmaVersion": 10
12+
},
13+
"rules": {
14+
"semi": [ "error", "always" ]
815
}
9-
},
10-
"env": {
11-
"es6": true,
12-
"node": true
13-
},
14-
"extends": "eslint:recommended"
1516
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (C) 2015-2017 Adrian Hall
1+
Copyright (C) 2015-2021 Adrian Hall
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
44
documentation files (the "Software"), to deal in the Software without restriction, including without limitation

index.js

Lines changed: 89 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2015-2017 Adrian Hall
2+
* Copyright (C) 2015-2021 Adrian Hall
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the "Software"), to deal
@@ -19,10 +19,9 @@
1919
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
* THE SOFTWARE.
2121
*/
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;
2625

2726
if (!isStream(new winston.Transport())) {
2827
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())) {
5857
*
5958
* @constructor
6059
*/
61-
var SplunkStreamEvent = function (config) {
62-
winston.Transport.call(this, config);
60+
class SplunkStreamEvent extends winston.Transport {
61+
constructor (config) {
62+
super(config);
6363

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';
6666

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;
7070

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+
}
7575

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+
}
9595

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);
9999

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+
}
103104
}
104-
};
105-
106-
util.inherits(SplunkStreamEvent, winston.Transport);
107105

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;
147113
}
148114

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+
}
152145
}
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+
}
157156

158157
// Insert this object into the Winston transports list
159158
winston.transports.SplunkStreamEvent = SplunkStreamEvent;

0 commit comments

Comments
 (0)