Log request with the username bound

This commit is contained in:
Daniel García Aubert 2020-07-23 12:14:39 +02:00
parent 1eae4ee41f
commit 6857a892f7
4 changed files with 11 additions and 4 deletions

View File

@ -2,12 +2,10 @@
const uuid = require('uuid');
module.exports = function initLogger ({ logger, logOnEvent = 'finish' }) {
module.exports = function initLogger ({ logger }) {
return function initLoggerMiddleware (req, res, next) {
const requestId = req.get('X-Request-Id') || uuid.v4();
res.locals.logger = logger.child({ request_id: requestId });
res.locals.logger.info({ client_request: req }, 'Incoming request');
res.on(logOnEvent, () => res.locals.logger.info({ server_response: res, status: res.statusCode }, 'Response sent'));
next();
};
};

View File

@ -1,6 +1,7 @@
'use strict';
const initLogger = require('../middlewares/logger');
const logReqRes = require('../middlewares/log-req-res');
const profiler = require('../middlewares/profiler');
const user = require('../middlewares/user');
const authorization = require('../middlewares/authorization');
@ -32,6 +33,7 @@ module.exports = class CopyController {
return [
initLogger({ logger: this.logger }),
user(this.metadataBackend),
logReqRes(),
profiler({ statsClient: this.statsdClient }),
tag({ tags: ['ingestion', 'copyfrom'] }),
rateLimits(this.userLimitsService, endpointGroup),
@ -46,8 +48,9 @@ module.exports = class CopyController {
const copyToMiddlewares = endpointGroup => {
return [
initLogger({ logger: this.logger, logOnEvent: 'log' }),
initLogger({ logger: this.logger }),
user(this.metadataBackend),
logReqRes({ logOnEvent: 'log' }),
profiler({ statsClient: this.statsdClient, logOnEvent: 'log' }),
tag({ tags: ['ingestion', 'copyto'], logOnEvent: 'log' }),
bodyParser(),

View File

@ -3,6 +3,7 @@
const util = require('util');
const initLogger = require('../middlewares/logger');
const logReqRes = require('../middlewares/log-req-res');
const profiler = require('../middlewares/profiler');
const bodyParser = require('../middlewares/body-parser');
const user = require('../middlewares/user');
@ -35,6 +36,7 @@ module.exports = class JobController {
sqlRouter.post('/job', [
initLogger({ logger: this.logger }),
user(this.metadataBackend),
logReqRes(),
profiler({ statsClient: this.statsdClient }),
tag({ tags: ['job', 'create'] }),
bodyParser(),
@ -46,6 +48,7 @@ module.exports = class JobController {
sqlRouter.get('/job/:job_id', [
initLogger({ logger: this.logger }),
user(this.metadataBackend),
logReqRes(),
profiler({ statsClient: this.statsdClient }),
tag({ tags: ['job', 'retrieve'] }),
bodyParser(),
@ -55,6 +58,7 @@ module.exports = class JobController {
sqlRouter.delete('/job/:job_id', [
initLogger({ logger: this.logger }),
user(this.metadataBackend),
logReqRes(),
profiler({ statsClient: this.statsdClient }),
tag({ tags: ['job', 'cancel'] }),
bodyParser(),

View File

@ -1,6 +1,7 @@
'use strict';
const initLogger = require('../middlewares/logger');
const logReqRes = require('../middlewares/log-req-res');
const profiler = require('../middlewares/profiler');
const bodyParser = require('../middlewares/body-parser');
const user = require('../middlewares/user');
@ -41,6 +42,7 @@ module.exports = class QueryController {
return [
initLogger({ logger: this.logger }),
user(this.metadataBackend),
logReqRes(),
profiler({ statsClient: this.statsdClient }),
tag({ tags: ['query'] }),
bodyParser(),