From be754f0c0e1ff1c6dc0526d6ba212278dafad0ef Mon Sep 17 00:00:00 2001 From: Karl Lam Date: Fri, 5 Jul 2013 10:54:31 +0800 Subject: [PATCH] GELF appender can add custom fields --- lib/appenders/gelf.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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;