2011-07-12 11:03:17 +08:00
|
|
|
var vows = require('vows'),
|
|
|
|
assert = require('assert');
|
|
|
|
|
2011-07-15 06:22:44 +08:00
|
|
|
//used for patternLayout tests.
|
|
|
|
function test(args, pattern, value) {
|
|
|
|
var layout = args[0]
|
|
|
|
, event = args[1];
|
|
|
|
|
|
|
|
assert.equal(layout(pattern)(event), value);
|
|
|
|
}
|
|
|
|
|
2011-07-12 11:03:17 +08:00
|
|
|
vows.describe('log4js layouts').addBatch({
|
|
|
|
'colouredLayout': {
|
|
|
|
topic: function() {
|
|
|
|
return require('../lib/layouts').colouredLayout;
|
|
|
|
},
|
|
|
|
|
|
|
|
'should apply level colour codes to output': function(layout) {
|
|
|
|
var output = layout({
|
2011-07-13 16:12:29 +08:00
|
|
|
data: ["nonsense"],
|
2011-07-12 11:03:17 +08:00
|
|
|
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
|
|
|
|
categoryName: "cheese",
|
|
|
|
level: {
|
|
|
|
toString: function() { return "ERROR"; }
|
|
|
|
}
|
|
|
|
});
|
2011-07-13 16:29:53 +08:00
|
|
|
assert.equal(output, '\033[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \033[39mnonsense');
|
2011-07-13 16:12:29 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'should support the console.log format for the message': function(layout) {
|
|
|
|
var output = layout({
|
|
|
|
data: ["thing %d", 2],
|
|
|
|
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
|
|
|
|
categoryName: "cheese",
|
|
|
|
level: {
|
|
|
|
toString: function() { return "ERROR"; }
|
|
|
|
}
|
|
|
|
});
|
2011-07-13 16:29:53 +08:00
|
|
|
assert.equal(output, '\033[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \033[39mthing 2');
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'messagePassThroughLayout': {
|
|
|
|
topic: function() {
|
|
|
|
return require('../lib/layouts').messagePassThroughLayout;
|
|
|
|
},
|
|
|
|
'should take a logevent and output only the message' : function(layout) {
|
|
|
|
assert.equal(layout({
|
2011-07-13 16:12:29 +08:00
|
|
|
data: ["nonsense"],
|
2011-07-12 11:03:17 +08:00
|
|
|
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
|
|
|
|
categoryName: "cheese",
|
|
|
|
level: {
|
|
|
|
colour: "green",
|
|
|
|
toString: function() { return "ERROR"; }
|
|
|
|
}
|
|
|
|
}), "nonsense");
|
2011-07-13 16:12:29 +08:00
|
|
|
},
|
|
|
|
'should support the console.log format for the message' : function(layout) {
|
|
|
|
assert.equal(layout({
|
|
|
|
data: ["thing %d", 1]
|
|
|
|
, startTime: new Date(2010, 11, 5, 14, 18, 30, 45)
|
|
|
|
, categoryName: "cheese"
|
|
|
|
, level : {
|
|
|
|
colour: "green"
|
|
|
|
, toString: function() { return "ERROR"; }
|
|
|
|
}
|
|
|
|
}), "thing 1");
|
|
|
|
}
|
2011-07-12 11:03:17 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'basicLayout': {
|
|
|
|
topic: function() {
|
|
|
|
var layout = require('../lib/layouts').basicLayout,
|
|
|
|
event = {
|
2011-07-13 16:12:29 +08:00
|
|
|
data: ['this is a test'],
|
2011-07-12 11:03:17 +08:00
|
|
|
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
|
|
|
|
categoryName: "tests",
|
|
|
|
level: {
|
|
|
|
toString: function() { return "DEBUG"; }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return [layout, event];
|
|
|
|
},
|
|
|
|
'should take a logevent and output a formatted string': function(args) {
|
|
|
|
var layout = args[0], event = args[1];
|
|
|
|
assert.equal(layout(event), "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test");
|
|
|
|
},
|
|
|
|
'should output a stacktrace, message if the event has an error attached': function(args) {
|
|
|
|
var layout = args[0], event = args[1], output, lines,
|
|
|
|
error = new Error("Some made-up error"),
|
|
|
|
stack = error.stack.split(/\n/);
|
|
|
|
|
2011-07-13 16:12:29 +08:00
|
|
|
event.data = ['this is a test', error];
|
2011-07-12 11:03:17 +08:00
|
|
|
output = layout(event);
|
|
|
|
lines = output.split(/\n/);
|
|
|
|
|
|
|
|
assert.length(lines, stack.length+1);
|
|
|
|
assert.equal(lines[0], "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(lines[1], "Error: Some made-up error");
|
2011-07-12 11:03:17 +08:00
|
|
|
for (var i = 1; i < stack.length; i++) {
|
|
|
|
assert.equal(lines[i+1], stack[i]);
|
|
|
|
}
|
|
|
|
},
|
2011-07-13 16:12:29 +08:00
|
|
|
'should output any extra data in the log event as util.inspect strings': function(args) {
|
2011-07-12 11:03:17 +08:00
|
|
|
var layout = args[0], event = args[1], output, lines;
|
2011-07-13 16:12:29 +08:00
|
|
|
event.data = ['this is a test', {
|
2011-07-12 11:03:17 +08:00
|
|
|
name: 'Cheese',
|
|
|
|
message: 'Gorgonzola smells.'
|
2011-07-13 16:12:29 +08:00
|
|
|
}];
|
2011-07-12 11:03:17 +08:00
|
|
|
output = layout(event);
|
|
|
|
lines = output.split(/\n/);
|
|
|
|
|
|
|
|
assert.length(lines, 2);
|
|
|
|
assert.equal(lines[0], "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(lines[1], "{ name: 'Cheese', message: 'Gorgonzola smells.' }");
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
2011-07-15 06:22:44 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'patternLayout': {
|
|
|
|
topic: function() {
|
|
|
|
var event = {
|
|
|
|
data: ['this is a test'],
|
|
|
|
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
|
|
|
|
categoryName: "multiple.levels.of.tests",
|
|
|
|
level: {
|
|
|
|
toString: function() { return "DEBUG"; }
|
|
|
|
}
|
|
|
|
}, layout = require('../lib/layouts').patternLayout;
|
|
|
|
return [layout, event];
|
|
|
|
},
|
|
|
|
|
|
|
|
'should default to "time logLevel loggerName - message"': function(args) {
|
|
|
|
test(args, null, "14:18:30 DEBUG multiple.levels.of.tests - this is a test\n");
|
|
|
|
},
|
|
|
|
'%r should output time only': function(args) {
|
|
|
|
test(args, '%r', '14:18:30');
|
|
|
|
},
|
|
|
|
'%p should output the log level': function(args) {
|
|
|
|
test(args, '%p', 'DEBUG');
|
|
|
|
},
|
|
|
|
'%c should output the log category': function(args) {
|
|
|
|
test(args, '%c', 'multiple.levels.of.tests');
|
|
|
|
},
|
|
|
|
'%m should output the log data': function(args) {
|
|
|
|
test(args, '%m', 'this is a test');
|
|
|
|
},
|
|
|
|
'%n should output a new line': function(args) {
|
|
|
|
test(args, '%n', '\n');
|
|
|
|
},
|
|
|
|
'%c should handle category names like java-style package names': function(args) {
|
|
|
|
test(args, '%c{1}', 'tests');
|
|
|
|
test(args, '%c{2}', 'of.tests');
|
|
|
|
test(args, '%c{3}', 'levels.of.tests');
|
|
|
|
test(args, '%c{4}', 'multiple.levels.of.tests');
|
|
|
|
test(args, '%c{5}', 'multiple.levels.of.tests');
|
|
|
|
test(args, '%c{99}', 'multiple.levels.of.tests');
|
|
|
|
},
|
|
|
|
'%d should output the date in ISO8601 format': function(args) {
|
|
|
|
test(args, '%d', '2010-12-05 14:18:30.045');
|
|
|
|
},
|
|
|
|
'%d should allow for format specification': function(args) {
|
|
|
|
test(args, '%d{ISO8601}', '2010-12-05 14:18:30.045');
|
|
|
|
test(args, '%d{ABSOLUTE}', '14:18:30.045');
|
|
|
|
test(args, '%d{DATE}', '05 12 2010 14:18:30.045');
|
|
|
|
test(args, '%d{yyyy MM dd}', '2010 12 05');
|
|
|
|
test(args, '%d{yyyy MM dd hh mm ss SSS}', '2010 12 05 14 18 30 045');
|
|
|
|
},
|
|
|
|
'%% should output %': function(args) {
|
|
|
|
test(args, '%%', '%');
|
|
|
|
},
|
|
|
|
'should output anything not preceded by % as literal': function(args) {
|
|
|
|
test(args, 'blah blah blah', 'blah blah blah');
|
|
|
|
},
|
|
|
|
'should handle complicated patterns': function(args) {
|
|
|
|
test(args,
|
|
|
|
'%m%n %c{2} at %d{ABSOLUTE} cheese %p%n',
|
|
|
|
'this is a test\n of.tests at 14:18:30.045 cheese DEBUG\n'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
'should truncate fields if specified': function(args) {
|
|
|
|
test(args, '%.4m', 'this');
|
|
|
|
test(args, '%.7m', 'this is');
|
|
|
|
test(args, '%.9m', 'this is a');
|
|
|
|
test(args, '%.14m', 'this is a test');
|
|
|
|
test(args, '%.2919102m', 'this is a test');
|
|
|
|
},
|
|
|
|
'should pad fields if specified': function(args) {
|
|
|
|
test(args, '%10p', ' DEBUG');
|
|
|
|
test(args, '%8p', ' DEBUG');
|
|
|
|
test(args, '%6p', ' DEBUG');
|
|
|
|
test(args, '%4p', 'DEBUG');
|
|
|
|
test(args, '%-4p', 'DEBUG');
|
|
|
|
test(args, '%-6p', 'DEBUG ');
|
|
|
|
test(args, '%-8p', 'DEBUG ');
|
|
|
|
test(args, '%-10p', 'DEBUG ');
|
|
|
|
}
|
|
|
|
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
2011-07-15 06:22:44 +08:00
|
|
|
|
2011-07-12 11:03:17 +08:00
|
|
|
}).export(module);
|
|
|
|
|