forked from winstonjs/winston
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-mutate.js
45 lines (37 loc) · 1.03 KB
/
format-mutate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const winston = require('../');
/*
* Simple string mask. For example purposes only.
*/
function maskCardNumbers(num) {
const str = num.toString();
const { length } = str;
return Array.from(str, (n, i) => {
return i < length - 4 ? '*' : n;
}).join('');
}
// Define the format that mutates the info object.
const maskFormat = winston.format(info => {
// You can CHANGE existing property values
if (info.creditCard) {
info.creditCard = maskCardNumbers(info.creditCard);
}
// You can also ADD NEW properties if you wish
info.hasCreditCard = !!info.creditCard;
return info;
});
// Then combine the format with other formats and make a logger
const logger = winston.createLogger({
format: winston.format.combine(
//
// Order is important here, the formats are called in the
// order they are passed to combine.
//
maskFormat(),
winston.format.json()
),
transports: [
new winston.transports.Console()
]
});
logger.info('transaction ok', { creditCard: 123456789012345 });