logs.ts 604 B

123456789101112131415161718192021222324
  1. /**
  2. * @description log middleware
  3. * @type Function
  4. * @param options
  5. */
  6. const { middlewareLog } = require('../utils/logger');
  7. module.exports = () => {
  8. const loggerMiddleware = middlewareLog();
  9. return async(ctx, next) => {
  10. // 静态文件不处理
  11. const isApi = /^\/api/.test(ctx.url);
  12. if (!isApi) {
  13. await next();
  14. return;
  15. };
  16. return loggerMiddleware(ctx, next)
  17. .catch(error => {
  18. const { body, status, message } = error;
  19. const errorText = `res: ${JSON.stringify({ status, message, body })}`;
  20. ctx.$log.error(errorText);
  21. });
  22. };
  23. };