Skip to content

Commit

Permalink
[[FEAT]] Make node 6 compatible (#39)
Browse files Browse the repository at this point in the history
* [[CHORE]] Prepare node6 env

* [[FIX]] DEV formatter for node6

* [[FIX]] JSON format for node > 6

* [[FIX]] pipe format for node > 6

* [[CHORE]] lintint
  • Loading branch information
jmendiara authored May 3, 2017
1 parent 25f91ec commit 972fbe5
Show file tree
Hide file tree
Showing 7 changed files with 2,543 additions and 18 deletions.
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: node_js
sudo: false
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
script:
- "6"
- "7"
script:
- "npm run travis"
- "npm run lint"
cache:
Expand Down
7 changes: 6 additions & 1 deletion lib/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ function formatTrace(level, context, message, args, err) {
recontext[key] = context[key] || notAvailable;
});

recontext.msg = message;
if (message instanceof Date || message instanceof Error) {
// Node6 related hack. See https://github.com/telefonicaid/logops/issues/36
recontext.msg = util.format(message);
} else {
recontext.msg = message;
}

var str = Object.keys(recontext)
.map(function(key) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"main": "lib/logops",
"typings": "./lib/logops.d.ts",
"engines": {
"node": ">=0.10.8 <6"
"node": ">=6"
},
"scripts": {
"travis": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec test/environment.js test/*.spec.js && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage && tsc --noEmit",
Expand Down
7 changes: 4 additions & 3 deletions test/format.dev.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Development format', function() {
it('should log dates', function() {
var now = new Date();
logger.info({}, now);
expect(logger._lastTrace).to.be.eql('INFO ' + now);
expect(logger._lastTrace).to.be.eql('INFO 1970-01-01T00:00:00.000Z');
});

it('should nothing but context with a Data as context', function() {
Expand Down Expand Up @@ -191,8 +191,9 @@ describe('Development format', function() {
});

it('should log extra errors', function() {
logger.info('Format', new Error('foo'));
expect(logger._lastTrace).to.be.eql(pad('INFO Format [Error: foo]'));
var error = new Error('foo');
logger.info('Format', error);
expect(logger._lastTrace).to.be.eql(pad('INFO Format ' + error.stack));
});

it('should log errors with extra information without stacktrace', function() {
Expand Down
7 changes: 4 additions & 3 deletions test/format.json.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('JSON format', function() {
var now = new Date();
logger.info({}, now);
expect(logger._lastTrace).to.be.eql(JSON.stringify({time: '1970-01-01T00:00:00.000Z', lvl: 'INFO',
msg: '' + now
msg: '1970-01-01T00:00:00.000Z'
}));
});

Expand Down Expand Up @@ -244,9 +244,10 @@ describe('JSON format', function() {
});

it('should log extra errors', function() {
logger.info('Format', new Error('foo'));
var error = new Error('foo');
logger.info('Format', error);
expect(logger._lastTrace).to.be.eql(JSON.stringify({time: '1970-01-01T00:00:00.000Z', lvl: 'INFO',
msg: 'Format [Error: foo]'
msg: 'Format ' + error.stack
}));
});

Expand Down
12 changes: 7 additions & 5 deletions test/format.pipe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('Pipe format', function() {
var now = new Date();
logger.info({}, now);
expect(logger._lastTrace).to.be.eql(
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=n/a | trans=n/a | op=n/a | msg=' + now
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=n/a | trans=n/a | op=n/a | msg=1970-01-01T00:00:00.000Z'
);
});

Expand Down Expand Up @@ -141,16 +141,18 @@ describe('Pipe format', function() {
});

it('should log extra errors', function() {
logger.info('Format', new Error('foo'));
var error = new Error('foo');
logger.info('Format', error);
expect(logger._lastTrace).to.be.eql(
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=n/a | trans=n/a | op=n/a | msg=Format [Error: foo]'
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=n/a | trans=n/a | op=n/a | msg=Format ' + error.stack
);
});

it('should log errors with extra information', function() {
logger.info(new Error('foo'), 'Format %s', 'works');
var error = new Error('foo');
logger.info(error, 'Format %s', 'works');
expect(logger._lastTrace).to.be.eql(
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=n/a | trans=n/a | op=n/a | msg=Format works [Error: foo]'
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=n/a | trans=n/a | op=n/a | msg=Format works ' + error.stack
);
});

Expand Down
Loading

0 comments on commit 972fbe5

Please sign in to comment.