diff --git a/lib/appenders/gelf.js b/lib/appenders/gelf.js index e0c7fc2..e255681 100644 --- a/lib/appenders/gelf.js +++ b/lib/appenders/gelf.js @@ -13,7 +13,7 @@ var util = require('util'); * @param hostname - hostname of the current host (default:os hostname) * @param facility - facility to log to (default:nodejs-server) */ -function gelfAppender (layout, host, port, hostname, facility) { +function gelfAppender (layout, host, port, hostname, facility, externalClient) { var logEventBuffer = []; @@ -42,7 +42,7 @@ function gelfAppender (layout, host, port, hostname, facility) { facility = facility || 'nodejs-server'; layout = layout || layouts.patternLayout('%m'); - var client = dgram.createSocket("udp4"); + var client = externalClient || dgram.createSocket("udp4"); process.on('exit', function() { if (client) client.close(); @@ -90,7 +90,7 @@ function configure(config) { if (config.layout) { layout = layouts.layout(config.layout.type, config.layout); } - return gelfAppender(layout, config.host, config.port, config.hostname, config.facility); + return gelfAppender(layout, config.host, config.port, config.hostname, config.facility, config.client); } exports.name = "gelf"; diff --git a/test/gelfAppender.js b/test/gelfAppender.js index 331752b..82b7ed6 100644 --- a/test/gelfAppender.js +++ b/test/gelfAppender.js @@ -1,31 +1,30 @@ var vows = require('vows') , log4js = require('../lib/log4js') , assert = require('assert') -, util = require('util') , dgram = require("dgram"); -log4js.configure({ "appenders": [{"type": "gelf"}] }, undefined); +var fakeClient = { + packetLength: 0, + close: function() { + }, + send: function(pkt, offset, pktLength, port, host) { + this.packetLength = pktLength; + } +}; + +log4js.configure({ "appenders": [{"type": "gelf", "client": fakeClient}] }, undefined); vows.describe('log4js gelfAppender').addBatch({ 'with default gelfAppender settings': { topic: function() { var logger = log4js.getLogger(); - - //Start local dgram server to act as GELF server - var server = dgram.createSocket("udp4"); - //Assert as soon as message arrives - server.on("message", this.callback); - //Send a fake message as soon as server is ready - server.on("listening", function () { - logger.info("This should be a packet of size 161 bytes at the server"); - }); - - //Listen on default values - server.bind(12201, 'localhost'); + var self = this; + logger.info('Fake log message'); + callback(); }, 'should receive log messages at the local gelf server': function(err, packet) { - assert.ok(packet.size > 0, "Recevied blank message"); + assert.ok(fakeClient.packetLength > 0, "Recevied blank message"); } } }).export(module); \ No newline at end of file