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

Commit 8a381b7

Browse files
committed
Merge branch 'change/lodash-modularize' into 'master'
Use lodash modules See merge request Backend/ackee-node-logger!8
2 parents 9bc69d8 + a0ae036 commit 8a381b7

5 files changed

Lines changed: 183 additions & 54 deletions

File tree

index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const _ = require('lodash');
1+
const get = require('lodash.get');
2+
const merge = require('lodash.merge');
3+
const isString = require('lodash.isstring');
4+
const isObject = require('lodash.isobject');
25
const pino = require('pino');
36
const multistream = require('pino-multi-stream').multistream;
47
const serializers = require('./serializers');
@@ -70,7 +73,7 @@ const defaultLogger = (options = {}) => {
7073
{ level: levels.warn, stream: process.stderr },
7174
];
7275
}
73-
if (!_.get(options, 'disableStackdriverFormat', false)) {
76+
if (!get(options, 'disableStackdriverFormat', false)) {
7477
streams = decorateStreams(streams, StackDriverFormatStream);
7578
}
7679

@@ -81,7 +84,7 @@ const defaultLogger = (options = {}) => {
8184
}
8285

8386
const logger = pino(
84-
_.merge(
87+
merge(
8588
{
8689
level: defaultLevel,
8790
timestamp: false,
@@ -112,9 +115,9 @@ const loggerFactory = data => {
112115
let moduleName;
113116
let options;
114117
if (data) {
115-
if (_.isString(data)) {
118+
if (isString(data)) {
116119
moduleName = data;
117-
} else if (_.isObject(data)) {
120+
} else if (isObject(data)) {
118121
options = data;
119122
} else {
120123
throw new TypeError(`Invalid argument of type ${typeof data}`);

package-lock.json

Lines changed: 119 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@
1515
"author": "Michal Vlasák <michal.vlasak@ackee.cz>",
1616
"license": "ISC",
1717
"dependencies": {
18-
"lodash": "^4.17.5",
18+
"lodash.assign": "^4.2.0",
19+
"lodash.clonedeep": "^4.5.0",
20+
"lodash.defaultsdeep": "^4.6.0",
21+
"lodash.foreach": "^4.5.0",
22+
"lodash.get": "^4.4.2",
23+
"lodash.isempty": "^4.4.0",
24+
"lodash.isobject": "^3.0.2",
25+
"lodash.isstring": "^4.0.1",
26+
"lodash.isundefined": "^3.0.1",
27+
"lodash.merge": "^4.6.1",
28+
"lodash.omit": "^4.5.0",
29+
"lodash.omitby": "^4.6.0",
30+
"lodash.pick": "^4.4.0",
31+
"omit-deep": "^0.3.0",
1932
"on-finished": "^2.3.0",
2033
"on-headers": "^1.0.1",
2134
"pino": "^4.13.0",

serializers.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
1-
const _ = require('lodash');
1+
const get = require('lodash.get');
2+
const omit = require('lodash.omit');
3+
const pick = require('lodash.pick');
4+
const omitBy = require('lodash.omitby');
5+
const defaultsDeep = require('lodash.defaultsdeep');
6+
const isUndefined = require('lodash.isundefined');
7+
const isEmpty = require('lodash.isempty');
8+
const forEach = require('lodash.foreach');
9+
const assign = require('lodash.assign');
210

311
const serializers = {
412
error(obj) {
513
return {
6-
message: _.get(obj, 'message'),
7-
code: _.get(obj, 'code'),
8-
stack: _.get(obj, 'stack'),
9-
data: _.get(obj, 'data'),
14+
message: get(obj, 'message'),
15+
code: get(obj, 'code'),
16+
stack: get(obj, 'stack'),
17+
data: get(obj, 'data'),
1018
};
1119
},
1220
process(obj) {
13-
const nodePath = _.get(obj.env, 'NODE_PATH');
14-
const nodeEnv = _.get(obj.env, 'NODE_ENV');
15-
return _.omitBy(
16-
_.defaultsDeep({ env: _.omitBy({ nodePath, nodeEnv }, _.isUndefined) }, _.omit(obj, 'env')),
17-
val => _.isUndefined(val) || _.isEmpty(val)
21+
const nodePath = get(obj.env, 'NODE_PATH');
22+
const nodeEnv = get(obj.env, 'NODE_ENV');
23+
return omitBy(
24+
defaultsDeep({ env: omitBy({ nodePath, nodeEnv }, isUndefined) }, omit(obj, 'env')),
25+
val => isUndefined(val) || isEmpty(val)
1826
);
1927
},
2028
req(obj) {
2129
const omitFields = ['password', 'passwordCheck'];
22-
const [body, query] = ['body', 'query'].map(name => _.omit(_.get(obj, name), omitFields));
30+
const [body, query] = ['body', 'query'].map(name => omit(get(obj, name), omitFields));
2331

24-
return _.omitBy(
32+
return omitBy(
2533
{
2634
body,
2735
query,
2836
url: obj.originalUrl || obj.url,
29-
method: _.get(obj, 'method'),
37+
method: get(obj, 'method'),
3038
},
31-
val => _.isUndefined(val) || _.isEmpty(val)
39+
val => isUndefined(val) || isEmpty(val)
3240
);
3341
},
3442
res(obj) {
3543
return {
36-
out: _.get(obj, 'out'),
37-
time: _.get(obj, 'time'),
44+
out: get(obj, 'out'),
45+
time: get(obj, 'time'),
3846
};
3947
},
4048
};
@@ -43,7 +51,7 @@ const disablePaths = paths => {
4351
if (!paths || paths.length <= 0) {
4452
return;
4553
}
46-
_.forEach(serializers, (value, key) => {
54+
forEach(serializers, (value, key) => {
4755
const matcher = new RegExp(`^${key}.(.*)`);
4856
const affectedFields = [];
4957
paths.forEach(field => {
@@ -54,7 +62,7 @@ const disablePaths = paths => {
5462

5563
if (affectedFields.length > 0) {
5664
const newSerializer = obj => {
57-
return _.omit(value(obj), affectedFields);
65+
return omit(value(obj), affectedFields);
5866
};
5967
serializers[key] = newSerializer;
6068
}
@@ -65,7 +73,7 @@ const enablePaths = paths => {
6573
if (!paths || paths.length <= 0) {
6674
return;
6775
}
68-
_.forEach(serializers, (value, key) => {
76+
forEach(serializers, (value, key) => {
6977
const matcher = new RegExp(`^${key}.(.*)`);
7078
const affectedFields = [];
7179
paths.forEach(field => {
@@ -76,9 +84,9 @@ const enablePaths = paths => {
7684

7785
if (affectedFields.length > 0) {
7886
const newSerializer = obj => {
79-
const newFields = _.pick(obj, affectedFields);
87+
const newFields = pick(obj, affectedFields);
8088
const originalResult = value(obj);
81-
return _.assign(originalResult, newFields);
89+
return assign(originalResult, newFields);
8290
};
8391
serializers[key] = newSerializer;
8492
}

tests/serializers.test.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const _ = require('lodash');
1+
const pick = require('lodash.pick');
2+
const omitDeep = require('omit-deep');
23
const stream = require('stream');
34

45
let loggerFactory;
@@ -32,11 +33,6 @@ test('Default serializers', () => {
3233
noteToSelf: 'Send to user',
3334
};
3435
const req = {
35-
query: {
36-
password: '1234',
37-
passwordCheck: '1234',
38-
search: 'my cat',
39-
},
4036
body: {
4137
password: '1234',
4238
passwordCheck: '1234',
@@ -53,23 +49,19 @@ test('Default serializers', () => {
5349
stream: new stream.Writable({
5450
write: (chunk, encoding, next) => {
5551
const json = JSON.parse(chunk);
56-
expect(json.error).toEqual(_.pick(error, ['message', 'code', 'stack', 'data']));
52+
expect(json.error).toEqual(pick(error, ['message', 'code', 'stack', 'data']));
5753
expect(json.process.env).toEqual({
5854
nodePath: process.env.NODE_PATH,
5955
nodeEnv: process.env.NODE_ENV,
6056
});
61-
expect(json.req).toEqual(
62-
_.pick(
63-
_.omit(req, [
64-
'body.password',
65-
'body.passwordCheck',
66-
'query.password',
67-
'query.passwordCheck',
68-
]),
69-
['url', 'body', 'query', 'method']
70-
)
71-
);
72-
expect(json.res).toEqual(_.pick(res, ['out', 'time']));
57+
const filteredReq = omitDeep(req, [
58+
'body.password',
59+
'body.passwordCheck',
60+
'query.password',
61+
'query.passwordCheck',
62+
]);
63+
expect(json.req).toEqual(pick(filteredReq, ['url', 'body', 'query', 'method']));
64+
expect(json.res).toEqual(pick(res, ['out', 'time']));
7365
loggerWrites();
7466
next();
7567
},
@@ -140,7 +132,7 @@ test('Disable custom path', () => {
140132
stream: new stream.Writable({
141133
write: (chunk, encoding, next) => {
142134
const json = JSON.parse(chunk);
143-
expect(json.req).toEqual(_.pick(req, ['body', 'query', 'method']));
135+
expect(json.req).toEqual(pick(req, ['body', 'query', 'method']));
144136
loggerWrites();
145137
next();
146138
},
@@ -168,7 +160,7 @@ test('Enable custom path', () => {
168160
stream: new stream.Writable({
169161
write: (chunk, encoding, next) => {
170162
const json = JSON.parse(chunk);
171-
expect(json.req).toEqual(_.pick(req, ['body', 'query', 'method', 'url', 'extraData']));
163+
expect(json.req).toEqual(pick(req, ['body', 'query', 'method', 'url', 'extraData']));
172164
loggerWrites();
173165
next();
174166
},

0 commit comments

Comments
 (0)