using stringifyForLogs

This commit is contained in:
Simon Martín 2019-02-27 10:40:49 +01:00
parent 932420e21b
commit e829df6367
2 changed files with 4 additions and 27 deletions

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const errorHandlerFactory = require('../services/error_handler_factory'); const errorHandlerFactory = require('../services/error_handler_factory');
const { stringifyForLogs } = require('../utils/logs');
const MAX_ERROR_STRING_LENGTH = 1024; const MAX_ERROR_STRING_LENGTH = 1024;
module.exports = function error() { module.exports = function error() {
@ -63,29 +64,5 @@ function setErrorHeader(errorHandler, res) {
message: errorHandler.message message: errorHandler.message
}; };
res.set('X-SQLAPI-Errors', stringifyForLogs(errorsLog)); res.set('X-SQLAPI-Errors', stringifyForLogs(errorsLog, MAX_ERROR_STRING_LENGTH));
}
/**
* Remove problematic nested characters
* from object for logs RegEx
*
* @param {Object} object
*/
function stringifyForLogs(object) {
Object.keys(object).map(key => {
if (typeof object[key] === 'string') {
object[key] = object[key]
.substring(0, MAX_ERROR_STRING_LENGTH)
.replace(/[^a-zA-Z0-9]/g, ' ');
} else if (typeof object[key] === 'object') {
stringifyForLogs(object[key]);
} else if (object[key] instanceof Array) {
for (let element of object[key]) {
stringifyForLogs(element);
}
}
});
return JSON.stringify(object);
} }

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const { stringifyForLogs } = require('../utils/logs');
const MAX_SQL_LENGTH = 2048; const MAX_SQL_LENGTH = 2048;
module.exports = function log() { module.exports = function log() {
@ -10,7 +11,7 @@ module.exports = function log() {
} }
} }
res.set('X-SQLAPI-Log', JSON.stringify(logObj)); res.set('X-SQLAPI-Log', stringifyForLogs(logObj, MAX_SQL_LENGTH));
return next(); return next();
}; };
@ -36,5 +37,4 @@ function prepareSQL(sql) {
return { return {
other: sql other: sql
} }
} }