forked from vanthome/winston-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (112 loc) · 3.27 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
'use strict';
const winston = require('winston');
const Transport = require('winston-transport');
const dayjs = require('dayjs');
const defaults = require('lodash.defaults');
const omit = require('lodash.omit');
const { Client } = require('@elastic/elasticsearch');
const defaultTransformer = require('./transformer');
const BulkWriter = require('./bulk_writer');
module.exports = class Elasticsearch extends Transport {
constructor(opts) {
super(opts);
this.name = 'elasticsearch';
this.on('finish', (info) => {
this.bulkWriter.schedule = () => { };
});
this.opts = opts || {};
// Set defaults
defaults(opts, {
level: 'info',
index: null,
indexPrefix: 'logs',
indexSuffixPattern: 'YYYY.MM.DD',
messageType: '_doc',
transformer: defaultTransformer,
ensureMappingTemplate: true,
flushInterval: 2000,
waitForActiveShards: 1,
handleExceptions: false,
pipeline: null,
bufferLimit: null,
buffering: true
});
// Use given client or create one
if (opts.client) {
this.client = opts.client;
} else {
defaults(opts, {
clientOpts: {
log: [
{
type: 'console',
level: 'error',
}
]
}
});
// Create a new ES client
// http://localhost:9200 is the default of the client already
const copts = { ...this.opts.clientOpts };
this.client = new Client(copts);
}
const bulkWriteropts = {
interval: opts.flushInterval,
waitForActiveShards: opts.waitForActiveShards,
pipeline: opts.pipeline,
ensureMappingTemplate: opts.ensureMappingTemplate,
mappingTemplate: opts.mappingTemplate,
indexPrefix: opts.indexPrefix,
buffering: opts.buffering,
bufferLimit: opts.buffering ? opts.bufferLimit : 0,
};
this.bulkWriter = new BulkWriter(
this,
this.client,
bulkWriteropts
);
this.bulkWriter.start();
}
log(info, callback) {
const { level, message, timestamp } = info;
const meta = Object.assign({}, omit(info, ['level', 'message']));
setImmediate(() => {
this.emit('logged', level);
});
const logData = {
message,
level,
timestamp,
meta,
};
const entry = this.opts.transformer(logData);
let index = this.getIndexName(this.opts);
if (entry.indexInterfix !== undefined) {
index = this.getIndexName(this.opts, entry.indexInterfix);
delete entry.indexInterfix;
}
this.bulkWriter.append(
index,
this.opts.messageType,
entry
);
callback();
}
getIndexName(opts, indexInterfix) {
this.test = 'test';
let indexName = opts.index;
if (indexName === null) {
// eslint-disable-next-line prefer-destructuring
let indexPrefix = opts.indexPrefix;
if (typeof indexPrefix === 'function') {
// eslint-disable-next-line prefer-destructuring
indexPrefix = opts.indexPrefix();
}
const now = dayjs();
const dateString = now.format(opts.indexSuffixPattern);
indexName = indexPrefix + (indexInterfix !== undefined ? '-' + indexInterfix : '') + '-' + dateString;
}
return indexName;
}
};
winston.transports.Elasticsearch = module.exports;