-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (56 loc) · 1.6 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
"use strict";
const util = require('util');
const layouts = require('log4js').layouts;
const Kafka = require('no-kafka');
const producer = new Kafka.Producer();
function logstashKafka(config, layout) {
let type = config.logType ? config.logType : config.category;
layout = layout || layouts.dummyLayout;
if(!config.fields) {
config.fields = {};
}
return function log(loggingEvent) {
if (loggingEvent.data.length > 1) {
let secondEvData = loggingEvent.data[1];
for (let k in secondEvData) {
config.fields[k] = secondEvData[k];
}
}
config.fields.level = loggingEvent.level.levelStr;
let logObject = {
"@version" : "1",
"@timestamp" : (new Date(loggingEvent.startTime)).toISOString(),
"type" : config.logType ? config.logType : config.category,
"message" : layout(loggingEvent),
"fields" : JSON.stringify(config.fields)
};
sendLog(config.topic, logObject);
};
}
function sendLog(topic, logObject) {
let logString = JSON.stringify(logObject);
producer.init().then(function(){
return producer.send({
topic: 'test',
partition: 0,
message: {
value: logString
}
});
})
.then(function (result) {
// console.log('result:', result);
})
.catch(function (err) {
console.log("log4js-logstash-kafka - Error: %s", util.inspect(err))
});
}
function configure(config) {
let layout;
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
}
return logstashKafka(config, layout);
}
exports.appender = logstashKafka;
exports.configure = configure;