Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a65ee94

Browse files
author
Michal Vlasák
committed
Update packages, refactor, add serializers, fields disabling, new interface
1 parent 210e33f commit a65ee94

5 files changed

Lines changed: 119 additions & 10 deletions

File tree

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage

.lintstagedrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"ignore": ['**/coverage/**/*.js'],
32
"linters": {
43
"*.js": [
54
"npm run pretty",

index.js

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,38 @@
33
// https://news.ycombinator.com/item?id=14209168
44
// https://tools.ietf.org/html/rfc5424
55

6+
const _ = require('lodash');
67
const pino = require('pino');
78
const multistream = require('pino-multi-stream').multistream;
9+
const serializers = require('./serializers');
810

9-
const pretty = pino.pretty();
10-
pretty.pipe(process.stdout);
11-
const prettyErr = pino.pretty();
12-
prettyErr.pipe(process.stderr);
11+
// options.disableFields = ['error.stack'];
12+
13+
const defaultLogger = (options = {}) => {
14+
const pretty = pino.pretty();
15+
pretty.pipe(process.stdout);
16+
const prettyErr = pino.pretty();
17+
prettyErr.pipe(process.stderr);
18+
19+
if (options.disableFields) {
20+
_.forEach(serializers, (value, key) => {
21+
const matcher = new RegExp(`^${key}.(.*)`);
22+
const affectedFields = [];
23+
options.disableFields.forEach(field => {
24+
field.replace(matcher, (match, p1) => {
25+
affectedFields.push(p1);
26+
});
27+
});
28+
29+
if (affectedFields.length > 0) {
30+
const newSerializer = obj => {
31+
return _.omit(value(obj), affectedFields);
32+
};
33+
serializers[key] = newSerializer;
34+
}
35+
});
36+
}
1337

14-
const loggerFactory = (moduleName, options = {}) => {
1538
const isProduction = process.env.NODE_ENV === 'production';
1639
const isTesting = process.env.NODE_ENV === 'test';
1740
let defaultLevel = 'debug';
@@ -36,12 +59,38 @@ const loggerFactory = (moduleName, options = {}) => {
3659
streams = options.streams;
3760
}
3861

39-
const logger = pino({ level: defaultLevel, timestamp: false, base: {} }, multistream(streams));
62+
const logger = pino({ level: defaultLevel, timestamp: false, base: {}, serializers }, multistream(streams));
4063
logger.warning = logger.warn;
64+
65+
return logger;
66+
};
67+
68+
let logger;
69+
70+
const loggerFactory = data => {
71+
let moduleName;
72+
let options;
73+
if (data) {
74+
if (_.isString(data)) {
75+
moduleName = data;
76+
} else if (_.isObject(data)) {
77+
options = data;
78+
} else {
79+
throw new TypeError(`Invalid argument of type ${typeof data}`);
80+
}
81+
}
82+
83+
if (!logger) {
84+
logger = defaultLogger(options);
85+
}
4186
if (!moduleName) {
4287
return logger;
4388
}
4489
return logger.child({ name: moduleName });
4590
};
4691

47-
module.exports = loggerFactory;
92+
const factoryProxy = new Proxy(loggerFactory, {
93+
get: (target, key) => target()[key],
94+
});
95+
96+
module.exports = factoryProxy;

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@
99
"test-watch": "jest --watch --coverage",
1010
"lint": "eslint .",
1111
"pretty": "prettier --write '*.js'",
12-
"precommit": "lint-staged && npm test"
12+
"precommit": "lint-staged && npm test",
13+
"check": "npm-check -i components -i config -i controllers -i enums -i errors -i middleware -i routes -i repositories -i services -i husky & exit 0"
1314
},
1415
"author": "Michal Vlasák <michal.vlasak@ackee.cz>",
1516
"license": "ISC",
1617
"dependencies": {
18+
"lodash": "^4.17.5",
1719
"pino": "^4.13.0",
1820
"pino-multi-stream": "^3.1.2"
1921
},
2022
"devDependencies": {
21-
"eslint": "4.17.0",
23+
"eslint": "^4.18.2",
2224
"eslint-config-ackee": "^0.8.1",
2325
"flush-write-stream": "^1.0.2",
2426
"husky": "^0.14.3",
2527
"jest": "^22.4.2",
2628
"lint-staged": "^7.0.0",
29+
"npm-check": "^5.5.2",
2730
"prettier": "^1.11.0",
2831
"split2": "^2.2.0"
2932
}

serializers.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const _ = require('lodash');
2+
3+
/**
4+
* Gets full URL from express Req object
5+
* @param {Req} req
6+
* @return {!string}
7+
*/
8+
const fullUrlFromReq = req => (req.get ? `${req.protocol}://${req.get('host')}${req.originalUrl}` : '');
9+
10+
module.exports = {
11+
error(obj) {
12+
return {
13+
message: _.get(obj, 'message'),
14+
code: _.get(obj, 'code'),
15+
stack: _.get(obj, 'stack'),
16+
data: _.get(obj, 'data'),
17+
};
18+
},
19+
processEnv(obj) {
20+
return {
21+
nodePath: _.get(obj, 'NODE_PATH'),
22+
nodeEnv: _.get(obj, 'NODE_ENV'),
23+
};
24+
},
25+
req(obj) {
26+
const body = _.cloneDeep(_.get(obj, 'body'));
27+
const query = _.cloneDeep(_.get(obj, 'query'));
28+
29+
if (_.get(body, 'password')) {
30+
delete body.password;
31+
}
32+
33+
if (_.get(query, 'password')) {
34+
delete query.password;
35+
}
36+
37+
if (_.get(body, 'passwordCheck')) {
38+
delete body.passwordCheck;
39+
}
40+
41+
if (_.get(query, 'passwordCheck')) {
42+
delete query.passwordCheck;
43+
}
44+
45+
return {
46+
body,
47+
query,
48+
url: fullUrlFromReq(obj),
49+
method: _.get(obj, 'method'),
50+
};
51+
},
52+
res(obj) {
53+
return {
54+
out: _.get(obj, 'out'),
55+
};
56+
},
57+
};

0 commit comments

Comments
 (0)