diff --git a/lib/appenders/gelf.js b/lib/appenders/gelf.js index 4599856..e3e2f5c 100644 --- a/lib/appenders/gelf.js +++ b/lib/appenders/gelf.js @@ -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;