Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added exceptionToMeta method for filtering returned meta object #173

Merged
merged 3 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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