Skip to content

Commit

Permalink
Merge pull request #173 from cubbuk/feature/exception-to-meta
Browse files Browse the repository at this point in the history
added exceptionToMeta method for filtering returned meta object
  • Loading branch information
bithavoc authored Jun 13, 2018
2 parents 750e469 + caa0fec commit 20fe0d3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ The logger needs to be added AFTER the express router(`app.router)`) and BEFORE
requestWhitelist: [String] // Array of request properties to log. Overrides global requestWhitelist for this instance
level: String or function(req, res, err) { return String; }// custom log level for errors (default is 'error'). Assign a function to dynamically set the log level based on request, response, and the exact error.
dynamicMeta: function(req, res, err) { return [Object]; } // Extract additional meta data from request or response (typically req.user data if using passport). meta must be true for this function to be activated
exceptionToMeta: function(error){return Object; } // Function to format the returned meta information on error log. If not given `winston.exception.getAllInfo` will be used by default
blacklistedMetaFields: [String] // fields to blacklist from meta data
```

To use winston's existing transports, set `transports` to the values (as in key-value) of the `winston.default.transports` object. This may be done, for example, by using underscorejs: `transports: _.values(winston.default.transports)`.
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ exports.errorLogger = function errorLogger(options) {
options.metaField = options.metaField || null;
options.level = options.level || 'error';
options.dynamicMeta = options.dynamicMeta || function(req, res, err) { return null; };
options.exceptionToMeta = options.exceptionToMeta || winston.exception.getAllInfo;
options.blacklistedMetaFields = options.blacklistedMetaFields || [];

// Using mustache style templating
var getTemplate = function(msg, data) {
Expand All @@ -131,8 +133,8 @@ exports.errorLogger = function errorLogger(options) {

return function (err, req, res, next) {

// Let winston gather all the error data.
var exceptionMeta = winston.exception.getAllInfo(err);
// Let winston gather all the error data
var exceptionMeta = _.omit(options.exceptionToMeta(err), options.blacklistedMetaFields);
exceptionMeta.req = filterObject(req, options.requestWhitelist, options.requestFilter);

if(options.dynamicMeta) {
Expand Down
41 changes: 41 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,47 @@ describe('express-winston', function () {
});
});

describe('exceptionToMeta option', function () {
it('should, use exceptionToMeta function when given', function () {
function exceptionToMeta(error) {
return {
stack: error.stack && error.stack.split('\n')
};
}

var testHelperOptions = { loggerOptions: { exceptionToMeta: exceptionToMeta } };
return errorLoggerTestHelper(testHelperOptions).then(function (result) {
result.log.meta.stack.should.be.ok();
result.log.meta.should.not.have.property('trace');
});
});

it('should, use getAllInfo function when not given', function () {
var testHelperOptions = { loggerOptions: { } };
return errorLoggerTestHelper(testHelperOptions).then(function (result) {
result.log.meta.should.have.property('date');
result.log.meta.should.have.property('process');
result.log.meta.should.have.property('os');
result.log.meta.should.have.property('trace');
result.log.meta.should.have.property('stack');
});
});
});

describe('blacklistedMetaFields option', function () {
it('should, remove given fields from the meta result', function () {
var testHelperOptionsWithBlacklist = { loggerOptions: { blacklistedMetaFields: ['trace'] } };
return errorLoggerTestHelper(testHelperOptionsWithBlacklist).then(function (result) {
result.log.meta.should.not.have.property('trace');
});

var testHelperOptionsWithoutBlacklist = { loggerOptions: {} };
return errorLoggerTestHelper(testHelperOptionsWithoutBlacklist).then(function (result) {
result.log.meta.should.have.property('trace');
});
});
});

describe('metaField option', function () {
it('should, when using a custom metaField, log the custom metaField', function () {
var testHelperOptions = {loggerOptions: {metaField: 'metaField'}};
Expand Down

0 comments on commit 20fe0d3

Please sign in to comment.