forked from vanthome/winston-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (128 loc) · 3.46 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
'use strict';
const winston = require('winston');
const Transport = require('winston-transport');
const moment = require('moment');
const _ = require('lodash');
const elasticsearch = require('elasticsearch');
const { LEVEL, SPLAT } = require('triple-beam');
const defaultTransformer = require('./transformer');
const BulkWriter = require('./bulk_writer');
module.exports = class Elasticsearch extends Transport {
constructor(opts) {
super(opts);
this.name = 'elasticsearch';
this.opts = opts || {};
if (!opts.timestamp) {
this.opts.timestamp = function timestamp() { return new Date().toISOString(); };
}
// Enforce context
// if (!(this instanceof Elasticsearch)) {
// return new Elasticsearch(opts);
// }
// Set defaults
const defaults = {
level: 'info',
index: null,
indexPrefix: 'logs',
indexSuffixPattern: 'YYYY.MM.DD',
messageType: '_doc',
transformer: defaultTransformer,
ensureMappingTemplate: true,
flushInterval: 2000,
waitForActiveShards: 1,
handleExceptions: false,
pipeline: null
};
_.defaults(opts, defaults);
// Use given client or create one
if (opts.client) {
this.client = opts.client;
} else {
const defaultClientOpts = {
clientOpts: {
log: [
{
type: 'console',
level: 'error',
}
]
}
};
_.defaults(opts, defaultClientOpts);
// Create a new ES client
// http://localhost:9200 is the default of the client already
this.client = new elasticsearch.Client(_.clone(this.opts.clientOpts));
}
const bulkWriteropts = {
interval: opts.flushInterval,
waitForActiveShards: opts.waitForActiveShards,
pipeline: opts.pipeline,
ensureMappingTemplate: opts.ensureMappingTemplate,
mappingTemplate: opts.mappingTemplate,
indexPrefix: opts.indexPrefix
};
this.bulkWriter = new BulkWriter(
this.client,
bulkWriteropts
);
this.bulkWriter.start();
}
log(info, callback) {
const level = info[LEVEL];
setImmediate(() => {
this.emit('logged', level);
});
let entry;
if (this.opts.rawTransformer) {
entry = this.opts.rawTransformer(info);
} else {
const { message } = info;
let meta = info[SPLAT];
if (meta !== undefined) {
// eslint-disable-next-line prefer-destructuring
meta = meta[0];
}
const logData = {
message,
level,
meta,
// timestamp: this.opts.timestamp()
};
entry = this.opts.transformer(logData);
}
this.bulkWriter.append(
this.getIndexName(this.opts),
this.opts.messageType,
entry
);
callback();
}
query(options, callback) {
// const opts = this.normalizeQuery(options);
const index = this.getIndexName(this.opts);
const query = {
index,
// q
};
return this.client.search(query);
}
search(q) {
const index = this.getIndexName(this.opts);
const query = {
index,
q
};
return this.client.search(query);
}
getIndexName(opts) {
this.test = 'test';
let indexName = opts.index;
if (indexName === null) {
const now = moment();
const dateString = now.format(opts.indexSuffixPattern);
indexName = opts.indexPrefix + '-' + dateString;
}
return indexName;
}
};
winston.transports.Elasticsearch = module.exports;