Skip to content

Commit

Permalink
[doc] Add documentation for rewriters.
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jun 9, 2015
1 parent 0e9c704 commit 7f75b48
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +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)
* [Filters and Rewriters](#filters-and-rewriters)
* [Working with transports](#working-with-transports)
* [Adding Custom Transports](#adding-custom-transports)
* [Installation](#installation)
Expand Down Expand Up @@ -631,14 +631,16 @@ 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.
### Filters and Rewriters
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) {
return maskCardNumbers(msg);
logger.addFilter(function(msg, meta, level) {
return meta.production
? maskCardNumbers(msg)
: msg;
});

logger.info('transaction with card number 123456789012345 successful.');
```

Expand All @@ -648,8 +650,27 @@ 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.
Where as for rewriters, if you wanted to sanitize the `creditCard` field of your `meta` you could:

``` js
logger.addRewriter(function(level, msg, meta) {
if (meta.creditCard) {
meta.creditCard = maskCardNumbers(meta.creditCard)
}

return meta;
});

logger.info('transaction ok', { creditCard: 123456789012345 });
```

which may result in this output:

```
info: transaction ok creditCard=123456****2345
```

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

## 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

0 comments on commit 7f75b48

Please sign in to comment.