Skip to content

Commit

Permalink
Allow to pass an Error object to the message field, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kkamkou committed Feb 22, 2016
1 parent 7579b70 commit c000193
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The Graylog Extended Log Format. Pro - because of code-quality.
## Installation
```
"dependencies": {
"gelf-pro": "~0.2"
"gelf-pro": "~0.3"
}
```
```npm install gelf-pro```
Expand Down Expand Up @@ -39,6 +39,9 @@ log.info("Hello world", extra, function (err, bytesSent) {});
log.info("Hello world", function (err, bytesSent) {});
log.info("Hello world", extra);
log.info("Hello world");

log.info('Oooops.', new Error('An error message'));
log.info(new Error('An error message'));
```

### Levels
Expand Down
10 changes: 9 additions & 1 deletion lib/gelf-pro.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ gelf.setConfig = function (conf) {
};

/**
* Creates and return adapter
* Creates and return adapter (singleton)
* @returns {Object}
*/
gelf.getAdapter = function () {
Expand Down Expand Up @@ -119,6 +119,14 @@ gelf.send = function (message, cb) {
extra = {};
}

// trying to convert an error to readable message
if (_.isError(message)) {
message = message.message.toString() || 'Error';
}
if (_.isError(extra)) {
extra = {error: {message: extra.message, stack: extra.stack}};
}

// creating string from object
extra = this.getStringFromObject(
_.merge(
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gelf-pro",
"version": "0.2.0",
"version": "0.3.0",
"main": "./lib/gelf-pro.js",
"author": "Kanstantsin Kamkou <[email protected]>",
"description": "The Graylog Extended Log Format for the Node.js",
Expand All @@ -22,12 +22,12 @@
"test": "mocha"
},
"dependencies": {
"lodash": "~3.10",
"async": "~1.4"
"lodash": "~4.5",
"async": "~1.5"
},
"devDependencies": {
"should": "~7.1",
"mocha": "~2.3",
"sinon": "~1.16"
"should": "~8.2",
"mocha": "~2.4",
"sinon": "~1.17"
}
}
17 changes: 16 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = {
result.should.not.have.property('id');
},

'Extra fileds normalization': function () {
'Normalize extra fields': function () {
var mock = sinon.mock(console);
mock.expects('warn').once().withExactArgs('the first value: the key format is not valid');

Expand All @@ -91,6 +91,21 @@ module.exports = {
result.should.have.property('_level1_value1').equal('value1');
result.should.have.property('_level1_level2_value2').equal('value2');
result.should.have.property('_level1_level2_level3_value3').equal('value3');
},

'Transform an Error object': function () {
var gelf = _.cloneDeep(gelfOriginal),
err = new Error('Some error message');

sinon.spy(gelf, 'getStringFromObject');

gelf.info(err);
gelf.info('Example', err);

JSON.parse(gelf.getStringFromObject.firstCall.returnValue)
.should.containEql({short_message: err.message.toString()});
JSON.parse(gelf.getStringFromObject.lastCall.returnValue)
.should.have.properties(['_error_message', '_error_stack']);
}
},

Expand Down

0 comments on commit c000193

Please sign in to comment.