2 var vows = require('vows')
3 , assert = require('assert')
4 , path = require('path')
6 , sandbox = require('sandboxed-module')
7 , log4js = require('../lib/log4js')
8 , EOL = require('os').EOL || '\n';
10 function removeFile(filename) {
12 fs.unlink(path.join(__dirname, filename), function(err) {
14 console.log("Could not delete ", filename, err);
20 vows.describe('../lib/appenders/dateFile').addBatch({
22 'adding multiple dateFileAppenders': {
24 var listenersCount = process.listeners('exit').length,
25 dateFileAppender = require('../lib/appenders/dateFile'),
30 logfile = path.join(__dirname, 'datefa-default-test' + count + '.log');
31 log4js.addAppender(dateFileAppender.appender(logfile));
34 return listenersCount;
36 teardown: function() {
37 removeFile('datefa-default-test0.log')();
38 removeFile('datefa-default-test1.log')();
39 removeFile('datefa-default-test2.log')();
40 removeFile('datefa-default-test3.log')();
41 removeFile('datefa-default-test4.log')();
44 'should only add one `exit` listener': function (initialCount) {
45 assert.equal(process.listeners('exit').length, initialCount + 1);
54 , dateFileAppender = sandbox.require(
55 '../lib/appenders/dateFile',
59 on: function(evt, listener) {
60 exitListener = listener;
66 DateRollingFileStream: function(filename) {
67 openedFiles.push(filename);
69 this.end = function() {
77 for (var i=0; i < 5; i += 1) {
78 dateFileAppender.appender('test' + i);
80 assert.isNotEmpty(openedFiles);
84 'should close all open files': function(openedFiles) {
85 assert.isEmpty(openedFiles);
89 'with default settings': {
92 testFile = path.join(__dirname, 'date-appender-default.log'),
93 appender = require('../lib/appenders/dateFile').appender(testFile),
94 logger = log4js.getLogger('default-settings');
95 log4js.clearAppenders();
96 log4js.addAppender(appender, 'default-settings');
98 logger.info("This should be in the file.");
100 setTimeout(function() {
101 fs.readFile(testFile, "utf8", that.callback);
105 teardown: removeFile('date-appender-default.log'),
107 'should write to the file': function(contents) {
108 assert.include(contents, 'This should be in the file');
111 'should use the basic layout': function(contents) {
114 /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
122 'with dateFileAppender': {
124 var log4js = require('../lib/log4js')
126 //this config file defines one file appender (to ./date-file-test.log)
127 //and sets the log level for "tests" to WARN
128 log4js.configure('test/with-dateFile.json');
129 logger = log4js.getLogger('tests');
130 logger.info('this should not be written to the file');
131 logger.warn('this should be written to the file');
133 fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback);
135 teardown: removeFile('date-file-test.log'),
137 'should load appender configuration from a json file': function(err, contents) {
141 assert.include(contents, 'this should be written to the file' + EOL);
142 assert.equal(contents.indexOf('this should not be written to the file'), -1);
145 'with options.alwaysIncludePattern': {
148 , log4js = require('../lib/log4js')
149 , format = require('../lib/date_format')
156 "filename": "test/date-file-test",
157 "pattern": "-from-MM-dd.log",
158 "alwaysIncludePattern": true,
160 "type": "messagePassThrough"
165 , thisTime = format.asString(options.appenders[0].pattern, new Date());
167 path.join(__dirname, 'date-file-test' + thisTime),
168 "this is existing data" + EOL,
171 log4js.clearAppenders();
172 log4js.configure(options);
173 logger = log4js.getLogger('tests');
174 logger.warn('this should be written to the file with the appended date');
175 this.teardown = removeFile('date-file-test' + thisTime);
176 //wait for filesystem to catch up
177 setTimeout(function() {
178 fs.readFile(path.join(__dirname, 'date-file-test' + thisTime), 'utf8', self.callback);
181 'should create file with the correct pattern': function(contents) {
182 assert.include(contents, 'this should be written to the file with the appended date');
184 'should not overwrite the file on open (bug found in issue #132)': function(contents) {
185 assert.include(contents, 'this is existing data');
191 appender = sandbox.require(
192 '../lib/appenders/dateFile',
195 { DateRollingFileStream:
209 filename: "whatever.log",
212 { cwd: '/absolute/path/to' }
216 'should prepend options.cwd to config.filename': function (fileOpened) {
217 var expected = path.sep + path.join("absolute", "path", "to", "whatever.log");
218 assert.equal(fileOpened, expected);