-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (110 loc) · 4 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
'use strict';
const
util = require('util'),
winston = require('winston'),
dynamodbIntegration = require('./lib/dynamodb-integration'),
isEmpty = require('lodash.isempty'),
isError = require('lodash.iserror'),
stringify = require('./lib/utils').stringify,
debug = require('./lib/utils').debug,
defaultFlushTimeoutMs = 10000;
const WinstonDynamoDB = function (options) {
winston.Transport.call(this, options);
this.level = options.level || 'info';
this.name = options.name || 'DynamoDB';
this.tableName = options.tableName;
this.logStreamName = options.logStreamName;
this.options = options;
const messageFormatter = options.messageFormatter ? options.messageFormatter : function (log) {
return [log.level, log.message].join(' - ')
};
this.formatMessage = options.jsonMessage ? stringify : messageFormatter;
this.proxyServer = options.proxyServer;
this.uploadRate = options.uploadRate || 2000;
this.logEvents = [];
this.errorHandler = options.errorHandler;
if (options.dynamoDbClient) {
this.dynamoDB = options.dynamoDbClient;
} else {
throw new Error("Pass configured DynamoDB client as 'dynamoDbClient' option");
}
debug('constructor finished');
};
util.inherits(WinstonDynamoDB, winston.Transport);
WinstonDynamoDB.prototype.log = function (info, callback) {
debug('log (called by winston)', info);
if (!isEmpty(info.message) || isError(info.message)) {
this.add(info);
}
if (!/^uncaughtException: /.test(info.message)) {
// do not wait, just return right away
return callback(null, true);
}
debug('message not empty, proceeding')
// clear interval and send logs immediately
// as Winston is about to end the process
clearInterval(this.intervalId);
this.intervalId = null;
this.submit(callback);
};
WinstonDynamoDB.prototype.createUploadInterval = function () {
this.intervalId = setInterval(() => {
this.submit();
}, this.uploadRate);
}
WinstonDynamoDB.prototype.add = function (log) {
debug('add log to queue', log);
if (!isEmpty(log.message) || isError(log.message)) {
this.logEvents.push({
message: this.formatMessage(log),
timestamp: process.hrtime.bigint(),
rawMessage: log
});
}
// When we reach maximum amount of items in batch, reschedule
if (this.logEvents.length >= dynamodbIntegration.MAX_BATCH_ITEM_NUM) {
debug('Max items for batch reached - submitting and rescheduling interval');
clearInterval(this.intervalId);
this.createUploadInterval();
this.submit();
}
if (!this.intervalId) {
debug('creating interval');
this.createUploadInterval()
}
};
WinstonDynamoDB.prototype.submit = function (callback) {
const defaultCallback = (err) => {
if (err) {
debug('error during submit', err, true);
this.errorHandler && this.errorHandler(err);
}
}
callback = callback || defaultCallback;
const streamName = typeof this.logStreamName === 'function' ?
this.logStreamName() : this.logStreamName;
if (isEmpty(this.logEvents)) {
return callback();
}
dynamodbIntegration.upload(
this.dynamoDB,
this.tableName,
streamName,
this.logEvents,
this.options,
callback
);
};
WinstonDynamoDB.prototype.kthxbye = function (callback) {
clearInterval(this.intervalId);
this.intervalId = null;
this.flushTimeout = this.flushTimeout || (Date.now() + defaultFlushTimeoutMs);
this.submit((function (error) {
if (error) return callback(error);
if (isEmpty(this.logEvents)) return callback();
if (Date.now() > this.flushTimeout) return callback(new Error('Timeout reached while waiting for logs to submit'));
else setTimeout(this.kthxbye.bind(this, callback), 0);
}).bind(this));
};
winston.transports.DynamoDB = WinstonDynamoDB;
module.exports = WinstonDynamoDB;