2 var vows = require('vows')
4 , path = require('path')
5 , sandbox = require('sandboxed-module')
6 , log4js = require('../lib/log4js')
7 , assert = require('assert')
8 , EOL = require('os').EOL || '\n';
10 log4js.clearAppenders();
12 function remove(filename) {
14 fs.unlinkSync(filename);
16 //doesn't really matter if it failed
20 vows.describe('log4js fileSyncAppender').addBatch({
21 'with default fileSyncAppender settings': {
24 , testFile = path.join(__dirname, '/fa-default-sync-test.log')
25 , logger = log4js.getLogger('default-settings');
28 log4js.clearAppenders();
30 require('../lib/appenders/fileSync').appender(testFile),
34 logger.info("This should be in the file.");
36 fs.readFile(testFile, "utf8", that.callback);
38 'should write log messages to the file': function (err, fileContents) {
39 assert.include(fileContents, "This should be in the file." + EOL);
41 'log messages should be in the basic layout format': function(err, fileContents) {
44 /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
48 'with a max file size and no backups': {
50 var testFile = path.join(__dirname, '/fa-maxFileSize-sync-test.log')
51 , logger = log4js.getLogger('max-file-size')
54 remove(testFile + '.1');
55 //log file of 100 bytes maximum, no backups
56 log4js.clearAppenders();
58 require('../lib/appenders/fileSync').appender(testFile, log4js.layouts.basicLayout, 100, 0),
61 logger.info("This is the first log message.");
62 logger.info("This is an intermediate log message.");
63 logger.info("This is the second log message.");
65 fs.readFile(testFile, "utf8", that.callback);
67 'log file should only contain the second message': function (err, fileContents) {
68 assert.include(fileContents, "This is the second log message." + EOL);
69 assert.equal(fileContents.indexOf("This is the first log message."), -1);
71 'the number of files': {
73 fs.readdir(__dirname, this.callback);
75 'starting with the test file name should be two': function(err, files) {
76 //there will always be one backup if you've specified a max log size
77 var logFiles = files.filter(
78 function(file) { return file.indexOf('fa-maxFileSize-sync-test.log') > -1; }
80 assert.equal(logFiles.length, 2);
84 'with a max file size and 2 backups': {
86 var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-sync-test.log')
87 , logger = log4js.getLogger('max-file-size-backups');
89 remove(testFile+'.1');
90 remove(testFile+'.2');
92 //log file of 50 bytes maximum, 2 backups
93 log4js.clearAppenders();
95 require('../lib/appenders/fileSync').appender(testFile, log4js.layouts.basicLayout, 50, 2),
96 'max-file-size-backups'
98 logger.info("This is the first log message.");
99 logger.info("This is the second log message.");
100 logger.info("This is the third log message.");
101 logger.info("This is the fourth log message.");
104 fs.readdir(__dirname, function(err, files) {
106 that.callback(null, files.sort());
108 that.callback(err, files);
113 topic: function(files) {
114 var logFiles = files.filter(
115 function(file) { return file.indexOf('fa-maxFileSize-with-backups-sync-test.log') > -1; }
119 'should be 3': function (files) {
120 assert.equal(files.length, 3);
122 'should be named in sequence': function (files) {
123 assert.deepEqual(files, [
124 'fa-maxFileSize-with-backups-sync-test.log',
125 'fa-maxFileSize-with-backups-sync-test.log.1',
126 'fa-maxFileSize-with-backups-sync-test.log.2'
129 'and the contents of the first file': {
130 topic: function(logFiles) {
131 fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback);
133 'should be the last log message': function(contents) {
134 assert.include(contents, 'This is the fourth log message.');
137 'and the contents of the second file': {
138 topic: function(logFiles) {
139 fs.readFile(path.join(__dirname, logFiles[1]), "utf8", this.callback);
141 'should be the third log message': function(contents) {
142 assert.include(contents, 'This is the third log message.');
145 'and the contents of the third file': {
146 topic: function(logFiles) {
147 fs.readFile(path.join(__dirname, logFiles[2]), "utf8", this.callback);
149 'should be the second log message': function(contents) {
150 assert.include(contents, 'This is the second log message.');
157 'with fileSyncAppender': {
159 var log4js = require('../lib/log4js')
161 //this config defines one file appender (to ./tmp-sync-tests.log)
162 //and sets the log level for "tests" to WARN
167 filename: "tmp-sync-tests.log",
168 layout: { type: "messagePassThrough" }
171 levels: { tests: "WARN" }
173 logger = log4js.getLogger('tests');
174 logger.info('this should not be written to the file');
175 logger.warn('this should be written to the file');
177 fs.readFile('tmp-sync-tests.log', 'utf8', this.callback);
179 'should load appender configuration from a json file': function(err, contents) {
180 assert.include(contents, 'this should be written to the file' + EOL);
181 assert.equal(contents.indexOf('this should not be written to the file'), -1);