2 var vows = require('vows')
4 , path = require('path')
5 , sandbox = require('sandboxed-module')
6 , log4js = require('../lib/log4js')
7 , assert = require('assert')
8 , zlib = require('zlib')
9 , EOL = require('os').EOL || '\n';
11 log4js.clearAppenders();
13 function remove(filename) {
15 fs.unlinkSync(filename);
17 //doesn't really matter if it failed
21 vows.describe('log4js fileAppender').addBatch({
22 'adding multiple fileAppenders': {
24 var listenersCount = process.listeners('exit').length
25 , logger = log4js.getLogger('default-settings')
29 logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
30 log4js.addAppender(require('../lib/appenders/file').appender(logfile), 'default-settings');
33 return listenersCount;
36 'does not add more than one `exit` listeners': function (initialCount) {
37 assert.ok(process.listeners('exit').length <= initialCount + 1);
45 , fileAppender = sandbox.require(
46 '../lib/appenders/file',
50 on: function(evt, listener) {
51 exitListener = listener;
57 RollingFileStream: function(filename) {
58 openedFiles.push(filename);
60 this.end = function() {
64 this.on = function() {};
70 for (var i=0; i < 5; i += 1) {
71 fileAppender.appender('test' + i, null, 100);
73 assert.isNotEmpty(openedFiles);
77 'should close all open files': function(openedFiles) {
78 assert.isEmpty(openedFiles);
82 'with default fileAppender settings': {
85 , testFile = path.join(__dirname, '/fa-default-test.log')
86 , logger = log4js.getLogger('default-settings');
89 log4js.clearAppenders();
90 log4js.addAppender(require('../lib/appenders/file').appender(testFile), 'default-settings');
92 logger.info("This should be in the file.");
94 setTimeout(function() {
95 fs.readFile(testFile, "utf8", that.callback);
98 'should write log messages to the file': function (err, fileContents) {
99 assert.include(fileContents, "This should be in the file." + EOL);
101 'log messages should be in the basic layout format': function(err, fileContents) {
104 /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
108 'fileAppender subcategories': {
112 log4js.clearAppenders();
114 function addAppender(cat) {
115 var testFile = path.join(
117 '/fa-subcategories-test-'+cat.join('-').replace(/\./g, "_")+'.log'
120 log4js.addAppender(require('../lib/appenders/file').appender(testFile), cat);
124 var file_sub1 = addAppender([ 'sub1']);
126 var file_sub1_sub12$sub1_sub13 = addAppender([ 'sub1.sub12', 'sub1.sub13' ]);
128 var file_sub1_sub12 = addAppender([ 'sub1.sub12' ]);
131 var logger_sub1_sub12_sub123 = log4js.getLogger('sub1.sub12.sub123');
133 var logger_sub1_sub13_sub133 = log4js.getLogger('sub1.sub13.sub133');
135 var logger_sub1_sub14 = log4js.getLogger('sub1.sub14');
137 var logger_sub2 = log4js.getLogger('sub2');
140 logger_sub1_sub12_sub123.info('sub1_sub12_sub123');
142 logger_sub1_sub13_sub133.info('sub1_sub13_sub133');
144 logger_sub1_sub14.info('sub1_sub14');
146 logger_sub2.info('sub2');
149 setTimeout(function() {
150 that.callback(null, {
151 file_sub1: fs.readFileSync(file_sub1).toString(),
152 file_sub1_sub12$sub1_sub13: fs.readFileSync(file_sub1_sub12$sub1_sub13).toString(),
153 file_sub1_sub12: fs.readFileSync(file_sub1_sub12).toString()
157 'check file contents': function (err, fileContents) {
159 // everything but category 'sub2'
161 fileContents.file_sub1,
162 /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] (sub1.sub12.sub123 - sub1_sub12_sub123|sub1.sub13.sub133 - sub1_sub13_sub133|sub1.sub14 - sub1_sub14)[\s\S]){3}$/ // jshint ignore:line
165 fileContents.file_sub1.match(/sub123/) &&
166 fileContents.file_sub1.match(/sub133/) &&
167 fileContents.file_sub1.match(/sub14/)
169 assert.ok(!fileContents.file_sub1.match(/sub2/));
171 // only catgories starting with 'sub1.sub12' and 'sub1.sub13'
173 fileContents.file_sub1_sub12$sub1_sub13,
174 /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] (sub1.sub12.sub123 - sub1_sub12_sub123|sub1.sub13.sub133 - sub1_sub13_sub133)[\s\S]){2}$/ //jshint ignore:line
177 fileContents.file_sub1_sub12$sub1_sub13.match(/sub123/) &&
178 fileContents.file_sub1_sub12$sub1_sub13.match(/sub133/)
180 assert.ok(!fileContents.file_sub1_sub12$sub1_sub13.match(/sub14|sub2/));
182 // only catgories starting with 'sub1.sub12'
184 fileContents.file_sub1_sub12,
185 /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] (sub1.sub12.sub123 - sub1_sub12_sub123)[\s\S]){1}$/ //jshint ignore:line
187 assert.ok(!fileContents.file_sub1_sub12.match(/sub14|sub2|sub13/));
191 'with a max file size and no backups': {
193 var testFile = path.join(__dirname, '/fa-maxFileSize-test.log')
194 , logger = log4js.getLogger('max-file-size')
197 remove(testFile + '.1');
198 //log file of 100 bytes maximum, no backups
199 log4js.clearAppenders();
201 require('../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 100, 0),
204 logger.info("This is the first log message.");
205 logger.info("This is an intermediate log message.");
206 logger.info("This is the second log message.");
207 //wait for the file system to catch up
208 setTimeout(function() {
209 fs.readFile(testFile, "utf8", that.callback);
212 'log file should only contain the second message': function(err, fileContents) {
213 assert.include(fileContents, "This is the second log message.");
214 assert.equal(fileContents.indexOf("This is the first log message."), -1);
216 'the number of files': {
218 fs.readdir(__dirname, this.callback);
220 'starting with the test file name should be two': function(err, files) {
221 //there will always be one backup if you've specified a max log size
222 var logFiles = files.filter(
223 function(file) { return file.indexOf('fa-maxFileSize-test.log') > -1; }
225 assert.equal(logFiles.length, 2);
229 'with a max file size and 2 backups': {
231 var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-test.log')
232 , logger = log4js.getLogger('max-file-size-backups');
234 remove(testFile+'.1');
235 remove(testFile+'.2');
237 //log file of 50 bytes maximum, 2 backups
238 log4js.clearAppenders();
240 require('../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 50, 2),
241 'max-file-size-backups'
243 logger.info("This is the first log message.");
244 logger.info("This is the second log message.");
245 logger.info("This is the third log message.");
246 logger.info("This is the fourth log message.");
248 //give the system a chance to open the stream
249 setTimeout(function() {
250 fs.readdir(__dirname, function(err, files) {
252 that.callback(null, files.sort());
254 that.callback(err, files);
260 topic: function(files) {
261 var logFiles = files.filter(
262 function(file) { return file.indexOf('fa-maxFileSize-with-backups-test.log') > -1; }
266 'should be 3': function (files) {
267 assert.equal(files.length, 3);
269 'should be named in sequence': function (files) {
270 assert.deepEqual(files, [
271 'fa-maxFileSize-with-backups-test.log',
272 'fa-maxFileSize-with-backups-test.log.1',
273 'fa-maxFileSize-with-backups-test.log.2'
276 'and the contents of the first file': {
277 topic: function(logFiles) {
278 fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback);
280 'should be the last log message': function(contents) {
281 assert.include(contents, 'This is the fourth log message.');
284 'and the contents of the second file': {
285 topic: function(logFiles) {
286 fs.readFile(path.join(__dirname, logFiles[1]), "utf8", this.callback);
288 'should be the third log message': function(contents) {
289 assert.include(contents, 'This is the third log message.');
292 'and the contents of the third file': {
293 topic: function(logFiles) {
294 fs.readFile(path.join(__dirname, logFiles[2]), "utf8", this.callback);
296 'should be the second log message': function(contents) {
297 assert.include(contents, 'This is the second log message.');
302 'with a max file size and 2 compressed backups': {
304 var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-compressed-test.log')
305 , logger = log4js.getLogger('max-file-size-backups');
307 remove(testFile+'.1.gz');
308 remove(testFile+'.2.gz');
310 //log file of 50 bytes maximum, 2 backups
311 log4js.clearAppenders();
313 require('../lib/appenders/file').appender(
314 testFile, log4js.layouts.basicLayout, 50, 2, true
316 'max-file-size-backups'
318 logger.info("This is the first log message.");
319 logger.info("This is the second log message.");
320 logger.info("This is the third log message.");
321 logger.info("This is the fourth log message.");
323 //give the system a chance to open the stream
324 setTimeout(function() {
325 fs.readdir(__dirname, function(err, files) {
327 that.callback(null, files.sort());
329 that.callback(err, files);
335 topic: function(files) {
336 var logFiles = files.filter(
338 return file.indexOf('fa-maxFileSize-with-backups-compressed-test.log') > -1;
343 'should be 3': function (files) {
344 assert.equal(files.length, 3);
346 'should be named in sequence': function (files) {
347 assert.deepEqual(files, [
348 'fa-maxFileSize-with-backups-compressed-test.log',
349 'fa-maxFileSize-with-backups-compressed-test.log.1.gz',
350 'fa-maxFileSize-with-backups-compressed-test.log.2.gz'
353 'and the contents of the first file': {
354 topic: function(logFiles) {
355 fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback);
357 'should be the last log message': function(contents) {
358 assert.include(contents, 'This is the fourth log message.');
361 'and the contents of the second file': {
362 topic: function(logFiles) {
363 zlib.gunzip(fs.readFileSync(path.join(__dirname, logFiles[1])), this.callback);
365 'should be the third log message': function(contents) {
366 assert.include(contents.toString('utf8'), 'This is the third log message.');
369 'and the contents of the third file': {
370 topic: function(logFiles) {
371 zlib.gunzip(fs.readFileSync(path.join(__dirname, logFiles[2])), this.callback);
373 'should be the second log message': function(contents) {
374 assert.include(contents.toString('utf8'), 'This is the second log message.');
381 'with fileAppender': {
383 var log4js = require('../lib/log4js')
385 //this config file defines one file appender (to ./tmp-tests.log)
386 //and sets the log level for "tests" to WARN
387 log4js.configure('./test/log4js.json');
388 logger = log4js.getLogger('tests');
389 logger.info('this should not be written to the file');
390 logger.warn('this should be written to the file');
392 fs.readFile('tmp-tests.log', 'utf8', this.callback);
394 'should load appender configuration from a json file': function (err, contents) {
395 assert.include(contents, 'this should be written to the file' + EOL);
396 assert.equal(contents.indexOf('this should not be written to the file'), -1);
401 'when underlying stream errors': {
405 , fileAppender = sandbox.require(
406 '../lib/appenders/file',
411 consoleArgs = Array.prototype.slice.call(arguments);
417 RollingFileStream: function(filename) {
419 this.end = function() {};
420 this.on = function(evt, cb) {
421 if (evt === 'error') {
430 fileAppender.appender('test1.log', null, 100);
431 errorHandler({ error: 'aargh' });
434 'should log the error to console.error': function(consoleArgs) {
435 assert.isNotEmpty(consoleArgs);
436 assert.equal(consoleArgs[0], 'log4js.fileAppender - Writing to file %s, error happened ');
437 assert.equal(consoleArgs[1], 'test1.log');
438 assert.equal(consoleArgs[2].error, 'aargh');