2013-05-27 06:17:32 +08:00
|
|
|
"use strict";
|
2012-01-12 17:22:55 +08:00
|
|
|
var vows = require('vows')
|
|
|
|
, assert = require('assert')
|
2012-02-10 15:14:50 +08:00
|
|
|
, sandbox = require('sandboxed-module')
|
2012-02-13 05:54:35 +08:00
|
|
|
, log4js = require('../lib/log4js')
|
2013-07-05 06:04:16 +08:00
|
|
|
, realLayouts = require('../lib/layouts')
|
2012-02-13 05:54:35 +08:00
|
|
|
, setupLogging = function(options, category, compressedLength) {
|
2013-05-27 06:17:32 +08:00
|
|
|
var fakeDgram = {
|
|
|
|
sent: false,
|
|
|
|
socket: {
|
|
|
|
packetLength: 0,
|
2013-07-05 06:04:16 +08:00
|
|
|
closed: false,
|
2013-05-27 06:17:32 +08:00
|
|
|
close: function() {
|
2013-07-05 06:04:16 +08:00
|
|
|
this.closed = true;
|
2013-05-27 06:17:32 +08:00
|
|
|
},
|
|
|
|
send: function(pkt, offset, pktLength, port, host) {
|
|
|
|
fakeDgram.sent = true;
|
|
|
|
this.packet = pkt;
|
|
|
|
this.offset = offset;
|
|
|
|
this.packetLength = pktLength;
|
|
|
|
this.port = port;
|
|
|
|
this.host = host;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
createSocket: function(type) {
|
|
|
|
this.type = type;
|
|
|
|
return this.socket;
|
2012-02-10 15:14:50 +08:00
|
|
|
}
|
2013-05-27 06:17:32 +08:00
|
|
|
}
|
2012-05-29 14:49:12 +08:00
|
|
|
, fakeZlib = {
|
2013-05-27 06:17:32 +08:00
|
|
|
gzip: function(objectToCompress, callback) {
|
|
|
|
fakeZlib.uncompressed = objectToCompress;
|
2013-07-05 06:04:16 +08:00
|
|
|
if (this.shouldError) {
|
|
|
|
callback({ stack: "oh noes" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-27 06:17:32 +08:00
|
|
|
if (compressedLength) {
|
|
|
|
callback(null, { length: compressedLength });
|
|
|
|
} else {
|
|
|
|
callback(null, "I've been compressed");
|
2012-02-13 05:54:35 +08:00
|
|
|
}
|
2013-05-27 06:17:32 +08:00
|
|
|
}
|
2012-02-13 05:54:35 +08:00
|
|
|
}
|
2013-07-05 06:04:16 +08:00
|
|
|
, exitHandler
|
|
|
|
, fakeConsole = {
|
|
|
|
error: function(message) {
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
, fakeLayouts = {
|
|
|
|
layout: function(type, options) {
|
|
|
|
this.type = type;
|
|
|
|
this.options = options;
|
|
|
|
return realLayouts.messagePassThroughLayout;
|
|
|
|
},
|
|
|
|
messagePassThroughLayout: realLayouts.messagePassThroughLayout
|
|
|
|
}
|
2012-02-13 05:54:35 +08:00
|
|
|
, appender = sandbox.require('../lib/appenders/gelf', {
|
2013-05-27 06:17:32 +08:00
|
|
|
requires: {
|
|
|
|
dgram: fakeDgram,
|
2013-07-05 06:04:16 +08:00
|
|
|
zlib: fakeZlib,
|
|
|
|
'../layouts': fakeLayouts
|
|
|
|
},
|
|
|
|
globals: {
|
|
|
|
process: {
|
|
|
|
on: function(evt, handler) {
|
|
|
|
if (evt === 'exit') {
|
|
|
|
exitHandler = handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
console: fakeConsole
|
2013-05-27 06:17:32 +08:00
|
|
|
}
|
2012-02-13 05:54:35 +08:00
|
|
|
});
|
2013-05-27 06:17:32 +08:00
|
|
|
|
|
|
|
log4js.clearAppenders();
|
|
|
|
log4js.addAppender(appender.configure(options || {}), category || "gelf-test");
|
|
|
|
return {
|
|
|
|
dgram: fakeDgram,
|
|
|
|
compress: fakeZlib,
|
2013-07-05 06:04:16 +08:00
|
|
|
exitHandler: exitHandler,
|
|
|
|
console: fakeConsole,
|
|
|
|
layouts: fakeLayouts,
|
2013-05-27 06:17:32 +08:00
|
|
|
logger: log4js.getLogger(category || "gelf-test")
|
|
|
|
};
|
2012-02-13 05:54:35 +08:00
|
|
|
};
|
2012-01-13 15:30:53 +08:00
|
|
|
|
2012-01-12 17:22:55 +08:00
|
|
|
vows.describe('log4js gelfAppender').addBatch({
|
2013-05-27 06:17:32 +08:00
|
|
|
|
|
|
|
'with default gelfAppender settings': {
|
|
|
|
topic: function() {
|
|
|
|
var setup = setupLogging();
|
|
|
|
setup.logger.info("This is a test");
|
|
|
|
return setup;
|
2012-02-13 05:54:35 +08:00
|
|
|
},
|
2013-05-27 06:17:32 +08:00
|
|
|
'the dgram packet': {
|
|
|
|
topic: function(setup) {
|
|
|
|
return setup.dgram;
|
|
|
|
},
|
|
|
|
'should be sent via udp to the localhost gelf server': function(dgram) {
|
|
|
|
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 be compressed': function(dgram) {
|
|
|
|
assert.equal(dgram.socket.packet, "I've been compressed");
|
|
|
|
}
|
2012-02-13 05:54:35 +08:00
|
|
|
},
|
2013-05-27 06:17:32 +08:00
|
|
|
'the uncompressed log message': {
|
|
|
|
topic: function(setup) {
|
|
|
|
var message = JSON.parse(setup.compress.uncompressed);
|
|
|
|
return message;
|
|
|
|
},
|
|
|
|
'should be in the gelf format': function(message) {
|
|
|
|
assert.equal(message.version, '1.0');
|
|
|
|
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');
|
|
|
|
}
|
2012-01-12 17:22:55 +08:00
|
|
|
}
|
2013-05-27 06:17:32 +08:00
|
|
|
},
|
|
|
|
'with a message longer than 8k': {
|
|
|
|
topic: function() {
|
|
|
|
var setup = setupLogging(undefined, undefined, 10240);
|
|
|
|
setup.logger.info("Blah.");
|
|
|
|
return setup;
|
|
|
|
},
|
|
|
|
'the dgram packet': {
|
|
|
|
topic: function(setup) {
|
|
|
|
return setup.dgram;
|
|
|
|
},
|
|
|
|
'should not be sent': function(dgram) {
|
|
|
|
assert.equal(dgram.sent, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'with non-default options': {
|
|
|
|
topic: function() {
|
|
|
|
var setup = setupLogging({
|
|
|
|
host: 'somewhere',
|
|
|
|
port: 12345,
|
|
|
|
hostname: 'cheese',
|
|
|
|
facility: 'nonsense'
|
|
|
|
});
|
|
|
|
setup.logger.debug("Just testing.");
|
|
|
|
return setup;
|
|
|
|
},
|
|
|
|
'the dgram packet': {
|
|
|
|
topic: function(setup) {
|
|
|
|
return setup.dgram;
|
|
|
|
},
|
|
|
|
'should pick up the options': function(dgram) {
|
|
|
|
assert.equal(dgram.socket.host, 'somewhere');
|
|
|
|
assert.equal(dgram.socket.port, 12345);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'the uncompressed packet': {
|
|
|
|
topic: function(setup) {
|
|
|
|
var message = JSON.parse(setup.compress.uncompressed);
|
|
|
|
return message;
|
|
|
|
},
|
|
|
|
'should pick up the options': function(message) {
|
|
|
|
assert.equal(message.host, 'cheese');
|
|
|
|
assert.equal(message.facility, 'nonsense');
|
|
|
|
}
|
|
|
|
}
|
2013-07-05 06:04:16 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'on process.exit': {
|
|
|
|
topic: function() {
|
|
|
|
var setup = setupLogging();
|
|
|
|
setup.exitHandler();
|
|
|
|
return setup;
|
|
|
|
},
|
|
|
|
'should close open sockets': function(setup) {
|
|
|
|
assert.isTrue(setup.dgram.socket.closed);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'on zlib error': {
|
|
|
|
topic: function() {
|
|
|
|
var setup = setupLogging();
|
|
|
|
setup.compress.shouldError = true;
|
|
|
|
setup.logger.info('whatever');
|
|
|
|
return setup;
|
|
|
|
},
|
|
|
|
'should output to console.error': function(setup) {
|
|
|
|
assert.equal(setup.console.message, 'oh noes');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'with layout in configuration': {
|
|
|
|
topic: function() {
|
|
|
|
var setup = setupLogging({
|
|
|
|
layout: {
|
|
|
|
type: 'madeuplayout',
|
|
|
|
earlgrey: 'yes, please'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return setup;
|
|
|
|
},
|
|
|
|
'should pass options to layout': function(setup) {
|
|
|
|
assert.equal(setup.layouts.type, 'madeuplayout');
|
|
|
|
assert.equal(setup.layouts.options.earlgrey, 'yes, please');
|
|
|
|
}
|
2013-05-27 06:17:32 +08:00
|
|
|
}
|
|
|
|
}).export(module);
|