GELF appender can add custom fields
This commit is contained in:
parent
2e7f6e5a66
commit
be754f0c0e
@ -47,9 +47,33 @@ function gelfAppender (layout, host, port, hostname, facility) {
|
|||||||
process.on('exit', function() {
|
process.on('exit', function() {
|
||||||
if (client) client.close();
|
if (client) client.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add custom fields (start with underscore )
|
||||||
|
* - if the first object passed to the logger contains 'GELF' field, add copy the underscore fields to the message
|
||||||
|
* @param loggingEvent
|
||||||
|
* @param msg
|
||||||
|
*/
|
||||||
|
function addCustomFields(loggingEvent, msg){
|
||||||
|
var data = loggingEvent.data;
|
||||||
|
if (!Array.isArray(data) || data.length === 0) return;
|
||||||
|
var firstData = data[0];
|
||||||
|
if (!firstData['GELF']) return; // identify with GELF field defined
|
||||||
|
var keys = Object.keys(firstData);
|
||||||
|
for (var i in keys){
|
||||||
|
var key = keys[i];
|
||||||
|
// skip _id field for graylog2, skip keys not starts with UNDERSCORE
|
||||||
|
if (!key.match(/^_/) || key === "_id") continue;
|
||||||
|
msg[key] = firstData[key];
|
||||||
|
}
|
||||||
|
loggingEvent.data.shift(); // remove the first object
|
||||||
|
//console.log('=== msg ===');
|
||||||
|
//console.log(msg);
|
||||||
|
}
|
||||||
|
|
||||||
function preparePacket(loggingEvent) {
|
function preparePacket(loggingEvent) {
|
||||||
var msg = {};
|
var msg = {};
|
||||||
|
addCustomFields(loggingEvent, msg);
|
||||||
msg.full_message = layout(loggingEvent);
|
msg.full_message = layout(loggingEvent);
|
||||||
msg.short_message = msg.full_message;
|
msg.short_message = msg.full_message;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user