mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
converted date file appender tests to mocha
This commit is contained in:
parent
3312724d7d
commit
d43d49d83d
@ -136,7 +136,9 @@ function dispatch(event) {
|
||||
|
||||
function load(file) {
|
||||
debug("loading ", file);
|
||||
return JSON.parse(fs.readFileSync(file, "utf-8"));
|
||||
var contents = fs.readFileSync(file, "utf-8");
|
||||
debug("file contents ", contents);
|
||||
return JSON.parse(contents);
|
||||
}
|
||||
|
||||
function configure(configurationFileOrObject) {
|
||||
|
||||
@ -1,153 +1,173 @@
|
||||
"use strict";
|
||||
var vows = require('vows')
|
||||
, assert = require('assert')
|
||||
var should = require('should')
|
||||
, async = require('async')
|
||||
, path = require('path')
|
||||
, fs = require('fs')
|
||||
, sandbox = require('sandboxed-module')
|
||||
, log4js = require('../lib/log4js');
|
||||
, sandbox = require('sandboxed-module');
|
||||
|
||||
function removeFile(filename) {
|
||||
return function() {
|
||||
fs.unlink(path.join(__dirname, filename), function(err) {
|
||||
if (err) {
|
||||
console.log("Could not delete ", filename, err);
|
||||
}
|
||||
});
|
||||
};
|
||||
function remove(filename, cb) {
|
||||
fs.unlink(path.join(__dirname, filename), function(err) {
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
vows.describe('../lib/appenders/dateFile').addBatch({
|
||||
'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));
|
||||
}
|
||||
|
||||
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')();
|
||||
},
|
||||
describe('../lib/appenders/dateFile', function() {
|
||||
describe('adding multiple dateFileAppenders', function() {
|
||||
var files = [], initialListeners;
|
||||
|
||||
before(function() {
|
||||
var dateFileAppender = require('../lib/appenders/dateFile'),
|
||||
count = 5,
|
||||
logfile;
|
||||
|
||||
initialListeners = process.listeners('exit').length;
|
||||
|
||||
'should only add one `exit` listener': function (initialCount) {
|
||||
assert.equal(process.listeners('exit').length, initialCount + 1);
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
'exit listener': {
|
||||
topic: function() {
|
||||
var exitListener
|
||||
, openedFiles = []
|
||||
, dateFileAppender = sandbox.require(
|
||||
'../lib/appenders/dateFile',
|
||||
{
|
||||
globals: {
|
||||
process: {
|
||||
on: function(evt, listener) {
|
||||
exitListener = listener;
|
||||
}
|
||||
}
|
||||
},
|
||||
requires: {
|
||||
'../streams': {
|
||||
DateRollingFileStream: function(filename) {
|
||||
openedFiles.push(filename);
|
||||
|
||||
this.end = function() {
|
||||
openedFiles.shift();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
for (var i=0; i < 5; i += 1) {
|
||||
dateFileAppender.appender('test' + i);
|
||||
}
|
||||
assert.isNotEmpty(openedFiles);
|
||||
exitListener();
|
||||
return openedFiles;
|
||||
},
|
||||
'should close all open files': function(openedFiles) {
|
||||
assert.isEmpty(openedFiles);
|
||||
while (count--) {
|
||||
logfile = path.join(__dirname, 'datefa-default-test' + count + '.log');
|
||||
dateFileAppender.configure({
|
||||
filename: logfile
|
||||
});
|
||||
files.push(logfile);
|
||||
}
|
||||
},
|
||||
|
||||
'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'),
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
async.forEach(files, remove, done);
|
||||
});
|
||||
|
||||
'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 - /
|
||||
);
|
||||
it('should only add one `exit` listener', function () {
|
||||
process.listeners('exit').length.should.be.below(initialListeners + 2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('exit listener', function() {
|
||||
var openedFiles = [];
|
||||
|
||||
before(function() {
|
||||
var exitListener
|
||||
, dateFileAppender = sandbox.require(
|
||||
'../lib/appenders/dateFile',
|
||||
{
|
||||
globals: {
|
||||
process: {
|
||||
on: function(evt, listener) {
|
||||
exitListener = listener;
|
||||
}
|
||||
}
|
||||
},
|
||||
requires: {
|
||||
'../streams': {
|
||||
DateRollingFileStream: function(filename) {
|
||||
openedFiles.push(filename);
|
||||
|
||||
this.end = function() {
|
||||
openedFiles.shift();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
for (var i=0; i < 5; i += 1) {
|
||||
dateFileAppender.configure({
|
||||
filename: 'test' + i
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}).addBatch({
|
||||
'configure': {
|
||||
'with dateFileAppender': {
|
||||
topic: function() {
|
||||
|
||||
openedFiles.should.not.be.empty;
|
||||
exitListener();
|
||||
});
|
||||
|
||||
it('should close all open files', function() {
|
||||
openedFiles.should.be.empty;
|
||||
});
|
||||
});
|
||||
|
||||
describe('with default settings', function() {
|
||||
var contents;
|
||||
|
||||
before(function(done) {
|
||||
var testFile = path.join(__dirname, 'date-appender-default.log'),
|
||||
log4js = require('../lib/log4js'),
|
||||
logger = log4js.getLogger('default-settings');
|
||||
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
"date": { type: "dateFile", filename: testFile }
|
||||
},
|
||||
categories: {
|
||||
default: { level: "debug", appenders: [ "date" ] }
|
||||
}
|
||||
});
|
||||
|
||||
logger.info("This should be in the file.");
|
||||
|
||||
setTimeout(function() {
|
||||
fs.readFile(testFile, "utf8", function(err, data) {
|
||||
contents = data;
|
||||
done(err);
|
||||
});
|
||||
}, 100);
|
||||
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
remove('date-appender-default.log', done);
|
||||
});
|
||||
|
||||
it('should write to the file', function() {
|
||||
contents.should.include('This should be in the file');
|
||||
});
|
||||
|
||||
it('should use the basic layout', function() {
|
||||
contents.should.match(
|
||||
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('configure', function() {
|
||||
describe('with dateFileAppender', function() {
|
||||
var contents;
|
||||
|
||||
before(function(done) {
|
||||
var log4js = require('../lib/log4js')
|
||||
, logger;
|
||||
, logger = log4js.getLogger('tests');
|
||||
|
||||
//this config file defines one file appender (to ./date-file-test.log)
|
||||
//and sets the log level for "tests" to WARN
|
||||
log4js.configure('test/with-dateFile.json');
|
||||
logger = log4js.getLogger('tests');
|
||||
logger.info('this should not be written to the file');
|
||||
logger.warn('this should be written to the file');
|
||||
|
||||
fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback);
|
||||
},
|
||||
teardown: removeFile('date-file-test.log'),
|
||||
fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', function(err, data) {
|
||||
contents = data;
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
remove('date-file-test.log', done);
|
||||
});
|
||||
|
||||
'should load appender configuration from a json file': function(err, contents) {
|
||||
assert.include(contents, 'this should be written to the file' + require('os').EOL);
|
||||
assert.equal(contents.indexOf('this should not be written to the file'), -1);
|
||||
}
|
||||
},
|
||||
'with options.alwaysIncludePattern': {
|
||||
topic: function() {
|
||||
var self = this
|
||||
, log4js = require('../lib/log4js')
|
||||
it('should load appender configuration from a json file', function() {
|
||||
contents.should.include('this should be written to the file' + require('os').EOL);
|
||||
contents.should.not.include('this should not be written to the file');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with options.alwaysIncludePattern', function() {
|
||||
var contents, thisTime;
|
||||
|
||||
before(function(done) {
|
||||
var log4js = require('../lib/log4js')
|
||||
, format = require('../lib/date_format')
|
||||
, logger
|
||||
, options = {
|
||||
"appenders": [
|
||||
{
|
||||
"category": "tests",
|
||||
"appenders": {
|
||||
"datefile": {
|
||||
"type": "dateFile",
|
||||
"filename": "test/date-file-test",
|
||||
"pattern": "-from-MM-dd.log",
|
||||
@ -156,63 +176,45 @@ vows.describe('../lib/appenders/dateFile').addBatch({
|
||||
"type": "messagePassThrough"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
, thisTime = format.asString(options.appenders[0].pattern, new Date());
|
||||
fs.writeFileSync(
|
||||
},
|
||||
categories: { default: { level: "debug", appenders: [ "datefile" ] } }
|
||||
};
|
||||
thisTime = format.asString(options.appenders.datefile.pattern, new Date());
|
||||
|
||||
fs.writeFile(
|
||||
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');
|
||||
this.teardown = removeFile('date-file-test' + thisTime);
|
||||
//wait for filesystem to catch up
|
||||
setTimeout(function() {
|
||||
fs.readFile(path.join(__dirname, 'date-file-test' + thisTime), 'utf8', self.callback);
|
||||
}, 100);
|
||||
},
|
||||
'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');
|
||||
}
|
||||
},
|
||||
'with cwd option': {
|
||||
topic: function() {
|
||||
var fileOpened,
|
||||
appender = sandbox.require(
|
||||
'../lib/appenders/dateFile',
|
||||
{ requires:
|
||||
{ '../streams':
|
||||
{ DateRollingFileStream:
|
||||
function(file) {
|
||||
fileOpened = file;
|
||||
return {
|
||||
on: function() {},
|
||||
end: function() {}
|
||||
};
|
||||
'utf8',
|
||||
function(err) {
|
||||
log4js.configure(options);
|
||||
logger = log4js.getLogger('tests');
|
||||
logger.warn('this should be written to the file with the appended date');
|
||||
//wait for filesystem to catch up
|
||||
setTimeout(function() {
|
||||
fs.readFile(
|
||||
path.join(__dirname, 'date-file-test' + thisTime),
|
||||
'utf8',
|
||||
function(err, data) {
|
||||
contents = data;
|
||||
done(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}, 100);
|
||||
}
|
||||
);
|
||||
appender.configure(
|
||||
{
|
||||
filename: "whatever.log",
|
||||
maxLogSize: 10
|
||||
},
|
||||
{ cwd: '/absolute/path/to' }
|
||||
);
|
||||
return fileOpened;
|
||||
},
|
||||
'should prepend options.cwd to config.filename': function(fileOpened) {
|
||||
assert.equal(fileOpened, "/absolute/path/to/whatever.log");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}).exportTo(module);
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
remove('date-file-test' + thisTime, done);
|
||||
});
|
||||
|
||||
it('should create file with the correct pattern', function() {
|
||||
contents.should.include('this should be written to the file with the appended date');
|
||||
});
|
||||
|
||||
it('should not overwrite the file on open (bug found in issue #132)', function() {
|
||||
contents.should.include('this is existing data');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,22 +1,19 @@
|
||||
"use strict";
|
||||
var fs = require('fs')
|
||||
, async = require('async')
|
||||
, path = require('path')
|
||||
, sandbox = require('sandboxed-module')
|
||||
, log4js = require('../lib/log4js')
|
||||
, should = require('should');
|
||||
|
||||
function remove(filename) {
|
||||
try {
|
||||
fs.unlinkSync(filename);
|
||||
} catch (e) {
|
||||
//doesn't really matter if it failed
|
||||
}
|
||||
function remove(filename, cb) {
|
||||
fs.unlink(filename, function(err) { cb(); });
|
||||
}
|
||||
|
||||
describe('log4js fileAppender', function() {
|
||||
|
||||
describe('adding multiple fileAppenders', function() {
|
||||
var initialCount, listenersCount;
|
||||
var files = [], initialCount, listenersCount;
|
||||
|
||||
before(function() {
|
||||
var logfile
|
||||
@ -28,12 +25,17 @@ describe('log4js fileAppender', function() {
|
||||
while (count--) {
|
||||
logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
|
||||
config.appenders["file" + count] = { type: "file", filename: logfile };
|
||||
files.push(logfile);
|
||||
}
|
||||
|
||||
log4js.configure(config);
|
||||
|
||||
listenersCount = process.listeners('exit').length;
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
async.forEach(files, remove, done);
|
||||
});
|
||||
|
||||
it('does not add more than one `exit` listeners', function () {
|
||||
listenersCount.should.be.below(initialCount + 2);
|
||||
@ -83,34 +85,38 @@ describe('log4js fileAppender', function() {
|
||||
});
|
||||
|
||||
describe('with default fileAppender settings', function() {
|
||||
var fileContents;
|
||||
var fileContents
|
||||
, testFile = path.join(__dirname, '/fa-default-test.log');
|
||||
|
||||
before(function(done) {
|
||||
var that = this
|
||||
, testFile = path.join(__dirname, '/fa-default-test.log')
|
||||
, logger = log4js.getLogger('default-settings');
|
||||
var logger = log4js.getLogger('default-settings');
|
||||
|
||||
remove(testFile);
|
||||
remove(testFile, function() {
|
||||
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
"file": { type: "file", filename: testFile }
|
||||
},
|
||||
categories: {
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
"file": { type: "file", filename: testFile }
|
||||
},
|
||||
categories: {
|
||||
default: { level: "debug", appenders: [ "file" ] }
|
||||
}
|
||||
});
|
||||
|
||||
logger.info("This should be in the file.");
|
||||
|
||||
setTimeout(function() {
|
||||
fs.readFile(testFile, "utf8", function(err, contents) {
|
||||
if (!err) {
|
||||
fileContents = contents;
|
||||
}
|
||||
done(err);
|
||||
});
|
||||
}, 100);
|
||||
|
||||
logger.info("This should be in the file.");
|
||||
|
||||
setTimeout(function() {
|
||||
fs.readFile(testFile, "utf8", function(err, contents) {
|
||||
if (!err) {
|
||||
fileContents = contents;
|
||||
}
|
||||
done(err);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
remove(testFile, done);
|
||||
});
|
||||
|
||||
it('should write log messages to the file', function() {
|
||||
@ -127,24 +133,32 @@ describe('log4js fileAppender', function() {
|
||||
describe('with a max file size and no backups', function() {
|
||||
var testFile = path.join(__dirname, '/fa-maxFileSize-test.log');
|
||||
|
||||
before(function() {
|
||||
before(function(done) {
|
||||
var logger = log4js.getLogger('max-file-size');
|
||||
|
||||
remove(testFile);
|
||||
remove(testFile + '.1');
|
||||
async.forEach([
|
||||
testFile,
|
||||
testFile + '.1'
|
||||
], remove, function() {
|
||||
|
||||
//log file of 100 bytes maximum, no backups
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
"file": { type: "file", filename: testFile, maxLogSize: 100, backups: 0 }
|
||||
},
|
||||
categories: {
|
||||
//log file of 100 bytes maximum, no backups
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
"file": { type: "file", filename: testFile, maxLogSize: 100, backups: 0 }
|
||||
},
|
||||
categories: {
|
||||
default: { level: "debug", appenders: [ "file" ] }
|
||||
}
|
||||
}
|
||||
});
|
||||
logger.info("This is the first log message.");
|
||||
logger.info("This is an intermediate log message.");
|
||||
logger.info("This is the second log message.");
|
||||
done();
|
||||
});
|
||||
logger.info("This is the first log message.");
|
||||
logger.info("This is an intermediate log message.");
|
||||
logger.info("This is the second log message.");
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
async.forEach([ testFile, testFile + '.1' ], remove, done);
|
||||
});
|
||||
|
||||
describe('log file', function() {
|
||||
@ -177,26 +191,32 @@ describe('log4js fileAppender', function() {
|
||||
describe('with a max file size and 2 backups', function() {
|
||||
var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-test.log');
|
||||
|
||||
before(function() {
|
||||
before(function(done) {
|
||||
var logger = log4js.getLogger('max-file-size-backups');
|
||||
remove(testFile);
|
||||
remove(testFile+'.1');
|
||||
remove(testFile+'.2');
|
||||
|
||||
//log file of 50 bytes maximum, 2 backups
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
"file": { type: "file", filename: testFile, maxLogSize: 50, backups: 2 }
|
||||
},
|
||||
categories: {
|
||||
default: { level: "debug", appenders: [ "file" ] }
|
||||
}
|
||||
});
|
||||
|
||||
logger.info("This is the first log message.");
|
||||
logger.info("This is the second log message.");
|
||||
logger.info("This is the third log message.");
|
||||
logger.info("This is the fourth log message.");
|
||||
async.forEach([
|
||||
testFile,
|
||||
testFile+'.1',
|
||||
testFile+'.2'
|
||||
], remove, function() {
|
||||
|
||||
//log file of 50 bytes maximum, 2 backups
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
"file": { type: "file", filename: testFile, maxLogSize: 50, backups: 2 }
|
||||
},
|
||||
categories: {
|
||||
default: { level: "debug", appenders: [ "file" ] }
|
||||
}
|
||||
});
|
||||
|
||||
logger.info("This is the first log message.");
|
||||
logger.info("This is the second log message.");
|
||||
logger.info("This is the third log message.");
|
||||
logger.info("This is the fourth log message.");
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('the log files', function() {
|
||||
@ -219,6 +239,10 @@ describe('log4js fileAppender', function() {
|
||||
}, 200);
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
async.forEach(logFiles, remove, done);
|
||||
});
|
||||
|
||||
it('should be 3', function () {
|
||||
logFiles.should.have.length(3);
|
||||
});
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
{
|
||||
"appenders": [
|
||||
{
|
||||
"category": "tests",
|
||||
"appenders": {
|
||||
"dateFile": {
|
||||
"type": "dateFile",
|
||||
"filename": "test/date-file-test.log",
|
||||
"pattern": "-from-MM-dd",
|
||||
@ -9,9 +8,9 @@
|
||||
"type": "messagePassThrough"
|
||||
}
|
||||
}
|
||||
],
|
||||
},
|
||||
|
||||
"levels": {
|
||||
"tests": "WARN"
|
||||
"categories": {
|
||||
"default": { "level": "WARN", "appenders": [ "dateFile" ] }
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user