fixed up gelf tests
This commit is contained in:
@@ -13,7 +13,7 @@ var util = require('util');
|
|||||||
* @param hostname - hostname of the current host (default:os hostname)
|
* @param hostname - hostname of the current host (default:os hostname)
|
||||||
* @param facility - facility to log to (default:nodejs-server)
|
* @param facility - facility to log to (default:nodejs-server)
|
||||||
*/
|
*/
|
||||||
function gelfAppender (layout, host, port, hostname, facility, externalClient) {
|
function gelfAppender (layout, host, port, hostname, facility) {
|
||||||
|
|
||||||
var logEventBuffer = [];
|
var logEventBuffer = [];
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ function gelfAppender (layout, host, port, hostname, facility, externalClient) {
|
|||||||
facility = facility || 'nodejs-server';
|
facility = facility || 'nodejs-server';
|
||||||
layout = layout || layouts.patternLayout('%m');
|
layout = layout || layouts.patternLayout('%m');
|
||||||
|
|
||||||
var client = externalClient || dgram.createSocket("udp4");
|
var client = dgram.createSocket("udp4");
|
||||||
|
|
||||||
process.on('exit', function() {
|
process.on('exit', function() {
|
||||||
if (client) client.close();
|
if (client) client.close();
|
||||||
@@ -90,7 +90,7 @@ function configure(config) {
|
|||||||
if (config.layout) {
|
if (config.layout) {
|
||||||
layout = layouts.layout(config.layout.type, config.layout);
|
layout = layouts.layout(config.layout.type, config.layout);
|
||||||
}
|
}
|
||||||
return gelfAppender(layout, config.host, config.port, config.hostname, config.facility, config.client);
|
return gelfAppender(layout, config.host, config.port, config.hostname, config.facility);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.name = "gelf";
|
exports.name = "gelf";
|
||||||
|
|||||||
+45
-18
@@ -1,30 +1,57 @@
|
|||||||
var vows = require('vows')
|
var vows = require('vows')
|
||||||
, log4js = require('../lib/log4js')
|
|
||||||
, assert = require('assert')
|
, assert = require('assert')
|
||||||
, dgram = require("dgram");
|
, sandbox = require('sandboxed-module')
|
||||||
|
, fakeDgram = {
|
||||||
|
socket: {
|
||||||
|
packetLength: 0,
|
||||||
|
close: function() {
|
||||||
|
},
|
||||||
|
send: function(pkt, offset, pktLength, port, host) {
|
||||||
|
this.packet = pkt;
|
||||||
|
this.offset = offset;
|
||||||
|
this.packetLength = pktLength;
|
||||||
|
this.port = port;
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
createSocket: function(type) {
|
||||||
|
this.type = type;
|
||||||
|
return this.socket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
, fakeCompressBuffer = {
|
||||||
|
compress: function(objectToCompress) {
|
||||||
|
this.uncompressed = objectToCompress;
|
||||||
|
return "I've been compressed";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
, appender = sandbox.require('../lib/appenders/gelf', {
|
||||||
|
requires: {
|
||||||
|
dgram: fakeDgram,
|
||||||
|
"compress-buffer": fakeCompressBuffer
|
||||||
|
}
|
||||||
|
})
|
||||||
|
, log4js = require('../lib/log4js');
|
||||||
|
|
||||||
var fakeClient = {
|
log4js.clearAppenders();
|
||||||
packetLength: 0,
|
log4js.addAppender(appender.configure({}), "gelf-test");
|
||||||
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({
|
vows.describe('log4js gelfAppender').addBatch({
|
||||||
|
|
||||||
'with default gelfAppender settings': {
|
'with default gelfAppender settings': {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
var logger = log4js.getLogger();
|
log4js.getLogger("gelf-test").info("This is a test");
|
||||||
var self = this;
|
return fakeDgram;
|
||||||
logger.info('Fake log message');
|
|
||||||
callback();
|
|
||||||
},
|
},
|
||||||
'should receive log messages at the local gelf server': function(err, packet) {
|
'should send log messages via udp to the localhost gelf server': function(dgram) {
|
||||||
assert.ok(fakeClient.packetLength > 0, "Recevied blank message");
|
assert.equal(dgram.type, "udp4");
|
||||||
|
assert.equal(dgram.socket.host, "localhost");
|
||||||
|
assert.equal(dgram.socket.port, 12201);
|
||||||
|
assert.equal(dgram.socket.offset, 0);
|
||||||
|
assert.ok(dgram.socket.packetLength > 0, "Received blank message");
|
||||||
|
},
|
||||||
|
'should compress the log message': function(dgram) {
|
||||||
|
assert.equal(dgram.socket.packet, "I've been compressed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).export(module);
|
}).export(module);
|
||||||
+141
-141
@@ -1,162 +1,162 @@
|
|||||||
var vows = require('vows'),
|
var vows = require('vows'),
|
||||||
assert = require('assert'),
|
assert = require('assert'),
|
||||||
log4js = require('../lib/log4js'),
|
log4js = require('../lib/log4js'),
|
||||||
sandbox = require('sandboxed-module');
|
sandbox = require('sandboxed-module');
|
||||||
|
|
||||||
function setupLogging(category, options) {
|
function setupLogging(category, options) {
|
||||||
var msgs = [];
|
var msgs = [];
|
||||||
|
|
||||||
var fakeMailer = {
|
var fakeMailer = {
|
||||||
send_mail: function (msg, callback) {
|
send_mail: function (msg, callback) {
|
||||||
msgs.push(msg);
|
msgs.push(msg);
|
||||||
callback(null, true);
|
callback(null, true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var smtpModule = sandbox.require('../lib/appenders/smtp', {
|
var smtpModule = sandbox.require('../lib/appenders/smtp', {
|
||||||
requires: {
|
requires: {
|
||||||
'nodemailer': fakeMailer
|
'nodemailer': fakeMailer
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
log4js.addAppender(smtpModule.configure(options), category);
|
log4js.addAppender(smtpModule.configure(options), category);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
logger: log4js.getLogger(category),
|
logger: log4js.getLogger(category),
|
||||||
mailer: fakeMailer,
|
mailer: fakeMailer,
|
||||||
results: msgs
|
results: msgs
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkMessages (result, sender, subject) {
|
function checkMessages (result, sender, subject) {
|
||||||
for (var i = 0; i < result.results.length; ++i) {
|
for (var i = 0; i < result.results.length; ++i) {
|
||||||
assert.equal(result.results[i].sender, sender ? sender : result.mailer.SMTP.user);
|
assert.equal(result.results[i].sender, sender ? sender : result.mailer.SMTP.user);
|
||||||
assert.equal(result.results[i].to, 'recipient@domain.com');
|
assert.equal(result.results[i].to, 'recipient@domain.com');
|
||||||
assert.equal(result.results[i].subject, subject ? subject : 'Log event #' + (i+1));
|
assert.equal(result.results[i].subject, subject ? subject : 'Log event #' + (i+1));
|
||||||
assert.ok(new RegExp('.+Log event #' + (i+1) + '\n$').test(result.results[i].body));
|
assert.ok(new RegExp('.+Log event #' + (i+1) + '\n$').test(result.results[i].body));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log4js.clearAppenders();
|
log4js.clearAppenders();
|
||||||
vows.describe('log4js smtpAppender').addBatch({
|
vows.describe('log4js smtpAppender').addBatch({
|
||||||
'minimal config': {
|
'minimal config': {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
var setup = setupLogging('minimal config', {
|
var setup = setupLogging('minimal config', {
|
||||||
recipients: 'recipient@domain.com',
|
recipients: 'recipient@domain.com',
|
||||||
smtp: {
|
smtp: {
|
||||||
port: 25,
|
port: 25,
|
||||||
user: 'user@domain.com'
|
user: 'user@domain.com'
|
||||||
}
|
|
||||||
});
|
|
||||||
setup.logger.info('Log event #1');
|
|
||||||
return setup;
|
|
||||||
},
|
|
||||||
'mailer should be configured properly': function (result) {
|
|
||||||
assert.ok(result.mailer.SMTP);
|
|
||||||
assert.equal(result.mailer.SMTP.port, 25);
|
|
||||||
assert.equal(result.mailer.SMTP.user, 'user@domain.com');
|
|
||||||
},
|
|
||||||
'there should be one message only': function (result) {
|
|
||||||
assert.equal(result.results.length, 1);
|
|
||||||
},
|
|
||||||
'message should contain proper data': function (result) {
|
|
||||||
checkMessages(result);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
setup.logger.info('Log event #1');
|
||||||
|
return setup;
|
||||||
},
|
},
|
||||||
'fancy config': {
|
'mailer should be configured properly': function (result) {
|
||||||
topic: function() {
|
assert.ok(result.mailer.SMTP);
|
||||||
var setup = setupLogging('fancy config', {
|
assert.equal(result.mailer.SMTP.port, 25);
|
||||||
recipients: 'recipient@domain.com',
|
assert.equal(result.mailer.SMTP.user, 'user@domain.com');
|
||||||
sender: 'sender@domain.com',
|
|
||||||
subject: 'This is subject',
|
|
||||||
smtp: {
|
|
||||||
port: 25,
|
|
||||||
user: 'user@domain.com'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setup.logger.info('Log event #1');
|
|
||||||
return setup;
|
|
||||||
},
|
|
||||||
'mailer should be configured properly': function (result) {
|
|
||||||
assert.ok(result.mailer.SMTP);
|
|
||||||
assert.equal(result.mailer.SMTP.port, 25);
|
|
||||||
assert.equal(result.mailer.SMTP.user, 'user@domain.com');
|
|
||||||
},
|
|
||||||
'there should be one message only': function (result) {
|
|
||||||
assert.equal(result.results.length, 1);
|
|
||||||
},
|
|
||||||
'message should contain proper data': function (result) {
|
|
||||||
checkMessages(result, 'sender@domain.com', 'This is subject');
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
'separate email for each event': {
|
'there should be one message only': function (result) {
|
||||||
topic: function() {
|
assert.equal(result.results.length, 1);
|
||||||
var self = this;
|
|
||||||
var setup = setupLogging('separate email for each event', {
|
|
||||||
recipients: 'recipient@domain.com',
|
|
||||||
smtp: {
|
|
||||||
port: 25,
|
|
||||||
user: 'user@domain.com'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setTimeout(function () {
|
|
||||||
setup.logger.info('Log event #1');
|
|
||||||
}, 0);
|
|
||||||
setTimeout(function () {
|
|
||||||
setup.logger.info('Log event #2');
|
|
||||||
}, 500);
|
|
||||||
setTimeout(function () {
|
|
||||||
setup.logger.info('Log event #3');
|
|
||||||
}, 1050);
|
|
||||||
setTimeout(function () {
|
|
||||||
self.callback(null, setup);
|
|
||||||
}, 2100);
|
|
||||||
},
|
|
||||||
'there should be three messages': function (result) {
|
|
||||||
assert.equal(result.results.length, 3);
|
|
||||||
},
|
|
||||||
'messages should contain proper data': function (result) {
|
|
||||||
checkMessages(result);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
'multiple events in one email': {
|
'message should contain proper data': function (result) {
|
||||||
topic: function() {
|
checkMessages(result);
|
||||||
var self = this;
|
|
||||||
var setup = setupLogging('multiple events in one email', {
|
|
||||||
recipients: 'recipient@domain.com',
|
|
||||||
sendInterval: 1,
|
|
||||||
smtp: {
|
|
||||||
port: 25,
|
|
||||||
user: 'user@domain.com'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setTimeout(function () {
|
|
||||||
setup.logger.info('Log event #1');
|
|
||||||
}, 0);
|
|
||||||
setTimeout(function () {
|
|
||||||
setup.logger.info('Log event #2');
|
|
||||||
}, 500);
|
|
||||||
setTimeout(function () {
|
|
||||||
setup.logger.info('Log event #3');
|
|
||||||
}, 1050);
|
|
||||||
setTimeout(function () {
|
|
||||||
self.callback(null, setup);
|
|
||||||
}, 2100);
|
|
||||||
},
|
|
||||||
'there should be two messages': function (result) {
|
|
||||||
assert.equal(result.results.length, 2);
|
|
||||||
},
|
|
||||||
'messages should contain proper data': function (result) {
|
|
||||||
assert.equal(result.results[0].sender, result.mailer.SMTP.user);
|
|
||||||
assert.equal(result.results[0].to, 'recipient@domain.com');
|
|
||||||
assert.equal(result.results[0].subject, 'Log event #1');
|
|
||||||
assert.equal(result.results[0].body.match(new RegExp('.+Log event #[1-2]$', 'gm')).length, 2);
|
|
||||||
|
|
||||||
assert.equal(result.results[1].sender, result.mailer.SMTP.user);
|
|
||||||
assert.equal(result.results[1].to, 'recipient@domain.com');
|
|
||||||
assert.equal(result.results[1].subject, 'Log event #3');
|
|
||||||
assert.ok(new RegExp('.+Log event #3\n$').test(result.results[1].body));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
'fancy config': {
|
||||||
|
topic: function() {
|
||||||
|
var setup = setupLogging('fancy config', {
|
||||||
|
recipients: 'recipient@domain.com',
|
||||||
|
sender: 'sender@domain.com',
|
||||||
|
subject: 'This is subject',
|
||||||
|
smtp: {
|
||||||
|
port: 25,
|
||||||
|
user: 'user@domain.com'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setup.logger.info('Log event #1');
|
||||||
|
return setup;
|
||||||
|
},
|
||||||
|
'mailer should be configured properly': function (result) {
|
||||||
|
assert.ok(result.mailer.SMTP);
|
||||||
|
assert.equal(result.mailer.SMTP.port, 25);
|
||||||
|
assert.equal(result.mailer.SMTP.user, 'user@domain.com');
|
||||||
|
},
|
||||||
|
'there should be one message only': function (result) {
|
||||||
|
assert.equal(result.results.length, 1);
|
||||||
|
},
|
||||||
|
'message should contain proper data': function (result) {
|
||||||
|
checkMessages(result, 'sender@domain.com', 'This is subject');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'separate email for each event': {
|
||||||
|
topic: function() {
|
||||||
|
var self = this;
|
||||||
|
var setup = setupLogging('separate email for each event', {
|
||||||
|
recipients: 'recipient@domain.com',
|
||||||
|
smtp: {
|
||||||
|
port: 25,
|
||||||
|
user: 'user@domain.com'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setTimeout(function () {
|
||||||
|
setup.logger.info('Log event #1');
|
||||||
|
}, 0);
|
||||||
|
setTimeout(function () {
|
||||||
|
setup.logger.info('Log event #2');
|
||||||
|
}, 500);
|
||||||
|
setTimeout(function () {
|
||||||
|
setup.logger.info('Log event #3');
|
||||||
|
}, 1050);
|
||||||
|
setTimeout(function () {
|
||||||
|
self.callback(null, setup);
|
||||||
|
}, 2100);
|
||||||
|
},
|
||||||
|
'there should be three messages': function (result) {
|
||||||
|
assert.equal(result.results.length, 3);
|
||||||
|
},
|
||||||
|
'messages should contain proper data': function (result) {
|
||||||
|
checkMessages(result);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'multiple events in one email': {
|
||||||
|
topic: function() {
|
||||||
|
var self = this;
|
||||||
|
var setup = setupLogging('multiple events in one email', {
|
||||||
|
recipients: 'recipient@domain.com',
|
||||||
|
sendInterval: 1,
|
||||||
|
smtp: {
|
||||||
|
port: 25,
|
||||||
|
user: 'user@domain.com'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setTimeout(function () {
|
||||||
|
setup.logger.info('Log event #1');
|
||||||
|
}, 0);
|
||||||
|
setTimeout(function () {
|
||||||
|
setup.logger.info('Log event #2');
|
||||||
|
}, 500);
|
||||||
|
setTimeout(function () {
|
||||||
|
setup.logger.info('Log event #3');
|
||||||
|
}, 1050);
|
||||||
|
setTimeout(function () {
|
||||||
|
self.callback(null, setup);
|
||||||
|
}, 2100);
|
||||||
|
},
|
||||||
|
'there should be two messages': function (result) {
|
||||||
|
assert.equal(result.results.length, 2);
|
||||||
|
},
|
||||||
|
'messages should contain proper data': function (result) {
|
||||||
|
assert.equal(result.results[0].sender, result.mailer.SMTP.user);
|
||||||
|
assert.equal(result.results[0].to, 'recipient@domain.com');
|
||||||
|
assert.equal(result.results[0].subject, 'Log event #1');
|
||||||
|
assert.equal(result.results[0].body.match(new RegExp('.+Log event #[1-2]$', 'gm')).length, 2);
|
||||||
|
|
||||||
|
assert.equal(result.results[1].sender, result.mailer.SMTP.user);
|
||||||
|
assert.equal(result.results[1].to, 'recipient@domain.com');
|
||||||
|
assert.equal(result.results[1].subject, 'Log event #3');
|
||||||
|
assert.ok(new RegExp('.+Log event #3\n$').test(result.results[1].body));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}).export(module);
|
}).export(module);
|
||||||
|
|||||||
Reference in New Issue
Block a user