2 var vows = require('vows')
3 , assert = require('assert')
5 , semver = require('semver')
6 , EOL = os.EOL || '\n';
8 //used for patternLayout tests.
9 function test(args, pattern, value) {
14 assert.equal(layout(pattern, tokens)(event), value);
17 vows.describe('log4js layouts').addBatch({
20 return require('../lib/layouts').colouredLayout;
23 'should apply level colour codes to output': function(layout) {
26 startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
27 categoryName: "cheese",
29 toString: function() { return "ERROR"; }
32 assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mnonsense');
34 'should support the console.log format for the message': function(layout) {
36 data: ["thing %d", 2],
37 startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
38 categoryName: "cheese",
40 toString: function() { return "ERROR"; }
43 assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mthing 2');
47 'messagePassThroughLayout': {
49 return require('../lib/layouts').messagePassThroughLayout;
51 'should take a logevent and output only the message' : function(layout) {
54 startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
55 categoryName: "cheese",
58 toString: function() { return "ERROR"; }
62 'should support the console.log format for the message' : function(layout) {
64 data: ["thing %d", 1, "cheese"],
65 startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
66 categoryName: "cheese",
69 toString: function() { return "ERROR"; }
71 }), "thing 1 cheese");
73 'should output the first item even if it is not a string': function(layout) {
75 data: [ { thing: 1} ],
76 startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
77 categoryName: "cheese",
80 toString: function() { return "ERROR"; }
84 'should print the stacks of a passed error objects': function(layout) {
85 assert.isArray(layout({
86 data: [ new Error() ],
87 startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
88 categoryName: "cheese",
91 toString: function() { return "ERROR"; }
93 }).match(/Error\s+at Object\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s+at runTest/)
94 , 'regexp did not return a match');
96 'with passed augmented errors': {
97 topic: function(layout){
98 var e = new Error("My Unique Error Message");
99 e.augmented = "My Unique attribute value";
100 e.augObj = { at1: "at2" };
103 startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
104 categoryName: "cheese",
107 toString: function() { return "ERROR"; }
111 'should print error the contained error message': function(layoutOutput) {
112 var m = layoutOutput.match(/Error: My Unique Error Message/);
115 'should print error augmented string attributes': function(layoutOutput) {
116 var m = layoutOutput.match(/augmented:\s'My Unique attribute value'/);
119 'should print error augmented object attributes': function(layoutOutput) {
120 var m = layoutOutput.match(/augObj:\s\{ at1: 'at2' \}/);
130 var layout = require('../lib/layouts').basicLayout,
132 data: ['this is a test'],
133 startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
134 categoryName: "tests",
136 toString: function() { return "DEBUG"; }
139 return [layout, event];
141 'should take a logevent and output a formatted string': function(args) {
142 var layout = args[0], event = args[1];
143 assert.equal(layout(event), "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test");
145 'should output a stacktrace, message if the event has an error attached': function(args) {
146 var i, layout = args[0], event = args[1], output, lines,
147 error = new Error("Some made-up error"),
148 stack = error.stack.split(/\n/);
150 event.data = ['this is a test', error];
151 output = layout(event);
152 lines = output.split(/\n/);
154 if (semver.satisfies(process.version, '>=6')) {
155 assert.equal(lines.length, stack.length);
158 "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test Error: Some made-up error"
160 for (i = 1; i < stack.length; i++) {
161 assert.equal(lines[i], stack[i]);
164 assert.equal(lines.length - 1, stack.length);
167 "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]"
169 for (i = 1; i < stack.length; i++) {
170 assert.equal(lines[i+2], stack[i+1]);
175 'should output any extra data in the log event as util.inspect strings': function(args) {
176 var layout = args[0], event = args[1], output, lines;
177 event.data = ['this is a test', {
179 message: 'Gorgonzola smells.'
181 output = layout(event);
184 "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test " +
185 "{ name: 'Cheese', message: 'Gorgonzola smells.' }"
193 data: ['this is a test'],
194 startTime: new Date('2010-12-05T14:18:30.045Z'), //new Date(2010, 11, 5, 14, 18, 30, 45),
195 categoryName: "multiple.levels.of.tests",
197 toString: function() { return "DEBUG"; }
199 }, layout = require('../lib/layouts').patternLayout
201 testString: 'testStringToken',
202 testFunction: function() { return 'testFunctionToken'; },
203 fnThatUsesLogEvent: function(logEvent) { return logEvent.level.toString(); }
206 //override getTimezoneOffset
207 event.startTime.getTimezoneOffset = function() { return 0; };
208 return [layout, event, tokens];
211 'should default to "time logLevel loggerName - message"': function(args) {
212 test(args, null, "14:18:30 DEBUG multiple.levels.of.tests - this is a test" + EOL);
214 '%r should output time only': function(args) {
215 test(args, '%r', '14:18:30');
217 '%p should output the log level': function(args) {
218 test(args, '%p', 'DEBUG');
220 '%c should output the log category': function(args) {
221 test(args, '%c', 'multiple.levels.of.tests');
223 '%m should output the log data': function(args) {
224 test(args, '%m', 'this is a test');
226 '%n should output a new line': function(args) {
227 test(args, '%n', EOL);
229 '%h should output hostname' : function(args) {
230 test(args, '%h', os.hostname().toString());
232 '%z should output pid' : function(args) {
233 test(args, '%z', process.pid);
235 '%c should handle category names like java-style package names': function(args) {
236 test(args, '%c{1}', 'tests');
237 test(args, '%c{2}', 'of.tests');
238 test(args, '%c{3}', 'levels.of.tests');
239 test(args, '%c{4}', 'multiple.levels.of.tests');
240 test(args, '%c{5}', 'multiple.levels.of.tests');
241 test(args, '%c{99}', 'multiple.levels.of.tests');
243 '%d should output the date in ISO8601 format': function(args) {
244 test(args, '%d', '2010-12-05 14:18:30.045');
246 '%d should allow for format specification': function(args) {
247 test(args, '%d{ISO8601_WITH_TZ_OFFSET}', '2010-12-05T14:18:30-0000');
248 test(args, '%d{ISO8601}', '2010-12-05 14:18:30.045');
249 test(args, '%d{ABSOLUTE}', '14:18:30.045');
250 test(args, '%d{DATE}', '05 12 2010 14:18:30.045');
251 test(args, '%d{yy MM dd hh mm ss}', '10 12 05 14 18 30');
252 test(args, '%d{yyyy MM dd}', '2010 12 05');
253 test(args, '%d{yyyy MM dd hh mm ss SSS}', '2010 12 05 14 18 30 045');
255 '%% should output %': function(args) {
256 test(args, '%%', '%');
258 'should output anything not preceded by % as literal': function(args) {
259 test(args, 'blah blah blah', 'blah blah blah');
261 'should output the original string if no replacer matches the token': function(args) {
262 test(args, '%a{3}', 'a{3}');
264 'should handle complicated patterns': function(args) {
266 '%m%n %c{2} at %d{ABSOLUTE} cheese %p%n',
267 'this is a test'+ EOL +' of.tests at 14:18:30.045 cheese DEBUG' + EOL
270 'should truncate fields if specified': function(args) {
271 test(args, '%.4m', 'this');
272 test(args, '%.7m', 'this is');
273 test(args, '%.9m', 'this is a');
274 test(args, '%.14m', 'this is a test');
275 test(args, '%.2919102m', 'this is a test');
277 'should pad fields if specified': function(args) {
278 test(args, '%10p', ' DEBUG');
279 test(args, '%8p', ' DEBUG');
280 test(args, '%6p', ' DEBUG');
281 test(args, '%4p', 'DEBUG');
282 test(args, '%-4p', 'DEBUG');
283 test(args, '%-6p', 'DEBUG ');
284 test(args, '%-8p', 'DEBUG ');
285 test(args, '%-10p', 'DEBUG ');
287 '%[%r%] should output colored time': function(args) {
288 test(args, '%[%r%]', '\x1B[36m14:18:30\x1B[39m');
290 '%x{testString} should output the string stored in tokens': function(args) {
291 test(args, '%x{testString}', 'testStringToken');
293 '%x{testFunction} should output the result of the function stored in tokens': function(args) {
294 test(args, '%x{testFunction}', 'testFunctionToken');
296 '%x{doesNotExist} should output the string stored in tokens': function(args) {
297 test(args, '%x{doesNotExist}', 'null');
299 '%x{fnThatUsesLogEvent} should be able to use the logEvent': function(args) {
300 test(args, '%x{fnThatUsesLogEvent}', 'DEBUG');
302 '%x should output the string stored in tokens': function(args) {
303 test(args, '%x', 'null');
307 topic: require('../lib/layouts'),
308 'should have a maker for each layout': function(layouts) {
309 assert.ok(layouts.layout("messagePassThrough"));
310 assert.ok(layouts.layout("basic"));
311 assert.ok(layouts.layout("colored"));
312 assert.ok(layouts.layout("coloured"));
313 assert.ok(layouts.layout("pattern"));
317 topic: require('../lib/layouts'),
318 'should be able to add a layout': function(layouts) {
319 layouts.addLayout('test_layout', function(config){
320 assert.equal(config, 'test_config');
321 return function(logEvent) {
322 return "TEST LAYOUT >"+logEvent.data;
325 var serializer = layouts.layout('test_layout', 'test_config');
326 assert.ok(serializer);
327 assert.equal(serializer({data: "INPUT"}), "TEST LAYOUT >INPUT");