2011-07-12 11:02:48 +08:00
|
|
|
var vows = require('vows')
|
|
|
|
, assert = require('assert')
|
|
|
|
, levels = require('../lib/levels');
|
2011-04-07 08:19:18 +08:00
|
|
|
|
|
|
|
function MockLogger() {
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
this.messages = [];
|
|
|
|
|
|
|
|
this.log = function(level, message, exception) {
|
|
|
|
that.messages.push({ level: level, message: message });
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isLevelEnabled = function(level) {
|
2011-07-12 11:02:48 +08:00
|
|
|
return level.isGreaterThanOrEqualTo(that.level);
|
2011-04-07 08:19:18 +08:00
|
|
|
};
|
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
this.level = levels.TRACE;
|
2011-04-07 08:19:18 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function MockRequest(remoteAddr, method, originalUrl) {
|
|
|
|
|
|
|
|
this.socket = { remoteAddress: remoteAddr };
|
|
|
|
this.originalUrl = originalUrl;
|
|
|
|
this.method = method;
|
|
|
|
this.httpVersionMajor = '5';
|
|
|
|
this.httpVersionMinor = '0';
|
|
|
|
this.headers = {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function MockResponse(statusCode) {
|
|
|
|
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
|
|
|
|
this.end = function(chunk, encoding) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vows.describe('log4js connect logger').addBatch({
|
|
|
|
'getConnectLoggerModule': {
|
|
|
|
topic: function() {
|
2011-07-12 11:02:48 +08:00
|
|
|
var clm = require('../lib/connect-logger');
|
2011-04-07 08:19:18 +08:00
|
|
|
return clm;
|
|
|
|
},
|
|
|
|
|
|
|
|
'should return a "connect logger" factory' : function(clm) {
|
|
|
|
assert.isObject(clm);
|
|
|
|
},
|
|
|
|
|
|
|
|
'take a log4js logger and return a "connect logger"' : {
|
|
|
|
topic: function(clm) {
|
|
|
|
var ml = new MockLogger();
|
|
|
|
var cl = clm.connectLogger(ml);
|
|
|
|
return cl;
|
|
|
|
},
|
|
|
|
|
|
|
|
'should return a "connect logger"': function(cl) {
|
|
|
|
assert.isFunction(cl);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'log events' : {
|
|
|
|
topic: function(clm) {
|
|
|
|
var ml = new MockLogger();
|
|
|
|
var cl = clm.connectLogger(ml);
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url');
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
cl(req, res, function() { });
|
2011-07-12 11:02:48 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return ml.messages;
|
2011-04-07 08:19:18 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
2011-11-16 05:39:07 +08:00
|
|
|
assert.equal(messages.length, 1);
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(messages[0].level, levels.INFO);
|
2011-04-07 08:19:18 +08:00
|
|
|
assert.include(messages[0].message, 'GET');
|
|
|
|
assert.include(messages[0].message, 'http://url');
|
|
|
|
assert.include(messages[0].message, 'my.remote.addr');
|
|
|
|
assert.include(messages[0].message, '200');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'log events with level below logging level' : {
|
|
|
|
topic: function(clm) {
|
|
|
|
var ml = new MockLogger();
|
2011-07-12 11:02:48 +08:00
|
|
|
ml.level = levels.FATAL;
|
2011-04-07 08:19:18 +08:00
|
|
|
var cl = clm.connectLogger(ml);
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url');
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
cl(req, res, function() { });
|
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return ml.messages;
|
|
|
|
},
|
|
|
|
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
|
|
|
assert.isEmpty(messages);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'log events with non-default level and custom format' : {
|
|
|
|
topic: function(clm) {
|
|
|
|
var ml = new MockLogger();
|
2011-07-12 11:02:48 +08:00
|
|
|
ml.level = levels.INFO;
|
|
|
|
var cl = clm.connectLogger(ml, { level: levels.INFO, format: ':method :url' } );
|
2011-04-07 08:19:18 +08:00
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url');
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
cl(req, res, function() { });
|
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return ml.messages;
|
|
|
|
},
|
|
|
|
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
2011-11-16 05:39:07 +08:00
|
|
|
assert.equal(messages.length, 1);
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(messages[0].level, levels.INFO);
|
2011-04-07 08:19:18 +08:00
|
|
|
assert.equal(messages[0].message, 'GET http://url');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).export(module);
|