Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
012b0d5ed7 | ||
|
|
de72005e7e | ||
|
|
c6a0e58409 | ||
|
|
f832a2ba79 | ||
|
|
3f10b68c30 | ||
|
|
54c311842c | ||
|
|
f948b5f5cd | ||
|
|
54e420eb58 | ||
|
|
40ba24a55d |
@@ -5,6 +5,9 @@ var layouts = require('../layouts')
|
||||
, os = require('os')
|
||||
, eol = os.EOL || '\n';
|
||||
|
||||
var openFiles = [];
|
||||
var listenerAtttached = false;
|
||||
|
||||
/**
|
||||
* File Appender writing the logs to a text file. Supports rolling of logs by size.
|
||||
*
|
||||
@@ -42,10 +45,18 @@ function fileAppender (file, layout, logSize, numBackups) {
|
||||
|
||||
var logFile = openTheStream(file, logSize, numBackups);
|
||||
|
||||
// push file to the stack of open handlers
|
||||
openFiles.push(logFile);
|
||||
|
||||
//close the file on process exit.
|
||||
process.on('exit', function() {
|
||||
logFile.end();
|
||||
});
|
||||
if (!listenerAtttached) {
|
||||
listenerAtttached = true;
|
||||
process.on('exit', function() {
|
||||
openFiles.forEach(function (file) {
|
||||
file.end();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return function(loggingEvent) {
|
||||
logFile.write(layout(loggingEvent) + eol, "utf8");
|
||||
|
||||
@@ -49,10 +49,9 @@ function formatLogData(logData) {
|
||||
if (output) {
|
||||
output += ' ';
|
||||
}
|
||||
output += util.inspect(item);
|
||||
if (item && item.stack) {
|
||||
output += item.stack;
|
||||
} else {
|
||||
output += util.inspect(item);
|
||||
output += "\n" + item.stack;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "log4js",
|
||||
"version": "0.5.1",
|
||||
"version": "0.5.2",
|
||||
"description": "Port of Log4js to work with node.",
|
||||
"keywords": [
|
||||
"logging",
|
||||
@@ -19,7 +19,7 @@
|
||||
},
|
||||
"engines": [ "node >=0.6" ],
|
||||
"scripts": {
|
||||
"test": "vows test/*.js"
|
||||
"test": "vows"
|
||||
},
|
||||
"directories": {
|
||||
"test": "test",
|
||||
|
||||
@@ -15,6 +15,24 @@ function remove(filename) {
|
||||
}
|
||||
|
||||
vows.describe('log4js fileAppender').addBatch({
|
||||
'adding multiple fileAppenders': {
|
||||
topic: function () {
|
||||
var listenersCount = process.listeners('exit').length
|
||||
, logger = log4js.getLogger('default-settings')
|
||||
, count = 5, logfile;
|
||||
|
||||
while (count--) {
|
||||
logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
|
||||
log4js.addAppender(require('../lib/appenders/file').appender(logfile), 'default-settings');
|
||||
}
|
||||
|
||||
return listenersCount;
|
||||
},
|
||||
|
||||
'does not adds more than one `exit` listeners': function (initialCount) {
|
||||
assert.ok(process.listeners('exit').length <= initialCount + 1);
|
||||
}
|
||||
},
|
||||
|
||||
'with default fileAppender settings': {
|
||||
topic: function() {
|
||||
@@ -76,8 +76,50 @@ vows.describe('log4js layouts').addBatch({
|
||||
, toString: function() { return "ERROR"; }
|
||||
}
|
||||
}), "{ thing: 1 }");
|
||||
},
|
||||
'should print the stacks of a passed error objects': function(layout) {
|
||||
assert.isArray(layout({
|
||||
data: [ new Error() ]
|
||||
, startTime: new Date(2010, 11, 5, 14, 18, 30, 45)
|
||||
, categoryName: "cheese"
|
||||
, level: {
|
||||
colour: "green"
|
||||
, toString: function() { return "ERROR"; }
|
||||
}
|
||||
}).match(/Error\s+at Object\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s+at runTest/)
|
||||
, 'regexp did not return a match');
|
||||
},
|
||||
'with passed augmented errors':
|
||||
{ topic:
|
||||
function(layout){
|
||||
var e = new Error("My Unique Error Message");
|
||||
e.augmented = "My Unique attribute value"
|
||||
e.augObj = { at1: "at2" }
|
||||
return layout({
|
||||
data: [ e ]
|
||||
, startTime: new Date(2010, 11, 5, 14, 18, 30, 45)
|
||||
, categoryName: "cheese"
|
||||
, level: {
|
||||
colour: "green"
|
||||
, toString: function() { return "ERROR"; }
|
||||
}
|
||||
});
|
||||
},
|
||||
'should print error the contained error message': function(layoutOutput) {
|
||||
var m = layoutOutput.match(/\{ \[Error: My Unique Error Message\]/);
|
||||
assert.isArray(m);
|
||||
},
|
||||
'should print error augmented string attributes': function(layoutOutput) {
|
||||
var m = layoutOutput.match(/augmented:\s'My Unique attribute value'/);
|
||||
assert.isArray(m);
|
||||
},
|
||||
'should print error augmented object attributes': function(layoutOutput) {
|
||||
var m = layoutOutput.match(/augObj:\s\{ at1: 'at2' \}/);
|
||||
assert.isArray(m);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
},
|
||||
|
||||
'basicLayout': {
|
||||
@@ -106,10 +148,11 @@ vows.describe('log4js layouts').addBatch({
|
||||
output = layout(event);
|
||||
lines = output.split(/\n/);
|
||||
|
||||
assert.equal(lines.length, stack.length);
|
||||
assert.equal(lines[0], "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test Error: Some made-up error");
|
||||
assert.equal(lines.length - 1, stack.length);
|
||||
assert.equal(lines[0], "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]");
|
||||
|
||||
for (var i = 1; i < stack.length; i++) {
|
||||
assert.equal(lines[i+1], stack[i+1]);
|
||||
assert.equal(lines[i+2], stack[i+1]);
|
||||
}
|
||||
},
|
||||
'should output any extra data in the log event as util.inspect strings': function(args) {
|
||||
Reference in New Issue
Block a user