Compare commits

...

9 Commits

Author SHA1 Message Date
Gareth Jones
012b0d5ed7 version 0.5.2 2012-08-14 10:47:25 +10:00
Gareth Jones
de72005e7e Fixed layout stack trace test 2012-08-14 09:44:43 +10:00
Gareth Jones
c6a0e58409 Merge pull request #89 from ixti/master
Fix possible memleak with `exit` event handlers
2012-08-13 16:32:08 -07:00
Aleksey V Zapparov
f832a2ba79 Do not assign multiple exit handlers for FA 2012-08-09 15:21:30 +02:00
Aleksey V Zapparov
3f10b68c30 Add test for amount of exit listeners in FA 2012-08-09 15:15:28 +02:00
Gareth Jones
54c311842c Merge pull request #86 from osher/patch-3
Update lib/layouts.js
2012-08-01 16:21:01 -07:00
osher
f948b5f5cd Add unit tests - layouts-test.js 2012-08-01 10:11:37 +03:00
osher
54e420eb58 Update lib/layouts.js
Errors sometimes carry additional attributes on them as part of the passed error data.
A utility that utilizes it, for example - is called 'errs', which is in use for instance 'nano' - the couch-db driver.

when only the stack is printed - all the additional information that is augmented on the error object does not sink to the log and is lost.

consider the following code:

```
//the oups throwing utility
function oups(){
  e = new Error();
  extend(
    { message    : "Oups error"
    , description: "huston, we got a problem"
    , status     : "MESS"
    , errorCode  : 991
    , arr :[1,2,3,4,{}]
    , data: 
      { c:{}
      , d:{e:{}}
      }
    }
  throw e;
}

var log = require('log4js')

try{
  oups()
} catch( e ) {
   log.error("error on oups", e );
}

```


output before the fix

```
error on oups Error: Oups error
    at repl:1:11
    at REPLServer.eval (repl.js:80:21)
    at Interface.<anonymous> (repl.js:182:12)
    at Interface.emit (events.js:67:17)
    at Interface._onLine (readline.js:162:10)
    at Interface._line (readline.js:426:8)
    at Interface._ttyWrite (readline.js:603:14)
    at ReadStream.<anonymous> (readline.js:82:12)
    at ReadStream.emit (events.js:88:20)
```


output after the fix would be

```
error on oups { [Error: My error message]
  name: 'Error',
  description: 'huston, we got a problem',
  status: 'MESS',
  errorCode: 991,
  arr: [ 1, 2, 3, 4, {} ],
  data: { c: {}, d: { e: {} } } }
Error: Oups error
    at repl:1:11
    at REPLServer.eval (repl.js:80:21)
    at Interface.<anonymous> (repl.js:182:12)
    at Interface.emit (events.js:67:17)
    at Interface._onLine (readline.js:162:10)
    at Interface._line (readline.js:426:8)
    at Interface._ttyWrite (readline.js:603:14)
    at ReadStream.<anonymous> (readline.js:82:12)
    at ReadStream.emit (events.js:88:20)
```
2012-07-31 14:32:03 +03:00
Gareth Jones
40ba24a55d Renamed tests so that vows will pick them up automatically 2012-07-31 14:52:36 +10:00
22 changed files with 82 additions and 11 deletions

View File

@@ -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");

View File

@@ -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;
}
});

View File

@@ -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",

View File

@@ -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() {

View File

@@ -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) {