CartoDB-SQL-API/app/middlewares/log.js

41 lines
785 B
JavaScript
Raw Normal View History

2019-02-27 16:01:51 +08:00
'use strict';
2019-02-27 17:40:49 +08:00
const { stringifyForLogs } = require('../utils/logs');
2019-02-27 16:01:51 +08:00
const MAX_SQL_LENGTH = 2048;
module.exports = function log() {
return function logMiddleware(req, res, next) {
const logObj = {
request: {
sql: prepareSQL(res.locals.sql)
}
2019-02-27 18:59:04 +08:00
};
2019-02-27 16:01:51 +08:00
2019-02-27 17:40:49 +08:00
res.set('X-SQLAPI-Log', stringifyForLogs(logObj, MAX_SQL_LENGTH));
2019-02-27 16:01:51 +08:00
return next();
};
};
function prepareSQL(sql) {
if (!sql) {
return null;
}
if (typeof sql === 'string') {
return {
simple: sql.substring(0, MAX_SQL_LENGTH)
2019-02-27 18:59:04 +08:00
};
}
if (Array.isArray(sql)) {
return {
multiple: sql.map(q => q.substring(0, MAX_SQL_LENGTH))
2019-02-27 18:59:04 +08:00
};
}
return {
other: sql
2019-02-27 18:59:04 +08:00
};
2019-02-27 16:01:51 +08:00
}