'use strict';

import {inspect} from 'node:util';
import {hrtime} from 'node:process';
import log from '../utils/logger';

/**
 * Logger middleware
 * async: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
 * await: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
 */
export function requestLogger(req, res, next) {
  const startTime = hrtime.bigint();
  next();
  const endTime = hrtime.bigint();

  log.info(
    `REQUEST: ${req.method} ${req.originalUrl} - execution: ${endTime - startTime} ns`,
  );
  log.info(
    `HEADER: ${inspect(req.header)}} - execution: ${endTime - startTime} ns`,
  );
  log.info(
    `BODY: ${inspect(req.body)} - execution: ${endTime - startTime} ns`,
  );
}
