Skip to content

Commit

Permalink
implemented filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris927 committed Jan 18, 2015
1 parent 861dbfb commit 2b96eee
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -37,6 +37,7 @@ There are two different ways to use winston: directly via the default logger, or
* [Working with multiple Loggers in winston](#working-with-multiple-loggers-in-winston)
* [Using winston in a CLI tool](#using-winston-in-a-cli-tool)
* [Extending another object with Logging](#extending-another-object-with-logging)
* [Filters](#filters)
* [Working with transports](#working-with-transports)
* [Adding Custom Transports](#adding-custom-transports)
* [Installation](#installation)
Expand Down Expand Up @@ -627,6 +628,26 @@ Often in a given code base with lots of Loggers it is useful to add logging meth
myObject.info("127.0.0.1 - there's no place like home");
```

### Filters
Filters allow modifying the contents of log messages, e.g. to mask data that
should not appear in logs.

``` js
logger.addFilter(function(msg) {
return maskCardNumbers(msg);
});
logger.info('transaction with card number 123456789012345 successful.');
```

This may result in this output:

```
info: transaction with card number 123456****2345 successful.
```

See [log-filter-test.js](./test/log-filter-test.js), where card number masking
is implemented as an example.

## Working with Transports
There are many transports supported by winston core. If you have a transport you would like to add either open an issue or fork and submit a pull request. Commits are welcome, but I'll give you extra street cred if you __add tests too :D__

Expand Down
3 changes: 2 additions & 1 deletion lib/winston.js
Expand Up @@ -78,7 +78,8 @@ var methods = [
'cli',
'handleExceptions',
'unhandleExceptions',
'addRewriter'
'addRewriter',
'addFilter'
];
common.setLevels(winston, null, defaultLogger.levels);
methods.forEach(function (method) {
Expand Down
14 changes: 14 additions & 0 deletions lib/winston/logger.js
Expand Up @@ -51,6 +51,7 @@ var Logger = exports.Logger = function (options) {
//
this.transports = {};
this.rewriters = [];
this.filters = [];
this.exceptionHandlers = {};
this.profilers = {};
this._names = [];
Expand Down Expand Up @@ -153,6 +154,10 @@ Logger.prototype.log = function (level) {
meta = rewriter(level, msg, meta);
});

this.filters.forEach(function(filter) {
msg = filter(msg);
});

//
// For consideration of terminal 'color" programs like colors.js,
// which can add ANSI escape color codes to strings, we destyle the
Expand Down Expand Up @@ -472,6 +477,15 @@ Logger.prototype.addRewriter = function (rewriter) {
this.rewriters.push(rewriter);
}

//
// ### function addFilter (filter)
// #### @filter {function} Filter function, called with the message as single
// argument, expected to return the filtered message.
//
Logger.prototype.addFilter = function (filter) {
this.filters.push(filter);
}

//
// ### function clear ()
// Remove all transports from this instance
Expand Down
51 changes: 51 additions & 0 deletions test/log-filter-test.js
@@ -0,0 +1,51 @@
/* log-filter-test.js: Test filtering of message content in winston.
* (c) 2015 Chris Oloff
* MIT LICENSE
*/

var assert = require('assert'),
vows = require('vows'),
winston = require('../lib/winston'),
helpers = require('./helpers');

/* To demo a filter, we filter out credit card numbers, assuming that a credit
* card number consists of 13 or more digits (without spaces). This function
* does the actual masking.
*/
function maskCardNumbers(s) {
var match;
while (match = s.match(/(\d{13}\d*)/)) {
var toBeMasked = match[1];
s = s.replace(toBeMasked, toBeMasked.substring(0,6) + '****' + toBeMasked.substring(toBeMasked.length - 4));
}
return s;
}

vows.describe('winston/logger/filter').addBatch({
"An instance of winston.Logger": {
topic: new (winston.Logger)({transports: [
new (winston.transports.Console)({ level: 'info' })
]}),
"the addFilter() method": {
topic: function (logger) {
logger.addFilter(function (msg) {
return maskCardNumbers(msg);
});
return logger;
},
"should add the filter": function(logger) {
assert.equal(helpers.size(logger.filters), 1);
},
"the log() method": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info', 'card number 123456789012345 for testing');
},
"should filter the card number": function (transport, level, msg, meta) {
assert.equal(msg, 'card number 123456****2345 for testing');
}
}
}
}
}).export(module);

0 comments on commit 2b96eee

Please sign in to comment.