2 var vows = require('vows')
3 , assert = require('assert')
4 , sandbox = require('sandboxed-module')
5 , log4js = require('../lib/log4js')
6 , realLayouts = require('../lib/layouts')
7 , setupLogging = function(options, category, compressedLength) {
16 send: function(pkt, offset, pktLength, port, host) {
17 fakeDgram.sent = true;
20 this.packetLength = pktLength;
25 createSocket: function(type) {
31 gzip: function(objectToCompress, callback) {
32 fakeZlib.uncompressed = objectToCompress;
33 if (this.shouldError) {
34 callback({ stack: "oh noes" });
38 if (compressedLength) {
39 callback(null, { length: compressedLength });
41 callback(null, "I've been compressed");
47 error: function(message) {
48 this.message = message;
52 layout: function(type, options) {
54 this.options = options;
55 return realLayouts.messagePassThroughLayout;
57 messagePassThroughLayout: realLayouts.messagePassThroughLayout
59 , appender = sandbox.require('../lib/appenders/gelf', {
63 '../layouts': fakeLayouts
67 on: function(evt, handler) {
69 exitHandler = handler;
77 log4js.clearAppenders();
78 log4js.addAppender(appender.configure(options || {}), category || "gelf-test");
82 exitHandler: exitHandler,
85 logger: log4js.getLogger(category || "gelf-test")
89 vows.describe('log4js gelfAppender').addBatch({
91 'with default gelfAppender settings': {
93 var setup = setupLogging();
94 setup.logger.info("This is a test");
98 topic: function(setup) {
101 'should be sent via udp to the localhost gelf server': function(dgram) {
102 assert.equal(dgram.type, "udp4");
103 assert.equal(dgram.socket.host, "localhost");
104 assert.equal(dgram.socket.port, 12201);
105 assert.equal(dgram.socket.offset, 0);
106 assert.ok(dgram.socket.packetLength > 0, "Received blank message");
108 'should be compressed': function(dgram) {
109 assert.equal(dgram.socket.packet, "I've been compressed");
112 'the uncompressed log message': {
113 topic: function(setup) {
114 var message = JSON.parse(setup.compress.uncompressed);
117 'should be in the gelf format': function(message) {
118 assert.equal(message.version, '1.1');
119 assert.equal(message.host, require('os').hostname());
120 assert.equal(message.level, 6); //INFO
121 assert.equal(message.short_message, 'This is a test');
125 'with a message longer than 8k': {
127 var setup = setupLogging(undefined, undefined, 10240);
128 setup.logger.info("Blah.");
131 'the dgram packet': {
132 topic: function(setup) {
135 'should not be sent': function(dgram) {
136 assert.equal(dgram.sent, false);
140 'with non-default options': {
142 var setup = setupLogging({
148 setup.logger.debug("Just testing.");
151 'the dgram packet': {
152 topic: function(setup) {
155 'should pick up the options': function(dgram) {
156 assert.equal(dgram.socket.host, 'somewhere');
157 assert.equal(dgram.socket.port, 12345);
160 'the uncompressed packet': {
161 topic: function(setup) {
162 var message = JSON.parse(setup.compress.uncompressed);
165 'should pick up the options': function(message) {
166 assert.equal(message.host, 'cheese');
167 assert.equal(message._facility, 'nonsense');
174 var setup = setupLogging();
178 'should close open sockets': function(setup) {
179 assert.isTrue(setup.dgram.socket.closed);
185 var setup = setupLogging();
186 setup.compress.shouldError = true;
187 setup.logger.info('whatever');
190 'should output to console.error': function(setup) {
191 assert.equal(setup.console.message, 'oh noes');
195 'with layout in configuration': {
197 var setup = setupLogging({
199 type: 'madeuplayout',
200 earlgrey: 'yes, please'
205 'should pass options to layout': function(setup) {
206 assert.equal(setup.layouts.type, 'madeuplayout');
207 assert.equal(setup.layouts.options.earlgrey, 'yes, please');
211 'with custom fields options': {
213 var setup = setupLogging({
217 facility: 'nonsense',
219 _every1: 'Hello every one',
220 _every2: 'Hello every two'
225 _every2: 'Overwritten!',
226 _myField: 'This is my field!'
228 setup.logger.debug(myFields, "Just testing.");
231 'the dgram packet': {
232 topic: function(setup) {
235 'should pick up the options': function(dgram) {
236 assert.equal(dgram.socket.host, 'somewhere');
237 assert.equal(dgram.socket.port, 12345);
240 'the uncompressed packet': {
241 topic: function(setup) {
242 var message = JSON.parse(setup.compress.uncompressed);
245 'should pick up the options': function(message) {
246 assert.equal(message.host, 'cheese');
247 assert.isUndefined(message.GELF); // make sure flag was removed
248 assert.equal(message._facility, 'nonsense');
249 assert.equal(message._every1, 'Hello every one'); // the default value
250 assert.equal(message._every2, 'Overwritten!'); // the overwritten value
251 assert.equal(message._myField, 'This is my field!'); // the value for this message only
252 assert.equal(message.short_message, 'Just testing.'); // skip the field object