From 5b2d840472c331c647b69b39196ebd66a49c7388 Mon Sep 17 00:00:00 2001 From: Jens John Date: Thu, 10 Jul 2014 14:07:38 +0900 Subject: [PATCH 1/5] GELF appender: move facility key into a custom field if present At the same time, don't assign a default value as the field is optional according to the GELF spec v1.1. --- lib/appenders/gelf.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/appenders/gelf.js b/lib/appenders/gelf.js index 8e075b5..e536462 100644 --- a/lib/appenders/gelf.js +++ b/lib/appenders/gelf.js @@ -48,7 +48,9 @@ function gelfAppender (layout, host, port, hostname, facility) { host = host || 'localhost'; port = port || 12201; hostname = hostname || require('os').hostname(); - facility = facility || 'nodejs-server'; + if(facility) { + customFields['_facility'] = facility; + } layout = layout || layouts.messagePassThroughLayout; var defaultCustomFields = customFields || {}; @@ -103,7 +105,6 @@ function gelfAppender (layout, host, port, hostname, facility) { msg.timestamp = msg.timestamp || new Date().getTime() / 1000; // log should use millisecond msg.host = hostname; msg.level = levelMapping[loggingEvent.level || levels.DEBUG]; - msg.facility = facility; return msg; } From 0f51ab1bb347d76b4576ddb0ff59b38fd67277a5 Mon Sep 17 00:00:00 2001 From: Jens John Date: Thu, 10 Jul 2014 14:26:27 +0900 Subject: [PATCH 2/5] GELF appender: set version to 1.1 --- lib/appenders/gelf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/appenders/gelf.js b/lib/appenders/gelf.js index e536462..01544f9 100644 --- a/lib/appenders/gelf.js +++ b/lib/appenders/gelf.js @@ -101,7 +101,7 @@ function gelfAppender (layout, host, port, hostname, facility) { msg.full_message = layout(loggingEvent); msg.short_message = msg.full_message; - msg.version="1.0"; + msg.version="1.1"; msg.timestamp = msg.timestamp || new Date().getTime() / 1000; // log should use millisecond msg.host = hostname; msg.level = levelMapping[loggingEvent.level || levels.DEBUG]; From fcafed9a7e88564fe50b26e5f0c5d0c49f739fee Mon Sep 17 00:00:00 2001 From: Jens John Date: Fri, 11 Jul 2014 12:52:23 +0900 Subject: [PATCH 3/5] fix gelfAppender-test.js to reflect the above changes --- test/gelfAppender-test.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/gelfAppender-test.js b/test/gelfAppender-test.js index 4a1ff58..72dcf0d 100644 --- a/test/gelfAppender-test.js +++ b/test/gelfAppender-test.js @@ -115,10 +115,9 @@ vows.describe('log4js gelfAppender').addBatch({ return message; }, 'should be in the gelf format': function(message) { - assert.equal(message.version, '1.0'); + assert.equal(message.version, '1.1'); assert.equal(message.host, require('os').hostname()); assert.equal(message.level, 6); //INFO - assert.equal(message.facility, 'nodejs-server'); assert.equal(message.full_message, message.short_message); assert.equal(message.full_message, 'This is a test'); } @@ -246,7 +245,7 @@ vows.describe('log4js gelfAppender').addBatch({ }, 'should pick up the options': function(message) { assert.equal(message.host, 'cheese'); - assert.equal(message.facility, 'nonsense'); + assert.equal(message._facility, 'nonsense'); assert.equal(message._every1, 'Hello every one'); // the default value assert.equal(message._every2, 'Overwritten!'); // the overwritten value assert.equal(message._myField, 'This is my field!'); // the value for this message only From 2b0a58c2f28c559eb6ca7708a2888b8a88e79c2b Mon Sep 17 00:00:00 2001 From: Jens John Date: Fri, 11 Jul 2014 13:02:06 +0900 Subject: [PATCH 4/5] reflect the above changes ; forgot a facility field --- test/gelfAppender-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/gelfAppender-test.js b/test/gelfAppender-test.js index 72dcf0d..44774ce 100644 --- a/test/gelfAppender-test.js +++ b/test/gelfAppender-test.js @@ -165,7 +165,7 @@ vows.describe('log4js gelfAppender').addBatch({ }, 'should pick up the options': function(message) { assert.equal(message.host, 'cheese'); - assert.equal(message.facility, 'nonsense'); + assert.equal(message._facility, 'nonsense'); } } }, From 52b4aa18746bbc7608f892d49c0f0b18a79fc846 Mon Sep 17 00:00:00 2001 From: Jens John Date: Fri, 11 Jul 2014 19:19:19 +0900 Subject: [PATCH 5/5] fix uninitialized field error --- lib/appenders/gelf.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/appenders/gelf.js b/lib/appenders/gelf.js index 01544f9..d130bf7 100644 --- a/lib/appenders/gelf.js +++ b/lib/appenders/gelf.js @@ -48,13 +48,14 @@ function gelfAppender (layout, host, port, hostname, facility) { host = host || 'localhost'; port = port || 12201; hostname = hostname || require('os').hostname(); - if(facility) { - customFields['_facility'] = facility; - } layout = layout || layouts.messagePassThroughLayout; var defaultCustomFields = customFields || {}; - + + if(facility) { + defaultCustomFields['_facility'] = facility; + } + var client = dgram.createSocket("udp4"); process.on('exit', function() {