Skip to content

Commit 2b96eee

Browse files
committed
implemented filters
1 parent 861dbfb commit 2b96eee

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ There are two different ways to use winston: directly via the default logger, or
3737
* [Working with multiple Loggers in winston](#working-with-multiple-loggers-in-winston)
3838
* [Using winston in a CLI tool](#using-winston-in-a-cli-tool)
3939
* [Extending another object with Logging](#extending-another-object-with-logging)
40+
* [Filters](#filters)
4041
* [Working with transports](#working-with-transports)
4142
* [Adding Custom Transports](#adding-custom-transports)
4243
* [Installation](#installation)
@@ -627,6 +628,26 @@ Often in a given code base with lots of Loggers it is useful to add logging meth
627628
myObject.info("127.0.0.1 - there's no place like home");
628629
```
629630

631+
### Filters
632+
Filters allow modifying the contents of log messages, e.g. to mask data that
633+
should not appear in logs.
634+
635+
``` js
636+
logger.addFilter(function(msg) {
637+
return maskCardNumbers(msg);
638+
});
639+
logger.info('transaction with card number 123456789012345 successful.');
640+
```
641+
642+
This may result in this output:
643+
644+
```
645+
info: transaction with card number 123456****2345 successful.
646+
```
647+
648+
See [log-filter-test.js](./test/log-filter-test.js), where card number masking
649+
is implemented as an example.
650+
630651
## Working with Transports
631652
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__
632653

lib/winston.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ var methods = [
7878
'cli',
7979
'handleExceptions',
8080
'unhandleExceptions',
81-
'addRewriter'
81+
'addRewriter',
82+
'addFilter'
8283
];
8384
common.setLevels(winston, null, defaultLogger.levels);
8485
methods.forEach(function (method) {

lib/winston/logger.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var Logger = exports.Logger = function (options) {
5151
//
5252
this.transports = {};
5353
this.rewriters = [];
54+
this.filters = [];
5455
this.exceptionHandlers = {};
5556
this.profilers = {};
5657
this._names = [];
@@ -153,6 +154,10 @@ Logger.prototype.log = function (level) {
153154
meta = rewriter(level, msg, meta);
154155
});
155156

157+
this.filters.forEach(function(filter) {
158+
msg = filter(msg);
159+
});
160+
156161
//
157162
// For consideration of terminal 'color" programs like colors.js,
158163
// which can add ANSI escape color codes to strings, we destyle the
@@ -472,6 +477,15 @@ Logger.prototype.addRewriter = function (rewriter) {
472477
this.rewriters.push(rewriter);
473478
}
474479

480+
//
481+
// ### function addFilter (filter)
482+
// #### @filter {function} Filter function, called with the message as single
483+
// argument, expected to return the filtered message.
484+
//
485+
Logger.prototype.addFilter = function (filter) {
486+
this.filters.push(filter);
487+
}
488+
475489
//
476490
// ### function clear ()
477491
// Remove all transports from this instance

test/log-filter-test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* log-filter-test.js: Test filtering of message content in winston.
2+
* (c) 2015 Chris Oloff
3+
* MIT LICENSE
4+
*/
5+
6+
var assert = require('assert'),
7+
vows = require('vows'),
8+
winston = require('../lib/winston'),
9+
helpers = require('./helpers');
10+
11+
/* To demo a filter, we filter out credit card numbers, assuming that a credit
12+
* card number consists of 13 or more digits (without spaces). This function
13+
* does the actual masking.
14+
*/
15+
function maskCardNumbers(s) {
16+
var match;
17+
while (match = s.match(/(\d{13}\d*)/)) {
18+
var toBeMasked = match[1];
19+
s = s.replace(toBeMasked, toBeMasked.substring(0,6) + '****' + toBeMasked.substring(toBeMasked.length - 4));
20+
}
21+
return s;
22+
}
23+
24+
vows.describe('winston/logger/filter').addBatch({
25+
"An instance of winston.Logger": {
26+
topic: new (winston.Logger)({transports: [
27+
new (winston.transports.Console)({ level: 'info' })
28+
]}),
29+
"the addFilter() method": {
30+
topic: function (logger) {
31+
logger.addFilter(function (msg) {
32+
return maskCardNumbers(msg);
33+
});
34+
return logger;
35+
},
36+
"should add the filter": function(logger) {
37+
assert.equal(helpers.size(logger.filters), 1);
38+
},
39+
"the log() method": {
40+
topic: function (logger) {
41+
logger.once('logging', this.callback);
42+
logger.log('info', 'card number 123456789012345 for testing');
43+
},
44+
"should filter the card number": function (transport, level, msg, meta) {
45+
assert.equal(msg, 'card number 123456****2345 for testing');
46+
}
47+
}
48+
}
49+
}
50+
}).export(module);
51+

0 commit comments

Comments
 (0)