2013-05-29 06:35:40 +08:00
|
|
|
"use strict";
|
|
|
|
var vows = require('vows')
|
|
|
|
, assert = require('assert')
|
|
|
|
, path = require('path')
|
|
|
|
, fs = require('fs')
|
|
|
|
, log4js = require('../lib/log4js');
|
2012-09-25 06:16:59 +08:00
|
|
|
|
|
|
|
function removeFile(filename) {
|
2013-03-20 06:14:27 +08:00
|
|
|
return function() {
|
|
|
|
fs.unlink(path.join(__dirname, filename), function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log("Could not delete ", filename, err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2012-09-25 06:16:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vows.describe('../lib/appenders/dateFile').addBatch({
|
2013-05-29 06:35:40 +08:00
|
|
|
'appender': {
|
|
|
|
'adding multiple dateFileAppenders': {
|
|
|
|
topic: function () {
|
|
|
|
var listenersCount = process.listeners('exit').length,
|
|
|
|
dateFileAppender = require('../lib/appenders/dateFile'),
|
|
|
|
count = 5,
|
|
|
|
logfile;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
logfile = path.join(__dirname, 'datefa-default-test' + count + '.log');
|
|
|
|
log4js.addAppender(dateFileAppender.appender(logfile));
|
2012-09-25 06:16:59 +08:00
|
|
|
}
|
2013-05-29 06:35:40 +08:00
|
|
|
|
|
|
|
return listenersCount;
|
|
|
|
},
|
|
|
|
teardown: function() {
|
|
|
|
removeFile('datefa-default-test0.log')();
|
|
|
|
removeFile('datefa-default-test1.log')();
|
|
|
|
removeFile('datefa-default-test2.log')();
|
|
|
|
removeFile('datefa-default-test3.log')();
|
|
|
|
removeFile('datefa-default-test4.log')();
|
|
|
|
},
|
|
|
|
|
|
|
|
'should only add one `exit` listener': function (initialCount) {
|
|
|
|
assert.equal(process.listeners('exit').length, initialCount + 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'with default settings': {
|
|
|
|
topic: function() {
|
|
|
|
var that = this,
|
|
|
|
testFile = path.join(__dirname, 'date-appender-default.log'),
|
|
|
|
appender = require('../lib/appenders/dateFile').appender(testFile),
|
|
|
|
logger = log4js.getLogger('default-settings');
|
|
|
|
log4js.clearAppenders();
|
|
|
|
log4js.addAppender(appender, 'default-settings');
|
|
|
|
|
|
|
|
logger.info("This should be in the file.");
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
fs.readFile(testFile, "utf8", that.callback);
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
},
|
|
|
|
teardown: removeFile('date-appender-default.log'),
|
|
|
|
|
|
|
|
'should write to the file': function(contents) {
|
|
|
|
assert.include(contents, 'This should be in the file');
|
|
|
|
},
|
|
|
|
|
|
|
|
'should use the basic layout': function(contents) {
|
|
|
|
assert.match(
|
|
|
|
contents,
|
|
|
|
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
|
|
|
|
);
|
|
|
|
}
|
2012-09-25 06:16:59 +08:00
|
|
|
}
|
2013-05-29 06:35:40 +08:00
|
|
|
|
|
|
|
}
|
2012-09-25 06:16:59 +08:00
|
|
|
}).addBatch({
|
2013-05-05 11:44:01 +08:00
|
|
|
'configure': {
|
|
|
|
'with dateFileAppender': {
|
2013-05-29 06:35:40 +08:00
|
|
|
topic: function() {
|
|
|
|
var log4js = require('../lib/log4js')
|
2013-05-05 11:44:01 +08:00
|
|
|
, logger;
|
2013-05-29 06:35:40 +08:00
|
|
|
//this config file defines one file appender (to ./date-file-test.log)
|
|
|
|
//and sets the log level for "tests" to WARN
|
2013-05-05 11:44:01 +08:00
|
|
|
log4js.configure('test/with-dateFile.json');
|
|
|
|
logger = log4js.getLogger('tests');
|
2013-05-29 06:35:40 +08:00
|
|
|
logger.info('this should not be written to the file');
|
|
|
|
logger.warn('this should be written to the file');
|
2013-05-05 11:44:01 +08:00
|
|
|
|
|
|
|
fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback);
|
2013-05-29 06:35:40 +08:00
|
|
|
},
|
2013-05-05 11:44:01 +08:00
|
|
|
teardown: removeFile('date-file-test.log'),
|
|
|
|
|
2013-05-29 06:35:40 +08:00
|
|
|
'should load appender configuration from a json file': function(err, contents) {
|
|
|
|
assert.include(contents, 'this should be written to the file' + require('os').EOL);
|
2013-05-05 11:44:01 +08:00
|
|
|
assert.equal(contents.indexOf('this should not be written to the file'), -1);
|
2013-05-29 06:35:40 +08:00
|
|
|
}
|
2013-05-05 11:44:01 +08:00
|
|
|
},
|
2013-05-29 06:35:40 +08:00
|
|
|
'with options.alwaysIncludePattern': {
|
|
|
|
topic: function() {
|
|
|
|
var log4js = require('../lib/log4js')
|
|
|
|
, format = require('../lib/date_format')
|
|
|
|
, logger
|
|
|
|
, options = {
|
|
|
|
"appenders": [
|
|
|
|
{
|
|
|
|
"category": "tests",
|
|
|
|
"type": "dateFile",
|
|
|
|
"filename": "test/date-file-test",
|
|
|
|
"pattern": "-from-MM-dd.log",
|
|
|
|
"alwaysIncludePattern": true,
|
|
|
|
"layout": {
|
|
|
|
"type": "messagePassThrough"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
, thisTime = format.asString(options.appenders[0].pattern, new Date());
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(__dirname, 'date-file-test' + thisTime),
|
|
|
|
"this is existing data" + require('os').EOL,
|
|
|
|
'utf8'
|
|
|
|
);
|
|
|
|
log4js.clearAppenders();
|
|
|
|
log4js.configure(options);
|
|
|
|
logger = log4js.getLogger('tests');
|
|
|
|
logger.warn('this should be written to the file with the appended date');
|
2013-05-05 11:44:01 +08:00
|
|
|
this.teardown = removeFile('date-file-test' + thisTime);
|
2013-05-29 06:35:40 +08:00
|
|
|
fs.readFile(path.join(__dirname, 'date-file-test' + thisTime), 'utf8', this.callback);
|
|
|
|
},
|
|
|
|
'should create file with the correct pattern': function(contents) {
|
|
|
|
assert.include(contents, 'this should be written to the file with the appended date');
|
|
|
|
},
|
|
|
|
'should not overwrite the file on open (bug found in issue #132)': function(contents) {
|
|
|
|
assert.include(contents, 'this is existing data');
|
|
|
|
}
|
|
|
|
}
|
2013-05-05 11:44:01 +08:00
|
|
|
|
|
|
|
}
|
2012-09-25 06:16:59 +08:00
|
|
|
}).exportTo(module);
|