auto_logger.js 967 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. /**
  3. * 自动记录日志
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/30
  7. * @version
  8. */
  9. module.exports = options => {
  10. return function* autoLogger(next) {
  11. // 自动记录log的action
  12. const autoLogAction = ['save', 'delete'];
  13. if (this.actionName !== undefined && autoLogAction.indexOf(this.actionName) >= 0) {
  14. // 操作数据的id
  15. const idReg = /\/(\d+)/;
  16. const paramInfo = this.request.originalUrl.match(idReg);
  17. let targetId = paramInfo[1] !== undefined ? paramInfo[1] : -1;
  18. targetId = parseInt(targetId);
  19. const logData = {
  20. controller: this.controllerName,
  21. action: this.actionName,
  22. operation: this.currentName === undefined ? '保存数据' : this.currentName,
  23. target_id: targetId,
  24. };
  25. yield this.service.log.addLog(logData);
  26. }
  27. yield next;
  28. };
  29. };