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]
|
2013-02-08 21:54:18 +08:00
|
|
|
, event = args[1]
|
|
|
|
, tokens = args[2];
|
2011-07-15 06:22:44 +08:00
|
|
|
|
2013-02-08 21:54:18 +08:00
|
|
|
assert.equal(layout(pattern, tokens)(event), value);
|
2011-07-15 06:22:44 +08:00
|
|
|
}
|
|
|
|
|
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({
|
2011-09-15 06:18:24 +08:00
|
|
|
data: ["thing %d", 1, "cheese"]
|
2011-07-13 16:12:29 +08:00
|
|
|
, startTime: new Date(2010, 11, 5, 14, 18, 30, 45)
|
|
|
|
, categoryName: "cheese"
|
|
|
|
, level : {
|
|
|
|
colour: "green"
|
|
|
|
, toString: function() { return "ERROR"; }
|
|
|
|
}
|
2011-09-15 06:18:24 +08:00
|
|
|
}), "thing 1 'cheese'");
|
2011-07-22 10:28:02 +08:00
|
|
|
},
|
|
|
|
'should output the first item even if it is not a string': function(layout) {
|
|
|
|
assert.equal(layout({
|
|
|
|
data: [ { thing: 1} ]
|
|
|
|
, startTime: new Date(2010, 11, 5, 14, 18, 30, 45)
|
|
|
|
, categoryName: "cheese"
|
|
|
|
, level: {
|
|
|
|
colour: "green"
|
|
|
|
, toString: function() { return "ERROR"; }
|
|
|
|
}
|
|
|
|
}), "{ thing: 1 }");
|
2012-08-01 15:11:37 +08:00
|
|
|
},
|
2012-08-14 07:44:43 +08:00
|
|
|
'should print the stacks of a passed error objects': function(layout) {
|
2012-08-01 15:11:37 +08:00
|
|
|
assert.isArray(layout({
|
|
|
|
data: [ new Error() ]
|
|
|
|
, startTime: new Date(2010, 11, 5, 14, 18, 30, 45)
|
|
|
|
, categoryName: "cheese"
|
|
|
|
, level: {
|
|
|
|
colour: "green"
|
|
|
|
, toString: function() { return "ERROR"; }
|
|
|
|
}
|
2012-08-14 07:44:43 +08:00
|
|
|
}).match(/Error\s+at Object\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s+at runTest/)
|
2012-08-01 15:11:37 +08:00
|
|
|
, 'regexp did not return a match');
|
|
|
|
},
|
2012-08-14 07:44:43 +08:00
|
|
|
'with passed augmented errors':
|
2012-08-01 15:11:37 +08:00
|
|
|
{ topic:
|
2012-08-14 07:44:43 +08:00
|
|
|
function(layout){
|
2012-08-01 15:11:37 +08:00
|
|
|
var e = new Error("My Unique Error Message");
|
|
|
|
e.augmented = "My Unique attribute value"
|
|
|
|
e.augObj = { at1: "at2" }
|
|
|
|
return layout({
|
|
|
|
data: [ e ]
|
|
|
|
, startTime: new Date(2010, 11, 5, 14, 18, 30, 45)
|
|
|
|
, categoryName: "cheese"
|
|
|
|
, level: {
|
|
|
|
colour: "green"
|
|
|
|
, toString: function() { return "ERROR"; }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2012-08-14 07:44:43 +08:00
|
|
|
'should print error the contained error message': function(layoutOutput) {
|
2012-08-01 15:11:37 +08:00
|
|
|
var m = layoutOutput.match(/\{ \[Error: My Unique Error Message\]/);
|
|
|
|
assert.isArray(m);
|
|
|
|
},
|
2012-08-14 07:44:43 +08:00
|
|
|
'should print error augmented string attributes': function(layoutOutput) {
|
2012-08-01 15:11:37 +08:00
|
|
|
var m = layoutOutput.match(/augmented:\s'My Unique attribute value'/);
|
|
|
|
assert.isArray(m);
|
|
|
|
},
|
2012-08-14 07:44:43 +08:00
|
|
|
'should print error augmented object attributes': function(layoutOutput) {
|
2012-08-01 15:11:37 +08:00
|
|
|
var m = layoutOutput.match(/augObj:\s\{ at1: 'at2' \}/);
|
|
|
|
assert.isArray(m);
|
|
|
|
}
|
2011-07-22 10:28:02 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:11:37 +08:00
|
|
|
|
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-09-15 06:18:24 +08:00
|
|
|
event.data = ['this is a test', error];
|
2011-07-12 11:03:17 +08:00
|
|
|
output = layout(event);
|
|
|
|
lines = output.split(/\n/);
|
|
|
|
|
2012-08-01 15:11:37 +08:00
|
|
|
assert.equal(lines.length - 1, stack.length);
|
|
|
|
assert.equal(lines[0], "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]");
|
|
|
|
|
2011-07-12 11:03:17 +08:00
|
|
|
for (var i = 1; i < stack.length; i++) {
|
2012-08-01 15:11:37 +08:00
|
|
|
assert.equal(lines[i+2], stack[i+1]);
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
|
|
|
},
|
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-09-15 06:18:24 +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);
|
2011-07-26 06:52:40 +08:00
|
|
|
assert.equal(output, "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test { 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"; }
|
|
|
|
}
|
2013-02-08 21:54:18 +08:00
|
|
|
}, layout = require('../lib/layouts').patternLayout
|
|
|
|
, tokens = {
|
|
|
|
testString: 'testStringToken',
|
|
|
|
testFunction: function() { return 'testFunctionToken'; }
|
|
|
|
};
|
|
|
|
return [layout, event, tokens];
|
2011-07-15 06:22:44 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'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 ');
|
2012-11-23 05:14:13 +08:00
|
|
|
},
|
|
|
|
'%[%r%] should output colored time': function(args) {
|
|
|
|
test(args, '%[%r%]', '\033[36m14:18:30\033[39m');
|
2013-02-08 21:54:18 +08:00
|
|
|
},
|
|
|
|
'%x{testString} should output the string stored in tokens': function(args) {
|
|
|
|
test(args, '%x{testString}', 'testStringToken');
|
|
|
|
},
|
|
|
|
'%x{testFunction} should output the result of the function stored in tokens': function(args) {
|
|
|
|
test(args, '%x{testFunction}', 'testFunctionToken');
|
|
|
|
},
|
|
|
|
'%x{doesNotExist} should output the string stored in tokens': function(args) {
|
|
|
|
test(args, '%x{doesNotExist}', '%x{doesNotExist}');
|
|
|
|
},
|
|
|
|
'%x should output the string stored in tokens': function(args) {
|
|
|
|
test(args, '%x', '%x');
|
|
|
|
},
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
|
|
|
}).export(module);
|