access-log.js 641 B

123456789101112131415161718192021
  1. // app/middleware/gzip.js
  2. 'use strict';
  3. module.exports = ({ enable = false, body = false }) => async function accessLog(ctx, next) {
  4. if (enable) {
  5. ctx.app.logger.debug(`[access-log] ${ctx.logger.paddingMessage} start...`);
  6. if (body && ctx.method !== 'GET') {
  7. ctx.app.logger.debug('[access-log] request body', ctx.request.body);
  8. }
  9. }
  10. await next();
  11. if (enable) {
  12. ctx.app.logger.info(`[access-log] ${ctx.logger.paddingMessage} ${ctx.response.status} ${ctx.response.message}`);
  13. }
  14. if (body && ctx.acceptJSON) {
  15. ctx.app.logger.debug('[access-log] response body', ctx.response && ctx.response.body);
  16. }
  17. };