diff --git a/src/listener.js b/src/listener.js index c362623..2f484e3 100644 --- a/src/listener.js +++ b/src/listener.js @@ -6,8 +6,10 @@ const zlib = require('zlib'); const router = require('./router'); const { logConfig, config } = require('./config'); const { drePool } = require('./db/nodeDb'); +const accessLogMiddleware = require('./routes/accessLogMiddleware'); const logger = require('./logger')('listener'); +const accessLogger = require('./logger')('access'); const exitHook = require('async-exit-hook'); const { pgClient, warp } = require('./warp'); const { postEvalQueue, registerQueue, updateQueue } = require('./bullQueue'); @@ -21,6 +23,7 @@ async function runListener() { const app = new Koa(); app + .use(accessLogMiddleware) .use(corsConfig()) .use(compress(compressionSettings)) .use(bodyParser()) @@ -33,6 +36,7 @@ async function runListener() { app.context.registerQueue = registerQueue; app.context.updateQueue = updateQueue; app.context.postEvalQueue = postEvalQueue; + app.context.accessLogger = accessLogger; app.listen(port); } diff --git a/src/routes/accessLogMiddleware.js b/src/routes/accessLogMiddleware.js new file mode 100644 index 0000000..0f57d38 --- /dev/null +++ b/src/routes/accessLogMiddleware.js @@ -0,0 +1,24 @@ +const util = require('node:util'); + +const LOG_FORMAT = '%s %s "%s %s HTTP/%s" %d %s %s[ms]'; + +module.exports = async (ctx, next) => { + const t0 = performance.now(); + await next(); + const t1 = performance.now(); + try { + ctx.accessLogger.debug(util.format( + LOG_FORMAT, + ctx.ip, + ctx.method, + `${ctx.path}${ctx.search}`, + ctx.req.httpVersion, + ctx.status, + ctx.length ? ctx.length.toString() : '-', + (t1 - t0).toFixed(3) + ) + ); + } catch (err) { + console.error(err); + } +}; diff --git a/src/routes/contract.js b/src/routes/contract.js index d4ed970..c242379 100644 --- a/src/routes/contract.js +++ b/src/routes/contract.js @@ -34,6 +34,7 @@ module.exports = async (ctx) => { const showValidity = ctx.query.validity === 'true'; const showErrorMessages = ctx.query.errorMessages === 'true'; const showErrors = ctx.query.errors === 'true'; + const showValidityCount = ctx.query.validityCount === 'true'; logger.info("New 'contract' request", { contractId, @@ -64,7 +65,9 @@ module.exports = async (ctx) => { page: parsedPage }; } - response.validityCount = await getContractValidityTotalCount(contractId, result.sortKey); + if (showValidityCount) { + response.validityCount = await getContractValidityTotalCount(contractId, result.sortKey); + } if (showErrorMessages) { const contractErrorMessages = await getContractErrorMessages(contractId, result.sortKey, parsedLimit, offset); response.errorMessages = contractErrorMessages.errorMessages;