Skip to content

Commit

Permalink
It is not possible to send a date object as extra, closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
kkamkou committed Mar 7, 2016
1 parent 6572efd commit 2738d6a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Graylog Extended Log Format. Pro - because of code-quality.
## Installation
```
"dependencies": {
"gelf-pro": "~0.5"
"gelf-pro": "~0.6"
}
```
```npm install gelf-pro```
Expand Down Expand Up @@ -47,6 +47,18 @@ log.info('Oooops.', new Error('An error message'));
log.info(new Error('An error message'));
```

##### Extra
In case `extra` [is a plain object](https://lodash.com/docs#isPlainObject),
the library converts it to a readable format. Other values [are converted to string](https://lodash.com/docs#toString).
```javascript
log.message(
'a new msg goes here',
{me: {fname: 'k', lname: 'k', bdate: new Date(2000, 01, 01)}}
);
// the extra becomes:
// {_me_fname: 'k', _me_lname: 'k', _me_bdate: 'Tue Feb 01 2000 00:00:00 GMT+0100 (CET)'}
```

##### Filtering
Sometimes we have to discard a message which is not suitable for the current environment.
It is not possible to modify the data.
Expand Down
4 changes: 2 additions & 2 deletions lib/gelf-pro.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ gelf.getStringFromObject = function (obj) {
if ((/[^\w]/).test(key)) {
console.warn(key + ': the key format is not valid');
}
if (_.isObject(value)) {
if (_.isPlainObject(value)) {
return recursion(value, prefix ? [prefix, key].join('_') : key);
}
result[(prefix ? [null, prefix, key] : [null, key]).join('_')] = value;
result[(prefix ? [null, prefix, key] : [null, key]).join('_')] = _.toString(value);
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gelf-pro",
"version": "0.5.1",
"version": "0.6.0",
"main": "./lib/gelf-pro.js",
"author": "Kanstantsin Kamkou <[email protected]>",
"description": "The Graylog Extended Log Format for the Node.js",
Expand Down
8 changes: 7 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ module.exports = {
result.should.have.property('_level1_level2_level3_value3').equal('value3');
},

'Work with dates': function () {
var gelf = _.cloneDeep(gelfOriginal),
date = new Date();
JSON.parse(gelf.getStringFromObject({test: date}))._test.should.equal(date.toString());
},

'Transform an Error object': function () {
var gelf = _.cloneDeep(gelfOriginal),
err = new Error('Some error message');
Expand Down Expand Up @@ -147,7 +153,7 @@ module.exports = {
bytes.should.be.equal(0);
stub.lastCall.args.should.eql([{short_message: 'test', level: 4}]);
spyFn.lastCall.returned(false).should.be.true();
spySend.neverCalledWith().should.be.true();;
spySend.neverCalledWith().should.be.true();
done();
});
}
Expand Down

0 comments on commit 2738d6a

Please sign in to comment.