Optimizations (#4135)

* removes costly json serialization to InMemoryCacheAdapter

* Always cache a copy of the array

* Use own mapValues

* Makes sure we dont make unnecessary calls to the logger

* Do not bypass loggers with silent logging (only applies to stdout)

* warn is not warning

* use ===

* Wrap logRequest / logResponse in the loggerController for more granular control

Also give the ability to pass functions to the logger so we don't serialize too early in JSON (costly)

* reconfiguring winston would override the transports levels and make subsequent tests fail
This commit is contained in:
Florent Vilmart
2017-09-04 20:47:49 -04:00
committed by GitHub
parent 17f4dcd176
commit 3079270b3e
8 changed files with 118 additions and 33 deletions

View File

@@ -17,8 +17,34 @@ export const LogOrder = {
ASCENDING: 'asc'
}
const logLevels = [
'error',
'warn',
'info',
'debug',
'verbose',
'silly',
]
export class LoggerController extends AdaptableController {
constructor(adapter, appId, options = {logLevel: 'info'}) {
super(adapter, appId, options);
let level = 'info';
if (options.verbose) {
level = 'verbose';
}
if (options.logLevel) {
level = options.logLevel;
}
const index = logLevels.indexOf(level); // info by default
logLevels.forEach((level, levelIndex) => {
if (levelIndex > index) { // silence the levels that are > maxIndex
this[level] = () => {};
}
});
}
maskSensitiveUrl(urlString) {
const password = url.parse(urlString, true).query.password;
@@ -80,7 +106,10 @@ export class LoggerController extends AdaptableController {
log(level, args) {
// make the passed in arguments object an array with the spread operator
args = this.maskSensitive([...args]);
args = [].concat(level, args);
args = [].concat(level, args.map((arg) => {
if (typeof arg === 'function') { return arg(); }
return arg;
}));
this.adapter.log.apply(this.adapter, args);
}
@@ -107,6 +136,36 @@ export class LoggerController extends AdaptableController {
silly() {
return this.log('silly', arguments);
}
logRequest({
method,
url,
headers,
body
}) {
this.verbose(() => {
const stringifiedBody = JSON.stringify(body, null, 2);
return `REQUEST for [${method}] ${url}: ${stringifiedBody}`;
}, {
method,
url,
headers,
body
});
}
logResponse({
method,
url,
result
}) {
this.verbose(
() => { const stringifiedResponse = JSON.stringify(result, null, 2);
return `RESPONSE from [${method}] ${url}: ${stringifiedResponse}`;
},
{result: result}
);
}
// check that date input is valid
static validDateTime(date) {
if (!date) {