-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
81 lines (66 loc) · 2.79 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
/**
* Licensed Materials - Property of IBM
* (c) Copyright IBM Corporation 2016, 2018. All Rights Reserved.
*
* Note to U.S. Government Users Restricted Rights:
* Use, duplication or disclosure restricted by GSA ADP Schedule
* Contract with IBM Corp.
*/
'use strict';
const LogmetProducer = require('logmet-client').LogmetProducer;
function logmetAppender(logmetClientProducer, spaceId, layout, timezoneOffset) {
const appender = (loggingEvent) => {
logmetClientProducer.sendData(layout(loggingEvent, timezoneOffset), spaceId);
};
appender.shutdown = (callback) => {
if (logmetClientProducer) {
logmetClientProducer.terminate(callback);
}
else {
callback();
}
};
return appender;
}
function configure(config, layouts) {
if (process.env.log4js_logmet_enabled !== 'true') { return () => {}; }
const options = {};
options.host = process.env.log4js_logmet_logging_host || config.logging_host;
options.port = process.env.log4js_logmet_logging_port || config.logging_port;
options.spaceId = process.env.log4js_logmet_space_id || config.space_id;
options.loggingToken = process.env.log4js_logmet_logging_token || config.logging_token;
for (let option in options) {
if (!options[option]) {
console.log(`Logmet appender: ${ option } not specified`);
return () => {};
}
}
let layout = (logEvent) => {
let instanceId = process.env.CF_INSTANCE_INDEX || require('os').hostname().replace(/^.*-([a-zA-Z0-9]+-[a-zA-Z0-9]+)$/g,'$1');
let logmetEvent = {
'component': process.env.log4js_logmet_component || config.component,
'host-ip': process.env.CF_INSTANCE_IP,
'instance-id': instanceId,
'loglevel': logEvent.level.levelStr,
'msg_timestamp': logEvent.startTime.toISOString(),
'message': logEvent.data.join(' | '),
'type': logEvent.categoryName
};
return logmetEvent;
};
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
}
console.log('Logmet appender configured');
const logmetClientProducer = new LogmetProducer(options.host, options.port, options.spaceId, options.loggingToken, false,
{bufferSize: process.env.log4js_logmet_buffer_size || 10000});
logmetClientProducer.connect((error, status) => {
if (error) {
console.error(`Logmet appender: Connection with Logmet failed. Details: ${ error }`);
} else if (status.handshakeCompleted) {
console.log('Logmet appender: LogmetClient is ready to send data.');
}
});
return logmetAppender(logmetClientProducer, options.spaceId, layout, config.timezoneOffset);
}
module.exports.configure = configure;