-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknxESlogger.js
51 lines (45 loc) · 1.32 KB
/
knxESlogger.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
const { ElasticsearchTransport } = require('winston-elasticsearch');
const winston = require('winston');
const { combine, splat } = winston.format;
// default logging config
const DEFAULT_CONFIG = {
KNXESlogging: {
'enabled': false,
'label': 'BLUEKNX2MQTT',
'loglevel:': 'info',
'options' : {
'indexPrefix': 'knx',
'clientOpts': {
'node': 'http://localhost:9200'
}
}
}
}
// merge default with the one from the config file
const config = Object.assign({}, DEFAULT_CONFIG, require('./config.js').parse());
// this will remove "val" if it is not a number and
// replace it with strval instead
// this to avoid issues in ES with the val field having 2 different types (number and string)
const fixVal = winston.format((info) => {
if (info.val && isNaN(info.val)) {
let {val, ...rest} = info;
rest.strval = val.toString();
return rest;
} else {
return info;
}
})();
const esTransportOpts = {
...config.KNXESlogging.options
};
const KNXESlogger = winston.createLogger({
level: 'info',
format: combine(fixVal),
transports: [
new ElasticsearchTransport(esTransportOpts)
],
});
const logKNXToES = ( data ) => {
KNXESlogger.info("knx", data);
}
module.exports = { logKNXToES };