GELF appender can add custom fields

This commit is contained in:
Karl Lam 2013-07-05 10:54:31 +08:00
parent 2e7f6e5a66
commit be754f0c0e

View File

@ -47,9 +47,33 @@ function gelfAppender (layout, host, port, hostname, facility) {
process.on('exit', function() {
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) {
var msg = {};
addCustomFields(loggingEvent, msg);
msg.full_message = layout(loggingEvent);
msg.short_message = msg.full_message;