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

42 lines
928 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
module.exports = function log() {
return function logMiddleware(req, res, next) {
2019-02-27 19:51:01 +08:00
const MAX_SQL_LENGTH = (global.settings.logQueries && global.settings.maxQueriesLogLength) || 1024;
2019-02-27 16:01:51 +08:00
const logObj = {
request: {
2019-02-27 19:51:01 +08:00
sql: prepareSQL(res.locals.sql, MAX_SQL_LENGTH)
2019-02-27 16:01:51 +08:00
}
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();
};
};
2019-02-27 19:51:01 +08:00
function prepareSQL(sql, MAX_SQL_LENGTH) {
if (!sql || !global.settings.logQueries) {
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
}