Skip to content

Commit

Permalink
[breaking refactor doc] Remove useless methods for .addRewriter and…
Browse files Browse the repository at this point in the history
… `.addFilter` since they are just simple Arrays of functions.
  • Loading branch information
indexzero committed Oct 29, 2015
1 parent 0f82204 commit f71e638
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ Often in a given code base with lots of Loggers it is useful to add logging meth
Filters allow modifying the contents of **log messages**, and Rewriters allow modifying the contents of **log meta** e.g. to mask data that should not appear in logs.

``` js
logger.addFilter(function(msg, meta, level) {
logger.filters.push(function(msg, meta, level) {
return meta.production
? maskCardNumbers(msg)
: msg;
Expand All @@ -675,7 +675,7 @@ info: transaction with card number 123456****2345 successful.
Where as for rewriters, if you wanted to sanitize the `creditCard` field of your `meta` you could:

``` js
logger.addRewriter(function(level, msg, meta) {
logger.rewriters.push(function(level, msg, meta) {
if (meta.creditCard) {
meta.creditCard = maskCardNumbers(meta.creditCard)
}
Expand Down
29 changes: 2 additions & 27 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ var Logger = exports.Logger = function (options) {
});
}

if (options.rewriters) {
options.rewriters.forEach(function (rewriter) {
self.addRewriter(rewriter);
});
if (Array.isArray(options.rewriters)) {
this.rewriters = options.rewriters;
}

if (options.exceptionHandlers) {
Expand Down Expand Up @@ -476,29 +474,6 @@ Logger.prototype.add = function (transport, options, created) {
return this;
};

//
// ### function addRewriter (transport, [options])
// #### @transport {Transport} Prototype of the Transport object to add.
// #### @options {Object} **Optional** Options for the Transport to add.
// #### @instance {Boolean} **Optional** Value indicating if `transport` is already instantiated.
// Adds a transport of the specified type to this instance.
//
Logger.prototype.addRewriter = function (rewriter) {
this.rewriters.push(rewriter);
}

//
// ### function addFilter (filter)
// #### @filter {function} Filter function, called with the message and
// optional metadata as the two arguments.
// Expected to return either the filtered message or an object with properties:
// - msg = the filtered message string
// - meta = the filtered metadata object
//
Logger.prototype.addFilter = function (filter) {
this.filters.push(filter);
}

//
// ### function clear ()
// Remove all transports from this instance
Expand Down
30 changes: 16 additions & 14 deletions test/log-filter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ vows.describe('winston/logger/filter').addBatch({
topic: new (winston.Logger)({transports: [
new (winston.transports.Console)({ level: 'info' })
]}),
"the addFilter() method, adding a filter only for the message": {
"the filters.push() method, adding a filter only for the message": {
topic: function (logger) {
logger.addFilter(function (msg) {
logger.filters.push(function (msg) {
return maskCardNumbers(msg);
});

return logger;
},
"should add the filter": function (logger) {
Expand All @@ -70,26 +71,27 @@ vows.describe('winston/logger/filter').addBatch({
}
}).addBatch({
"A fresh instance of winston.Logger": {
topic: new (winston.Logger)({transports: [
new (winston.transports.Console)({ level: 'info' })
]}),
"the addFilter() method adding a filter for the message and metadata": {
topic: new (winston.Logger)({
transports: [new (winston.transports.Console)({ level: 'info' })]
}),
"the filters.push() method adding a filter for the message and metadata": {
topic: function (logger) {
logger.addFilter(function (msg, meta) {
logger.filters.push(function (msg, meta) {
return maskSecrets(msg, meta);
});

return logger;
},
"the log() method with a filtered message and filtered metadata": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info',
'We should make sure the secret stays SECRET.',
{ 'SECRET': "You shouldn't see this.",
'public': 'But you can look at this.',
'secret': "We'll have to take you to Area-51 now.",
'not-secret': 'No worries about this one.',
'Secret': "It's confidential!" });
logger.log('info', 'We should make sure the secret stays SECRET.', {
'SECRET': "You shouldn't see this.",
'public': 'But you can look at this.',
'secret': "We'll have to take you to Area-51 now.",
'not-secret': 'No worries about this one.',
'Secret': "It's confidential!"
});
},
"should filter out secrets": function (transport, level, msg, meta) {
assert.equal(msg, 'We should make sure the ****** stays ******.');
Expand Down
3 changes: 2 additions & 1 deletion test/log-rewriter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ vows.describe('winston/logger/rewriter').addBatch({
]}),
"the addRewriter() method": {
topic: function (logger) {
logger.addRewriter(function (level, msg, meta) {
logger.rewriters.push(function (level, msg, meta) {
meta.level = level;
meta.msg = msg;
meta.foo = 'bar';
return meta;
});

return logger;
},
"should add the rewriter": function (logger) {
Expand Down

0 comments on commit f71e638

Please sign in to comment.