-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
72 lines (58 loc) · 1.69 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
var Writable = require('stream').Writable;
var util = require('util');
var elasticsearch = require('elasticsearch');
var moment = require('moment');
var levels = {
10: 'trace',
20: 'debug',
30: 'info',
40: 'warn',
50: 'error',
60: 'fatal'
};
function generateIndexName (pattern, entry) {
return moment.utc(entry.timestamp).format(pattern);
}
function callOrString (value, entry) {
if (typeof(value) === 'function') {
return value(entry);
}
return value;
}
function ElasticsearchStream (options) {
options = options || {};
this._client = options.client || new elasticsearch.Client(options);
this._type = options.type || 'logs';
var indexPattern = options.indexPattern || '[logstash-]YYYY.MM.DD';
this._index = options.index || generateIndexName.bind(null, indexPattern);
Writable.call(this, options);
}
util.inherits(ElasticsearchStream, Writable);
ElasticsearchStream.prototype._write = function (entry, encoding, callback) {
var client = this._client;
var index = this._index;
var type = this._type;
entry = JSON.parse(entry.toString('utf8'));
// Reassign these fields so them match what the default Kibana dashboard
// expects to see.
entry['@timestamp'] = entry.time;
entry.level = levels[entry.level];
entry.message = entry.msg;
// remove duplicate fields
delete entry.time;
delete entry.msg;
var datestamp = moment(entry.timestamp).format('YYYY.MM.DD');
var options = {
index: callOrString(index, entry),
type: callOrString(type, entry),
body: entry
};
var self = this;
client.index(options, function (err, resp) {
if (err) {
self.emit('error', err);
}
callback();
});
};
module.exports = ElasticsearchStream;