Merge pull request #56 from arifamirani/master

Fixed tests for gelf appender
This commit is contained in:
Gareth Jones 2012-01-15 14:36:55 -08:00
commit 2ab6f5fa24
2 changed files with 17 additions and 18 deletions

View File

@ -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";

View File

@ -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);